// /components/pois/PoiUpdateModal.js import React, { useState, useEffect } from "react"; import Select from "react-select"; import { useSelector, useDispatch } from "react-redux"; import { fetchLocationDevicesThunk } from "../../redux/thunks/database/fetchLocationDevicesThunk"; import { fetchPoiTypThunk } from "../../redux/thunks/database/pois/fetchPoiTypThunk"; import { selectMapLayersState } from "../../redux/slices/mapLayersSlice"; import { selectPoiTypData, selectPoiTypStatus } from "../../redux/slices/database/pois/poiTypSlice"; import { deletePoiThunk } from "../../redux/thunks/database/pois/deletePoiThunk"; import { updatePoiThunk } from "../../redux/thunks/database/pois/updatePoiThunk"; const PoiUpdateModal = ({ onClose, poiData }) => { const dispatch = useDispatch(); const mapLayersVisibility = useSelector(selectMapLayersState); const poiTypData = useSelector(selectPoiTypData); const poiTypStatus = useSelector(selectPoiTypStatus); const devices = useSelector(state => state.locationDevicesFromDB.devices); const [poiId, setPoiId] = useState(poiData?.idPoi || ""); const [name, setName] = useState(poiData?.name || ""); const [description, setDescription] = useState(poiData?.description || ""); const [deviceName, setDeviceName] = useState(null); const [poiTypeId, setPoiTypeId] = useState(null); const systemNameToIdMap = {}; useEffect(() => { dispatch(fetchLocationDevicesThunk()); if (poiTypStatus === "idle") { dispatch(fetchPoiTypThunk()); } }, [dispatch, poiTypStatus]); useEffect(() => { if (poiData && devices.length > 0) { const selectedDevice = devices.find(device => device.idLD === poiData.idLD); if (selectedDevice) { setDeviceName({ value: selectedDevice.idLD, label: selectedDevice.name }); } } }, [poiData, devices]); useEffect(() => { if (poiData && poiTypData.length > 0) { const selectedTyp = poiTypData.find(typ => typ.idPoiTyp === poiData.idPoiTyp); if (selectedTyp) { setPoiTypeId({ value: selectedTyp.idPoiTyp, label: selectedTyp.name }); } } }, [poiData, poiTypData]); const filterDevices = () => { const activeSystems = Object.keys(mapLayersVisibility).filter( system => mapLayersVisibility[system] ); const activeSystemIds = activeSystems .map(system => systemNameToIdMap[system]) .filter(id => id !== undefined); return devices.filter(device => activeSystemIds.includes(device.idsystem_typ)); }; const handleSubmit = async event => { event.preventDefault(); try { await dispatch( updatePoiThunk({ idPoi: poiId, name, description, idPoiTyp: poiTypeId?.value ?? poiData?.idPoiTyp, idLD: deviceName?.value, }) ).unwrap(); onClose(); window.location.reload(); } 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 { await dispatch(deletePoiThunk(poiId)).unwrap(); onClose(); window.location.reload(); } catch (error) { console.error("Fehler beim Löschen des POI:", error); alert("Fehler beim Löschen des POI."); } } }; const poiTypeOptions = Array.isArray(poiTypData) ? poiTypData.map(poiTyp => ({ value: poiTyp.idPoiTyp, label: poiTyp.name, })) : []; const deviceOptions = filterDevices().map(device => ({ value: device.idLD, label: device.name, })); const customStyles = { control: provided => ({ ...provided, width: "100%", minWidth: "300px", maxWidth: "100%", }), menu: provided => ({ ...provided, width: "100%", minWidth: "300px", }), }; return (