forked from ONEARMY/community-platform
-
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.
feat: disable zoom in button when location denied
- Loading branch information
Showing
3 changed files
with
72 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { useEffect, useState } from 'react' | ||
import { Tooltip } from 'react-tooltip' | ||
import { Button } from 'oa-components' | ||
import { logger } from 'src/logger' | ||
|
||
import { GetLocation } from '../../utils/geolocation' | ||
|
||
interface IProps { | ||
setCenter: (ILatLng) => void | ||
setZoom: (number) => void | ||
} | ||
|
||
const ZOOM_IN_TOOLTIP = 'Zoom in to your location' | ||
const DENIED_TOOLTIP = 'Request to get your location already denied' | ||
|
||
export const ButtonZoomIn = ({ setCenter, setZoom }: IProps) => { | ||
const [isDisabled, setIsDisabled] = useState<boolean>(false) | ||
|
||
useEffect(() => { | ||
navigator.permissions.query({ name: 'geolocation' }).then((result) => { | ||
if (result.state === 'denied') { | ||
setIsDisabled(true) | ||
} | ||
}) | ||
}, []) | ||
|
||
const promptUserLocation = async () => { | ||
try { | ||
const position = await GetLocation() | ||
setCenter({ | ||
lat: position.coords.latitude, | ||
lng: position.coords.longitude, | ||
}) | ||
} catch (error) { | ||
if (error === 'User denied geolocation prompt') { | ||
setIsDisabled(true) | ||
} | ||
logger.error(error) | ||
} | ||
} | ||
|
||
const sx = { | ||
backgroundColor: 'white', | ||
borderRadius: 99, | ||
padding: 4, | ||
':hover': { | ||
backgroundColor: 'lightgray', | ||
}, | ||
} | ||
|
||
return ( | ||
<> | ||
<Button | ||
data-tooltip-content={isDisabled ? DENIED_TOOLTIP : ZOOM_IN_TOOLTIP} | ||
data-cy="LocationViewButton" | ||
data-tooltip-id="locationButton-tooltip" | ||
sx={sx} | ||
onClick={() => { | ||
promptUserLocation() | ||
setZoom(9) | ||
}} | ||
disabled={isDisabled} | ||
icon="gps-location" | ||
/> | ||
<Tooltip id="locationButton-tooltip" place="left" /> | ||
</> | ||
) | ||
} |
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