// /components/AddPOIModal.js import React, { useState, useEffect } from "react"; import { useDispatch, useSelector } from "react-redux"; import { selectGisStationsStaticDistrict } from "../../redux/slices/webservice/gisStationsStaticDistrictSlice"; import { fetchPoiTypes } from "../../redux/slices/database/pois/poiTypesSlice"; import { incrementTrigger } from "../../redux/slices/poiReadFromDbTriggerSlice"; import { addPoiThunk } from "../../redux/thunks/database/pois/addPoiThunk"; import { fetchPoiIconsDataThunk } from "../../redux/thunks/database/pois/fetchPoiIconsDataThunk"; const AddPOIModal = ({ onClose, map, latlng }) => { const dispatch = useDispatch(); const poiTypData = useSelector((state) => state.poiTypes.data); const status = useSelector((state) => state.addPoi.status); const error = useSelector((state) => state.addPoi.error); const [name, setName] = useState(""); const [poiTypeId, setPoiTypeId] = useState(""); const [deviceName, setDeviceName] = useState(""); const [latitude] = useState(latlng.lat.toFixed(5)); const [longitude] = useState(latlng.lng.toFixed(5)); const gisStationsStatic = useSelector(selectGisStationsStaticDistrict); const locationDeviceData = gisStationsStatic?.Points ?? []; useEffect(() => { if (poiTypData.length > 0 && !poiTypeId) { setPoiTypeId(poiTypData[0].idPoiTyp); } }, [poiTypData]); useEffect(() => { if (locationDeviceData?.length > 0) { setDeviceName((prev) => prev || locationDeviceData[0]?.LD_Name || ""); } }, [locationDeviceData]); useEffect(() => { dispatch(fetchPoiTypes()); }, [dispatch]); const handleSubmit = async (event) => { event.preventDefault(); const formData = { name, poiTypeId: Number(poiTypeId), latitude, longitude, idLD: locationDeviceData.find((device) => device.LD_Name === deviceName)?.IdLD, }; try { await dispatch(addPoiThunk(formData)).unwrap(); dispatch(incrementTrigger()); onClose(); // Icons im Hintergrund nachladen (nicht blockierend) setTimeout(() => { dispatch(fetchPoiIconsDataThunk()); }, 100); } catch (error) { console.error("Fehler beim Hinzufügen des POI:", error); } if (map?.closePopup) { map.closePopup(); } }; return (
e.stopPropagation()}>
setName(e.target.value)} placeholder="Name der Station" className="block p-2 w-full border-2 border-gray-200 rounded-md text-sm" />
Lat: {latitude} Lng: {longitude}
{status === "loading" &&
Wird hinzugefügt...
} {status === "failed" && error &&
Fehler: {error}
}
); }; export default AddPOIModal;