From 3db31cd9f1838fa0593c1a32f27e37e546630872 Mon Sep 17 00:00:00 2001 From: alimpens Date: Mon, 6 Jan 2025 17:07:11 +0100 Subject: [PATCH 1/7] Add current location button --- .../locatie/kies/_components/BaseLayer.tsx | 25 ++++++++++++++++--- .../CurrentLocationButton.tsx | 23 +++++++++++++++++ .../Notification/Notification.module.css | 3 +++ .../_components/Notification/Notification.tsx | 6 +++++ .../locatie/kies/_components/map.module.css | 15 +++++++++++ 5 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 apps/public/src/app/(map)/locatie/kies/_components/CurrentLocationButton/CurrentLocationButton.tsx create mode 100644 apps/public/src/app/(map)/locatie/kies/_components/Notification/Notification.module.css create mode 100644 apps/public/src/app/(map)/locatie/kies/_components/Notification/Notification.tsx diff --git a/apps/public/src/app/(map)/locatie/kies/_components/BaseLayer.tsx b/apps/public/src/app/(map)/locatie/kies/_components/BaseLayer.tsx index 36ae1413..497f0d05 100644 --- a/apps/public/src/app/(map)/locatie/kies/_components/BaseLayer.tsx +++ b/apps/public/src/app/(map)/locatie/kies/_components/BaseLayer.tsx @@ -1,11 +1,16 @@ +import { Paragraph } from '@amsterdam/design-system-react' import L from 'leaflet' import { useEffect, useRef, useState } from 'react' import 'leaflet/dist/leaflet.css' +import { CurrentLocationButton } from './CurrentLocationButton/CurrentLocationButton' import styles from './map.module.css' +import { Notification } from './Notification/Notification' export const BaseLayer = () => { - const containerRef = useRef(null) + const mapRef = useRef(null) + + const [notification, setNotification] = useState<{ heading: string; description: string } | null>(null) // Use state instead of a ref for storing the Leaflet map object otherwise you may run into DOM issues when React StrictMode is enabled const [mapInstance, setMapInstance] = useState(null) @@ -15,11 +20,11 @@ export const BaseLayer = () => { useEffect(() => { // Ensure that the target DOM element exists and that the map doesn't already exist (to prevent duplicate renders in StrictMode) - if (containerRef.current === null || createdMapInstance.current !== false) { + if (mapRef.current === null || createdMapInstance.current !== false) { return undefined } - const map = new L.Map(containerRef.current, { + const map = new L.Map(mapRef.current, { center: L.latLng([52.370216, 4.895168]), zoom: 14, layers: [ @@ -57,5 +62,17 @@ export const BaseLayer = () => { } }, [mapInstance]) - return
+ return ( +
+
+
+ Mijn locatie + {notification && ( + setNotification(null)}> + {notification.description} + + )} +
+
+ ) } diff --git a/apps/public/src/app/(map)/locatie/kies/_components/CurrentLocationButton/CurrentLocationButton.tsx b/apps/public/src/app/(map)/locatie/kies/_components/CurrentLocationButton/CurrentLocationButton.tsx new file mode 100644 index 00000000..cca076e6 --- /dev/null +++ b/apps/public/src/app/(map)/locatie/kies/_components/CurrentLocationButton/CurrentLocationButton.tsx @@ -0,0 +1,23 @@ +import type { ButtonProps } from '@amsterdam/design-system-react' +import { Button } from '@amsterdam/design-system-react' + +type Props = { + setNotification: (notification: { heading: string; description: string } | null) => void +} & ButtonProps + +export const CurrentLocationButton = ({ setNotification, ...restProps }: Props) => { + const onSuccess = () => console.log('success') + + const onError = () => { + setNotification({ + heading: 'meldingen.amsterdam.nl heeft geen toestemming om uw locatie te gebruiken.', + description: 'Dit kunt u wijzigen in de voorkeuren of instellingen van uw browser of systeem.', + }) + } + + const handleClick = () => { + navigator.geolocation.getCurrentPosition(onSuccess, onError) + } + + return {notification && ( setNotification(null)}> {notification.description} diff --git a/apps/public/src/app/(map)/locatie/kies/_components/CurrentLocationButton/CurrentLocationButton.tsx b/apps/public/src/app/(map)/locatie/kies/_components/CurrentLocationButton/CurrentLocationButton.tsx deleted file mode 100644 index cca076e6..00000000 --- a/apps/public/src/app/(map)/locatie/kies/_components/CurrentLocationButton/CurrentLocationButton.tsx +++ /dev/null @@ -1,23 +0,0 @@ -import type { ButtonProps } from '@amsterdam/design-system-react' -import { Button } from '@amsterdam/design-system-react' - -type Props = { - setNotification: (notification: { heading: string; description: string } | null) => void -} & ButtonProps - -export const CurrentLocationButton = ({ setNotification, ...restProps }: Props) => { - const onSuccess = () => console.log('success') - - const onError = () => { - setNotification({ - heading: 'meldingen.amsterdam.nl heeft geen toestemming om uw locatie te gebruiken.', - description: 'Dit kunt u wijzigen in de voorkeuren of instellingen van uw browser of systeem.', - }) - } - - const handleClick = () => { - navigator.geolocation.getCurrentPosition(onSuccess, onError) - } - - return