-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: useMapsLibrary returns API object instead of boolean
BREAKING CHANGE: loading multiple libraries at once is no longer supported, changed the return type of useMapsLibrary.
- Loading branch information
1 parent
9169bca
commit a50fb37
Showing
18 changed files
with
329 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {APILoadingStatus} from '../components/api-provider'; | ||
import {useApiLoadingStatus} from './use-api-loading-status'; | ||
/** | ||
* Hook to check if the Google Maps API is loaded | ||
*/ | ||
export function useApiIsLoaded(): boolean { | ||
const status = useApiLoadingStatus(); | ||
|
||
return status === APILoadingStatus.LOADED; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {useContext} from 'react'; | ||
import {APILoadingStatus, APIProviderContext} from '../components/api-provider'; | ||
|
||
export function useApiLoadingStatus(): APILoadingStatus { | ||
return useContext(APIProviderContext)?.status || APILoadingStatus.NOT_LOADED; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {useContext, useEffect} from 'react'; | ||
|
||
import {APIProviderContext} from '../components/api-provider'; | ||
import {useApiIsLoaded} from './use-api-is-loaded'; | ||
|
||
interface ApiLibraries { | ||
core: google.maps.CoreLibrary; | ||
maps: google.maps.MapsLibrary; | ||
places: google.maps.PlacesLibrary; | ||
geocoding: google.maps.GeocodingLibrary; | ||
routes: google.maps.RoutesLibrary; | ||
marker: google.maps.MarkerLibrary; | ||
geometry: google.maps.GeometryLibrary; | ||
elevation: google.maps.ElevationLibrary; | ||
streetView: google.maps.StreetViewLibrary; | ||
journeySharing: google.maps.JourneySharingLibrary; | ||
drawing: google.maps.DrawingLibrary; | ||
visualization: google.maps.VisualizationLibrary; | ||
} | ||
|
||
export function useMapsLibrary< | ||
K extends keyof ApiLibraries, | ||
V extends ApiLibraries[K] | ||
>(name: K): V | null; | ||
|
||
export function useMapsLibrary(name: string) { | ||
const apiIsLoaded = useApiIsLoaded(); | ||
const ctx = useContext(APIProviderContext); | ||
|
||
useEffect(() => { | ||
if (!apiIsLoaded || !ctx) return; | ||
|
||
// Trigger loading the libraries via our proxy-method. | ||
// The returned promise is ignored, since importLibrary will update loadedLibraries | ||
// list in the context, triggering a re-render. | ||
void ctx.importLibrary(name); | ||
}, [apiIsLoaded, ctx?.importLibrary]); | ||
|
||
return ctx?.loadedLibraries[name] || null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import type {GoogleMapsApiLoader as ActualLoader} from '../google-maps-api-loader'; | ||
|
||
// FIXME: this should no longer be needed with the next version of @googlemaps/jest-mocks | ||
import {importLibraryMock} from './lib/import-library-mock'; | ||
|
||
export class GoogleMapsApiLoader { | ||
static load: typeof ActualLoader.load = jest.fn(() => Promise.resolve()); | ||
static load: typeof ActualLoader.load = jest.fn(() => { | ||
google.maps.importLibrary = importLibraryMock; | ||
return Promise.resolve(); | ||
}); | ||
} |
Oops, something went wrong.