// /utils/markerUtils.js import circleIcon from "../components/CircleIcon"; import { saveLineData } from "./mapUtils"; import { redrawPolyline } from "./mapUtils"; import L from "leaflet"; import { toast } from "react-toastify"; export const insertNewMarker = (closestPoints, newPoint, lineData, map) => { const newMarker = L.marker(newPoint, { icon: circleIcon, draggable: true, }).addTo(map); lineData.coordinates.splice(closestPoints[2], 0, [newPoint.lat, newPoint.lng]); // Hier direkt speichern nach Einfügen saveLineData(lineData); redrawPolyline(lineData); // Event-Listener für das Verschieben des Markers hinzufügen newMarker.on("dragend", () => { const newCoords = newMarker.getLatLng(); setNewCoords(newCoords); const newCoordinates = [...lineData.coordinates]; newCoordinates[closestPoints[2]] = [newCoords.lat, newCoords.lng]; lineData.coordinates = newCoordinates; redrawPolyline(lineData); updateMarkerPosition(newMarker.getLatLng(), lineData, newMarker); saveLineData(lineData); // Speichern der neuen Koordinaten nach dem Verschieben }); }; export const removeMarker = (marker, lineData, currentZoom, currentCenter) => { // Save zoom and center to localStorage //localStorage.setItem("mapZoom", currentZoom); //localStorage.setItem("mapCenter", JSON.stringify(currentCenter)); // Find the index of the coordinate that matches the marker's position const index = lineData.coordinates.findIndex((coord) => L.latLng(coord[0], coord[1]).equals(marker.getLatLng())); if (index !== -1) { // Remove the coordinate from the line data lineData.coordinates.splice(index, 1); // Redraw the polyline with the updated coordinates redrawPolyline(lineData); // Remove the marker from the map marker.remove(); // Save the updated line data saveLineData(lineData); // Refresh the browser window.location.reload(); } }; export const handleEditPoi = ( marker, userRights, setCurrentPoiData, setShowPoiUpdateModal, fetchPoiData, toast // Hier toast als Parameter erhalten ) => { console.log("Selected Marker ID (idPoi):", marker.options.id); console.log("Selected Marker Description:", marker.options.description); console.log("User Rights:", userRights); // Sicherstellen, dass userRights ein Array ist if (!Array.isArray(userRights)) { console.error("User Rights is not an array:", userRights); toast.error("Benutzerrechte sind ungültig.", { position: "top-center", autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, }); return; } console.log( "User Rights include 56:", userRights.some((r) => r.IdRight === 56) ); // Prüfung, ob der Benutzer die notwendigen Rechte hat if (!userRights.some((r) => r.IdRight === 56)) { toast.error("Benutzer hat keine Berechtigung zum Bearbeiten.", { position: "top-center", autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, }); console.log("Benutzer hat keine Berechtigung zum Bearbeiten."); return; // Beendet die Funktion frühzeitig, wenn keine Berechtigung vorliegt } setCurrentPoiData({ idPoi: marker.options.id, name: marker.options.name, description: marker.options.description, }); fetchPoiData(marker.options.id); setShowPoiUpdateModal(true); };