fix: useEffect nur einmal ausführen "POI hinzufügen" war manchmal zweimal oder mehr vorhanden im Kontextmenü

This commit is contained in:
ISA
2024-06-06 08:38:52 +02:00
parent 29bf479101
commit 3d72b3682f

View File

@@ -48,6 +48,7 @@ const plusRoundIcon = L.icon({
}); });
const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => { const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => {
const [menuItemAdded, setMenuItemAdded] = useState(false);
const linePositions = lineCoordinates || [ const linePositions = lineCoordinates || [
[52.505, 8], [52.505, 8],
[52, 8.5], [52, 8.5],
@@ -1135,28 +1136,31 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => {
}).addTo(map); }).addTo(map);
} }
}, [map]); */ }, [map]); */
const addItemsToMapContextMenu = () => {
console.log("contextMenuItems hasRights:", hasRights);
map.contextmenu.addItem({ const addItemsToMapContextMenu = () => {
text: "POI hinzufügen", if (!menuItemAdded) {
icon: "img/add_station.png", console.log("contextMenuItems hasRights:", hasRights);
className: "background-red",
callback: (event) => addStationCallback(event, 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(() => { useEffect(() => {
if (map && poiLayerRef.current && isPoiTypLoaded) { if (map && poiLayerRef.current && isPoiTypLoaded && !menuItemAdded) {
addItemsToMapContextMenu(); addItemsToMapContextMenu();
} }
}, [ }, [
map, map,
locations, poiLayerRef,
onLocationUpdate,
poiReadTrigger,
isPoiTypLoaded, isPoiTypLoaded,
userRights, menuItemAdded, // Hinzufügen zu den Abhängigkeiten, um den Effekt korrekt zu steuern
]); ]);
//------------------------------------------ //------------------------------------------