Skip to content

Commit

Permalink
console: Fix location form
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelJankoski committed Nov 22, 2023
1 parent f84566c commit 089bf09
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions pkg/webui/console/components/location-form/index.js
Original file line number Diff line number Diff line change
@@ -104,8 +104,8 @@ const LocationForm = props => {
} = props

const form = useRef(null)
const [latitude, setLatitude] = useState(defaultLocation[0])
const [longitude, setLongitude] = useState(defaultLocation[1])
const [latitude, setLatitude] = useState(props.initialValues.latitude)
const [longitude, setLongitude] = useState(props.initialValues.longitude)
const [zoom, setZoom] = useState(14)
const [error, setError] = useState(undefined)
const [mapCenter, setMapCenter] = useState(undefined)
@@ -123,40 +123,41 @@ const LocationForm = props => {
markers.push({ position: { longitude, latitude } })
}

useEffect(() => {
if (entryExists) {
setLatitude(initialValues.latitude)
setLongitude(initialValues.longitude)
setMapCenter([initialValues.latitude, initialValues.longitude])
}
}, [entryExists, initialValues.latitude, initialValues.longitude])

useEffect(() => {
if (!hasLocationSet(initialValues) && additionalMarkers.length === 0) {
if ('geolocation' in navigator) {
const getCurrentLocation = useCallback(async () => {
let newState = { mapCenter: defaultLocation }
if (
!hasLocationSet(initialValues) &&
additionalMarkers.length === 0 &&
'geolocation' in navigator
) {
newState = await new Promise(resolve => {
navigator.geolocation.getCurrentPosition(
position => {
const latitude = isNaN(position.coords.latitude)
? defaultLocation[0]
: position.coords.latitude
const longitude = isNaN(position.coords.longitude)
? defaultLocation[1]
: position.coords.longitude
setLatitude(latitude)
setLongitude(longitude)
setMapCenter([latitude, longitude])
setLoading(false)
resolve({
mapCenter: [
isNaN(position.coords.latitude) ? defaultLocation[0] : position.coords.latitude,
isNaN(position.coords.longitude) ? defaultLocation[1] : position.coords.longitude,
],
})
},
() => {
setMapCenter(defaultLocation)
setZoom(2)
setLoading(false)
resolve({ mapCenter: defaultLocation, zoom: 2 })
},
)
}
})
}

return newState
}, [additionalMarkers.length, initialValues])

useEffect(() => {
getCurrentLocation().then(res => {
setMapCenter(res.mapCenter)
setZoom(res.zoom ?? 2)
setLoading(false)
})
}, [getCurrentLocation])

const handleSubmit = useCallback(
async (values, { setSubmitting }) => {
setError(undefined)

0 comments on commit 089bf09

Please sign in to comment.