// components/pois/poiUpdateModal.js import React, { useState, useEffect } from "react"; import Select from "react-select"; // Importiere react-select import { useRecoilState } from "recoil"; import { selectedPoiState } from "../../store/atoms/poiState"; import { currentPoiState } from "../../store/atoms/currentPoiState"; import { mapLayersState } from "../../store/atoms/mapLayersState"; const PoiUpdateModal = ({ onClose, poiData, onSubmit }) => { const currentPoi = useRecoilState(currentPoiState); const selectedPoi = useRecoilState(selectedPoiState); const [mapLayersVisibility] = useRecoilState(mapLayersState); const [poiId, setPoiId] = useState(poiData ? poiData.idPoi : ""); const [name, setName] = useState(poiData ? poiData.name : ""); const [poiTypData, setPoiTypData] = useState([]); const [poiTypeId, setPoiTypeId] = useState(""); const [locationDeviceData, setLocationDeviceData] = useState([]); const [filteredDevices, setFilteredDevices] = useState([]); const [deviceName, setDeviceName] = useState(poiData ? poiData.deviceName : null); // Verwende null für react-select const [idLD, setIdLD] = useState(poiData ? poiData.idLD : ""); const [description, setDescription] = useState(poiData ? poiData.description : ""); // Map von Systemnamen zu IDs (wie zuvor) const systemNameToIdMap = { TALAS: 1, ECI: 2, ULAF: 3, GSMModem: 5, CiscoRouter: 6, WAGO: 7, Siemens: 8, OTDR: 9, WDM: 10, GMA: 11, Messdatensammler: 12, Messstellen: 13, TALASICL: 100, DAUZ: 110, SMSFunkmodem: 111, Basisgerät: 200, }; useEffect(() => { if (poiData) { //console.log("Initial poiData:", poiData); setPoiId(poiData.idPoi); setName(poiData.name); setPoiTypeId(poiData.idPoiTyp); setIdLD(poiData.idLD); setDescription(poiData.description); setDeviceName(poiData.idLD); //console.log("Loaded POI Data for editing:", poiData); } }, [poiData]); useEffect(() => { const fetchDeviceId = async () => { if (poiData && poiData.idLD) { try { const response = await fetch(`/api/talas_v5_DB/locationDevice/getDeviceIdById?idLD=${poiData.idLD}`); const data = await response.json(); if (data) setDeviceName(data.name); } catch (error) { console.error("Fehler beim Abrufen der Geräteinformation in PoiUpdateModel.js: ", error); } } }; fetchDeviceId(); }, [poiData]); const handleDeletePoi = async () => { if (confirm("Sind Sie sicher, dass Sie diesen POI löschen möchten?")) { try { const response = await fetch(`/api/talas_v5_DB/pois/deletePoi?id=${poiId}`, { method: "DELETE", }); if (response.ok) { alert("POI wurde erfolgreich gelöscht."); onClose(); window.location.reload(); } else { throw new Error("Fehler beim Löschen des POI."); } } catch (error) { console.error("Fehler beim Löschen des POI 2:", error); alert("Fehler beim Löschen des POI."); } } }; useEffect(() => { const fetchPoiTypData = async () => { try { const response = await fetch("/api/talas_v5_DB/poiTyp/readPoiTyp"); const data = await response.json(); setPoiTypData(data); if (selectedPoi && data) { const matchingType = data.find((pt) => pt.name === selectedPoi.typ); if (matchingType) { setPoiTypeId(matchingType.idPoiTyp); } } } catch (error) { console.error("Fehler beim Abrufen der poiTyp Daten:", error); } }; fetchPoiTypData(); }, [selectedPoi]); useEffect(() => { const fetchData = async () => { try { // const response = await fetch("/api/talas_v5/location_device"); //"/api/talas_v5_DB/locationDevice/location_device" const response = await fetch("/api/talas_v5_DB/locationDevice/locationDevices"); const data = await response.json(); setLocationDeviceData(data); filterDevices(data); if (poiData && poiData.idLD) { const selectedDevice = data.find((device) => device.id === poiData.idLD); setDeviceName(selectedDevice ? selectedDevice.id : data[0].id); //console.log("Selected Device in poiUpdate:", selectedDevice); } } catch (error) { console.error("Fehler beim Abrufen der Standort- und Gerätedaten:", error); } }; fetchData(); }, []); useEffect(() => { fetch("/api/talas_v5_DB/locationDevice/locationDevices") .then((response) => response.json()) .then((data) => { setLocationDeviceData(data); const currentDevice = data.find((device) => device.idLD === currentPoi.idLD); if (currentDevice) { setDeviceName(currentDevice.name); //console.log("Current Device name in poiUpdate:", currentDevice.name); } }) .catch((error) => { console.error("Fehler beim Abrufen der Gerätedaten:", error); setLocationDeviceData([]); }); }, [poiData?.idLD, currentPoi]); // Funktion zum Filtern der Geräte basierend auf den aktiven Systemen (Layern) const filterDevices = (devices) => { const activeSystems = Object.keys(mapLayersVisibility).filter((system) => mapLayersVisibility[system]); // Mappe aktive Systeme auf ihre ids const activeSystemIds = activeSystems.map((system) => systemNameToIdMap[system]).filter((id) => id !== undefined); // Filtere die Geräte nach aktiven Systemen basierend auf idsystem_typ const filtered = devices.filter((device) => activeSystemIds.includes(device.idsystem_typ)); setFilteredDevices(filtered); }; const handleSubmit = async (event) => { event.preventDefault(); const idLDResponse = await fetch(`/api/talas_v5_DB/locationDevice/getDeviceId?deviceName=${encodeURIComponent(deviceName)}`); const idLDData = await idLDResponse.json(); const idLD = idLDData.idLD; try { const response = await fetch("/api/talas_v5_DB/pois/updatePoi", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ idPoi: poiId, name: name, description: description, idPoiTyp: poiTypeId, idLD: idLD, }), }); if (response.ok) { onClose(); window.location.reload(); } else { const errorResponse = await response.json(); throw new Error(errorResponse.error || "Fehler beim Aktualisieren des POI."); } } catch (error) { console.error("Fehler beim Aktualisieren des POI:", error); alert("Fehler beim Aktualisieren des POI."); } }; const handleDeletePoi = async () => { if (confirm("Sind Sie sicher, dass Sie diesen POI löschen möchten?")) { try { const response = await fetch(`/api/talas_v5_DB/pois/deletePoi?id=${poiId}`, { method: "DELETE", }); if (response.ok) { onClose(); window.location.reload(); // Aktualisiert die Seite nach dem Löschen } else { throw new Error("Fehler beim Löschen des POI."); } } catch (error) { console.error("Fehler beim Löschen des POI:", error); alert("Fehler beim Löschen des POI."); } } }; // Erstelle Optionen für react-select const poiTypeOptions = poiTypData.map((poiTyp) => ({ value: poiTyp.idPoiTyp, label: poiTyp.name, })); const deviceOptions = filteredDevices.map((device) => ({ value: device.name, label: device.name, })); // Custom styles for react-select const customStyles = { control: (provided) => ({ ...provided, width: "100%", minWidth: "300px", // Minimum width for the dropdown maxWidth: "100%", // Maximum width (you can adjust this if needed) }), menu: (provided) => ({ ...provided, width: "100%", minWidth: "300px", // Ensure the dropdown menu stays at the minimum width }), }; return (