From 3d72b3682faa9e46ce9f8a9bb84230b0e9c083e6 Mon Sep 17 00:00:00 2001 From: ISA Date: Thu, 6 Jun 2024 08:38:52 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20useEffect=20nur=20einmal=20ausf=C3=BChre?= =?UTF-8?q?n=20"POI=20hinzuf=C3=BCgen"=20war=20manchmal=20zweimal=20oder?= =?UTF-8?q?=20mehr=20vorhanden=20im=20Kontextmen=C3=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/MapComponent.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/components/MapComponent.js b/components/MapComponent.js index 7b891e9ce..4ebaa2bfa 100644 --- a/components/MapComponent.js +++ b/components/MapComponent.js @@ -48,6 +48,7 @@ const plusRoundIcon = L.icon({ }); const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => { + const [menuItemAdded, setMenuItemAdded] = useState(false); const linePositions = lineCoordinates || [ [52.505, 8], [52, 8.5], @@ -1135,28 +1136,31 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => { }).addTo(map); } }, [map]); */ - const addItemsToMapContextMenu = () => { - console.log("contextMenuItems hasRights:", hasRights); - map.contextmenu.addItem({ - text: "POI hinzufügen", - icon: "img/add_station.png", - className: "background-red", - callback: (event) => addStationCallback(event, hasRights), - }); + const addItemsToMapContextMenu = () => { + if (!menuItemAdded) { + console.log("contextMenuItems hasRights:", hasRights); + + map.contextmenu.addItem({ + text: "POI hinzufügen", + icon: "img/add_station.png", + className: "background-red", + callback: (event) => addStationCallback(event, hasRights), + }); + + setMenuItemAdded(true); // Menüpunkt wurde hinzugefült, Zustand aktualisieren + } }; - //-------------------------------------------------- + useEffect(() => { - if (map && poiLayerRef.current && isPoiTypLoaded) { + if (map && poiLayerRef.current && isPoiTypLoaded && !menuItemAdded) { addItemsToMapContextMenu(); } }, [ map, - locations, - onLocationUpdate, - poiReadTrigger, + poiLayerRef, isPoiTypLoaded, - userRights, + menuItemAdded, // Hinzufügen zu den Abhängigkeiten, um den Effekt korrekt zu steuern ]); //------------------------------------------