From 9269dd8cf6a011b64491d9339b398c34096a8b32 Mon Sep 17 00:00:00 2001 From: Leon K Date: Sat, 15 Jul 2023 21:51:34 +0200 Subject: [PATCH] add click event handler to show tables --- frontend/src/components/Markers.tsx | 62 +++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/frontend/src/components/Markers.tsx b/frontend/src/components/Markers.tsx index de03c3b..e117fad 100644 --- a/frontend/src/components/Markers.tsx +++ b/frontend/src/components/Markers.tsx @@ -4,19 +4,45 @@ import { Marker, useMap, useMapEvent } from 'react-leaflet'; import { useEffect, useState } from 'react'; import { WaypointRecord } from 'interfaces/waypointRecord.interface'; import location from '../img/location.png'; -import React from 'react'; + +interface ExtendedWaypointRecord extends WaypointRecord { + drawn: boolean; +} function Markers({ conditions }: { conditions: WaypointRecord[] }) { + const [drawnConditions, setDrawnConditions] = useState([]); + + useEffect(() => { + setDrawnConditions( + conditions.map(condition => { + return { + ...condition, + drawn: false, + }; + }) + ); + }, [conditions]); + const map = useMap(); const [zoom, setZoom] = useState(map.getZoom()); const [MarkerIcon, setMarkerIcon] = useState(); + const handleMarkerClick = (name: string) => { + setDrawnConditions(previousValue => { + const index = previousValue.findIndex(element => element.waypoint.name === name); + + previousValue[index].drawn = !previousValue[index].drawn; + + return previousValue; + }); + }; + useMapEvent('zoomend', () => { setZoom(map.getZoom()); }); useEffect(() => { - const IconSize = 1 / zoom + 15; + const IconSize = 1 / (zoom * 2) + 15; setMarkerIcon( new Icon({ iconUrl: location, @@ -29,21 +55,25 @@ function Markers({ conditions }: { conditions: WaypointRecord[] }) { return null; // Return null if there are no conditions to render } - // Render markers based on the conditions - const markers = conditions.map((condition, index) => { - const { waypoint } = condition; - const { latitude, longitude } = waypoint; - - // Render a marker using the latitude and longitude - return ( - - - - - ); - }); + return ( + <> + {drawnConditions && + drawnConditions.map(condition => { + const { waypoint } = condition; + const { latitude, longitude } = waypoint; + if (condition.drawn) { + return ( + <> + handleMarkerClick(waypoint.name) }} /> + handleMarkerClick(waypoint.name) }} /> + + ); + } - return <>{markers}; + return handleMarkerClick(waypoint.name) }} />; + })} + + ); } function MarkerConditionTable(condition: WaypointRecord, zoom: number) {