-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28c9f09
commit 7697d23
Showing
6 changed files
with
92 additions
and
111 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
40 changes: 18 additions & 22 deletions
40
apps/frontend/src/components/Fields/MapField/MapHandler.tsx
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,28 +1,24 @@ | ||
|
||
import './MapField.scss' | ||
import _ from 'lodash' | ||
import { useEffect, useRef, useState } from 'react' | ||
import { MapPoint, Nullish } from '@3dp4me/types'; | ||
import { | ||
useMap | ||
} from '@vis.gl/react-google-maps'; | ||
|
||
import { MapPoint, Nullish } from '@3dp4me/types' | ||
import { useMap } from '@vis.gl/react-google-maps' | ||
import { useEffect } from 'react' | ||
|
||
interface MapHandlerProps { | ||
place: Nullish<MapPoint>; | ||
place: Nullish<MapPoint> | ||
} | ||
|
||
export const MapHandler = ({ place }: MapHandlerProps) => { | ||
const map = useMap(); | ||
const map = useMap() | ||
|
||
useEffect(() => { | ||
if (!map || !place ) return; | ||
|
||
if (place) { | ||
// const bounds = new google.maps.LatLngBounds({ lat: place.latitude, lng: place.longitude }); | ||
map.setCenter({ lat: place.latitude, lng: place.longitude }); | ||
} | ||
}, [map, place]); | ||
|
||
return null; | ||
}; | ||
|
||
if (!map || !place) return | ||
|
||
if (place) { | ||
// const bounds = new google.maps.LatLngBounds({ lat: place.latitude, lng: place.longitude }); | ||
map.setCenter({ lat: place.latitude, lng: place.longitude }) | ||
} | ||
}, [map, place]) | ||
|
||
return null | ||
} |
82 changes: 41 additions & 41 deletions
82
apps/frontend/src/components/Fields/MapField/PlaceAutocomplete.tsx
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,54 +1,54 @@ | ||
/* global google */ | ||
import './MapField.scss' | ||
import _ from 'lodash' | ||
|
||
import { Nullish } from '@3dp4me/types' | ||
import { useMapsLibrary } from '@vis.gl/react-google-maps' | ||
import { useEffect, useRef, useState } from 'react' | ||
import { Nullish } from '@3dp4me/types'; | ||
import { | ||
useMapsLibrary, | ||
} from '@vis.gl/react-google-maps'; | ||
import styled from 'styled-components'; | ||
import styled from 'styled-components' | ||
|
||
interface PlaceAutocompleteProps { | ||
onPlaceSelect: (place: Nullish<google.maps.places.PlaceResult>) => void; | ||
onPlaceSelect: (place: Nullish<google.maps.places.PlaceResult>) => void | ||
} | ||
|
||
const AutocompleteContainer = styled.div` | ||
width: 300px; | ||
input { | ||
width: 100%; | ||
height: 40px; | ||
padding: 0 12px; | ||
font-size: 18px; | ||
box-sizing: border-box; | ||
} | ||
width: 300px; | ||
input { | ||
width: 100%; | ||
height: 40px; | ||
padding: 0 12px; | ||
font-size: 18px; | ||
box-sizing: border-box; | ||
} | ||
` | ||
|
||
export const PlaceAutocomplete = ({ onPlaceSelect }: PlaceAutocompleteProps) => { | ||
const [placeAutocomplete, setPlaceAutocomplete] = useState<Nullish<google.maps.places.Autocomplete>>(null); | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
const places = useMapsLibrary('places'); | ||
|
||
const [placeAutocomplete, setPlaceAutocomplete] = | ||
useState<Nullish<google.maps.places.Autocomplete>>(null) | ||
const inputRef = useRef<HTMLInputElement>(null) | ||
const places = useMapsLibrary('places') | ||
|
||
useEffect(() => { | ||
if (!places || !inputRef.current) return; | ||
const options = { | ||
fields: ['geometry', 'name', 'formatted_address'] | ||
}; | ||
setPlaceAutocomplete(new places.Autocomplete(inputRef.current, options)); | ||
}, [places]); | ||
if (!places || !inputRef.current) return | ||
|
||
const options = { | ||
fields: ['geometry', 'name', 'formatted_address'], | ||
} | ||
|
||
setPlaceAutocomplete(new places.Autocomplete(inputRef.current, options)) | ||
}, [places]) | ||
|
||
useEffect(() => { | ||
if (!placeAutocomplete) return; | ||
placeAutocomplete.addListener('place_changed', () => { | ||
onPlaceSelect(placeAutocomplete.getPlace()); | ||
}); | ||
}, [onPlaceSelect, placeAutocomplete]); | ||
if (!placeAutocomplete) return | ||
|
||
placeAutocomplete.addListener('place_changed', () => { | ||
onPlaceSelect(placeAutocomplete.getPlace()) | ||
}) | ||
}, [onPlaceSelect, placeAutocomplete]) | ||
|
||
return ( | ||
<AutocompleteContainer> | ||
<input ref={inputRef} /> | ||
</AutocompleteContainer> | ||
); | ||
}; | ||
<AutocompleteContainer> | ||
<input ref={inputRef} /> | ||
</AutocompleteContainer> | ||
) | ||
} |
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