Skip to content

Commit

Permalink
[google-maps-input] Move API key + locale resolving to loader
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Oct 6, 2020
1 parent 096c50c commit 8dfa784
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 14 deletions.
5 changes: 5 additions & 0 deletions packages/@sanity/google-maps-input/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"react/prop-types": "off"
}
}
5 changes: 1 addition & 4 deletions packages/@sanity/google-maps-input/src/GeopointInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {GeopointSelect} from './GeopointSelect'
import {GoogleMapsLoadProxy} from './GoogleMapsLoadProxy'
import {Geopoint, GeopointSchemaType} from './types'

const locale = (typeof window !== 'undefined' && window.navigator.language) || 'en'

const getStaticImageUrl = value => {
const loc = `${value.lat},${value.lng}`
const params = {
Expand Down Expand Up @@ -148,15 +146,14 @@ class GeopointInput extends React.Component<InputProps, InputState> {
isOpen={this.state.modalOpen}
>
<div className={styles.dialogInner}>
<GoogleMapsLoadProxy apiKey={config.apiKey} locale={locale}>
<GoogleMapsLoadProxy>
{api => (
<GeopointSelect
api={api}
value={value}
onChange={this.handleChange}
defaultLocation={config.defaultLocation}
defaultZoom={config.defaultZoom}
locale={locale}
/>
)}
</GoogleMapsLoadProxy>
Expand Down
1 change: 0 additions & 1 deletion packages/@sanity/google-maps-input/src/GeopointSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const fallbackLatLng: LatLng = {lat: 40.7058254, lng: -74.1180863}
interface SelectProps {
api: typeof window.google.maps
value?: Geopoint
locale: string
onChange: (latLng: google.maps.LatLng) => void
defaultLocation?: LatLng
defaultZoom?: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import React from 'react'
import {loadGoogleMapsApi} from './loadGoogleMapsApi'

interface LoadProps {
apiKey: string
locale?: string
children: (api: typeof window.google.maps) => React.ReactElement
}

Expand Down Expand Up @@ -31,8 +29,7 @@ export class GoogleMapsLoadProxy extends React.Component<LoadProps, LoadState> {
return
}

const {apiKey, locale} = this.props
loadGoogleMapsApi(apiKey, locale)
loadGoogleMapsApi()
.then(api => this.setState({loading: false, api}))
.catch(error => this.setState({error}))
}
Expand Down
11 changes: 6 additions & 5 deletions packages/@sanity/google-maps-input/src/loadGoogleMapsApi.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import config from 'config:@sanity/google-maps-input'

const locale = (typeof window !== 'undefined' && window.navigator.language) || 'en'

let loadingPromise: Promise<typeof window.google.maps>

export function loadGoogleMapsApi(
apiKey: string,
locale?: string
): Promise<typeof window.google.maps> {
export function loadGoogleMapsApi(): Promise<typeof window.google.maps> {
const callbackName = '___sanity_googleMapsApiCallback'
const selectedLocale = locale || 'en-US'

Expand All @@ -23,7 +24,7 @@ export function loadGoogleMapsApi(

const script = document.createElement('script')
script.onerror = reject
script.src = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&libraries=places&callback=${callbackName}&language=${selectedLocale}`
script.src = `https://maps.googleapis.com/maps/api/js?key=${config.apiKey}&libraries=places&callback=${callbackName}&language=${selectedLocale}`
document.getElementsByTagName('head')[0].appendChild(script)
})

Expand Down

0 comments on commit 8dfa784

Please sign in to comment.