120 lines
3.8 KiB
JavaScript
120 lines
3.8 KiB
JavaScript
// /utils/poiUtils.js
|
|
import circleIcon from "../components/gisPolylines/icons/CircleIcon.js";
|
|
import { saveLineData, redrawPolyline } from "./mapUtils.js";
|
|
import L from "leaflet";
|
|
import "leaflet.smooth_marker_bouncing";
|
|
import { toast } from "react-toastify";
|
|
import * as config from "../config/config.js";
|
|
|
|
export const insertNewPOI = (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", () => {
|
|
console.log("Marker wurde verschoben in insertNewPOI");
|
|
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 removePOI = (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
|
|
) => {
|
|
const editMode = localStorage.getItem("editMode") === "true";
|
|
userRights = editMode ? userRights : undefined;
|
|
//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 includes 56:", userRights.includes(56));
|
|
|
|
// Prüfung, ob der Benutzer die notwendigen Rechte hat
|
|
if (!userRights.includes(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,
|
|
idLD: marker.options.idLD,
|
|
});
|
|
|
|
fetchPoiData(marker.options.id);
|
|
|
|
setShowPoiUpdateModal(true);
|
|
};
|
|
//-------------------------------------------------------------------
|