diff --git a/frontend/src/components/Markers.tsx b/frontend/src/components/Markers.tsx index e48cfd0..2f76dda 100644 --- a/frontend/src/components/Markers.tsx +++ b/frontend/src/components/Markers.tsx @@ -7,10 +7,12 @@ import location from '../img/location.png'; interface ExtendedWaypointRecord extends WaypointRecord { drawn: boolean; + zIndex: number; } function Markers({ conditions }: { conditions: WaypointRecord[] }) { const [drawnConditions, setDrawnConditions] = useState([]); + const [countConditionsDrawn, setCountConditionsDrawn] = useState(0); useEffect(() => { setDrawnConditions( @@ -18,6 +20,7 @@ function Markers({ conditions }: { conditions: WaypointRecord[] }) { return { ...condition, drawn: false, + zIndex: 0, }; }) ); @@ -28,17 +31,38 @@ function Markers({ conditions }: { conditions: WaypointRecord[] }) { const [MarkerIcon, setMarkerIcon] = useState(); const handleMarkerClick = (name: string) => { - setDrawnConditions(previousValue => { - return previousValue.map(condition => { - if (condition.waypoint.name === name) { - return { - ...condition, - drawn: !condition.drawn, - }; + const copy = drawnConditions.map(condition => { + if (condition.waypoint.name === name) { + if (condition.drawn === true && condition.zIndex === 0) { + if (countConditionsDrawn === 1) { + condition.drawn = false; + } else { + condition.zIndex = 1001; + } + } else if (condition.drawn === true) { + condition.drawn = false; + condition.zIndex = 0; + } else { + condition.drawn = true; + condition.zIndex = 1001; } - return condition; - }); + } else { + condition.zIndex = 0; + } + // console.log(condition.waypoint.name + ' ' + condition.zIndex); + return condition; }); + setDrawnConditions(copy); + + setCountConditionsDrawn( + drawnConditions.reduce((count, condition) => { + if (condition.drawn === true) { + return count + 1; + } else { + return count; + } + }, 0) + ); }; useMapEvent('zoomend', () => { @@ -68,9 +92,15 @@ function Markers({ conditions }: { conditions: WaypointRecord[] }) { if (condition.drawn) { return ( <> - handleMarkerClick(waypoint.name) }} /> - handleMarkerClick(waypoint.name) }} /> - handleMarkerClick(waypoint.name) }} /> + handleMarkerClick(waypoint.name) }} zIndexOffset={condition.zIndex} /> + handleMarkerClick(waypoint.name) }} zIndexOffset={condition.zIndex} /> + handleMarkerClick(waypoint.name) }} + zIndexOffset={condition.zIndex} + /> ); }