104 lines
2.8 KiB
JavaScript
104 lines
2.8 KiB
JavaScript
// /utils/poiUtils.js
|
|
import L from "leaflet";
|
|
import { toast } from "react-toastify";
|
|
import circleIcon from "@/components/gisPolylines/icons/CircleIcon.js";
|
|
import { redrawPolyline } from "@/utils/polylines/redrawPolyline.js";
|
|
|
|
import { store } from "../redux/store";
|
|
import { updatePolylineCoordinatesThunk } from "../redux/thunks/database/polylines/updatePolylineCoordinatesThunk";
|
|
import { cleanupMarkers } from "@/utils/common/cleanupMarkers";
|
|
|
|
// 🧠 Zentrale Redux-Dispatch-Hilfsfunktion
|
|
const savePolylineRedux = lineData => {
|
|
return store
|
|
.dispatch(
|
|
updatePolylineCoordinatesThunk({
|
|
idModul: lineData.idModul,
|
|
idLD: lineData.idLD,
|
|
newCoordinates: lineData.coordinates,
|
|
})
|
|
)
|
|
.unwrap()
|
|
.catch(error => {
|
|
console.error("Fehler beim Speichern der Linienkoordinaten:", error.message);
|
|
});
|
|
};
|
|
|
|
export const insertNewPOI = (closestPoints, newPoint, lineData, map) => {
|
|
const newMarker = L.marker(newPoint, {
|
|
icon: circleIcon,
|
|
draggable: true,
|
|
}).addTo(map);
|
|
|
|
// Stützpunkt in Koordinatenliste einfügen
|
|
lineData.coordinates.splice(closestPoints[2], 0, [newPoint.lat, newPoint.lng]);
|
|
|
|
// Redux: Speichern der neuen Koordinaten
|
|
savePolylineRedux(lineData);
|
|
redrawPolyline(lineData);
|
|
|
|
newMarker.on("dragend", () => {
|
|
const newCoords = newMarker.getLatLng();
|
|
const newCoordinates = [...lineData.coordinates];
|
|
newCoordinates[closestPoints[2]] = [newCoords.lat, newCoords.lng];
|
|
lineData.coordinates = newCoordinates;
|
|
|
|
redrawPolyline(lineData);
|
|
updateMarkerPosition(newCoords, lineData, newMarker);
|
|
savePolylineRedux(lineData);
|
|
});
|
|
};
|
|
|
|
export const removePOI = (marker, lineData, currentZoom, currentCenter) => {
|
|
const index = lineData.coordinates.findIndex(coord =>
|
|
L.latLng(coord[0], coord[1]).equals(marker.getLatLng())
|
|
);
|
|
|
|
if (index !== -1) {
|
|
lineData.coordinates.splice(index, 1);
|
|
redrawPolyline(lineData);
|
|
cleanupMarkers([marker]);
|
|
savePolylineRedux(lineData);
|
|
window.location.reload();
|
|
}
|
|
};
|
|
|
|
export const updateMarkerPosition = (newCoords, lineData, marker) => {
|
|
if (!marker) return;
|
|
marker.setLatLng([newCoords.lat, newCoords.lng]);
|
|
};
|
|
|
|
export const handleEditPoi = (
|
|
marker,
|
|
userRights,
|
|
setCurrentPoiData,
|
|
setShowPoiUpdateModal,
|
|
fetchPoiData,
|
|
toast
|
|
) => {
|
|
if (!Array.isArray(userRights)) {
|
|
toast.error("Benutzerrechte sind ungültig.", {
|
|
position: "top-center",
|
|
autoClose: 5000,
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (!userRights.some(r => r.IdRight === 56)) {
|
|
toast.error("Benutzer hat keine Berechtigung zum Bearbeiten.", {
|
|
position: "top-center",
|
|
autoClose: 5000,
|
|
});
|
|
return;
|
|
}
|
|
|
|
setCurrentPoiData({
|
|
idPoi: marker.options.id,
|
|
name: marker.options.name,
|
|
description: marker.options.description,
|
|
});
|
|
|
|
fetchPoiData(marker.options.id);
|
|
setShowPoiUpdateModal(true);
|
|
};
|