// components/AddStationPopup.js import React, { useState, useEffect, use } from "react"; import ReactDOM from "react-dom"; import { setPoiMarkers } from "../redux/slices/readPoiMarkersStoreSlice"; import { selectGisStationsStatic } from "../redux/slices/webService/gisStationsStaticSlice"; import { useDispatch, useSelector } from "react-redux"; import { fetchPoiTypes } from "../redux/slices/db/poiTypesSlice"; import { incrementTrigger } from "../redux/slices/poiReadFromDbTriggerSlice"; const ShowAddStationPopup = ({ onClose, map, latlng }) => { const dispatch = useDispatch(); const poiTypData = useSelector((state) => state.poiTypes.data); const [name, setName] = useState(""); const [poiTypeId, setPoiTypeId] = useState(""); // Initialize as string const [poiTypeName, setPoiTypeName] = useState(""); // Initialize as string const [latitude] = useState(latlng.lat.toFixed(5)); const [longitude] = useState(latlng.lng.toFixed(5)); const [deviceName, setDeviceName] = useState(""); //----------------------------------------------------- useEffect(() => { const fetchpoiTypData = async () => { try { const response = await fetch("/api/talas_v5_DB/poiTyp/readPoiTyp"); const data = await response.json(); setpoiTypData(data); if (data && data.length > 0) { console.log("POI-Typen geladen:", data); setPoiTypeId(data[0].idPoiTyp); // Setzt den ersten Typ setPoiTypeName(data[0].name); } } catch (error) { console.error("Fehler beim Abrufen der poiTyp Daten:", error); } }; fetchpoiTypData(); }, []); useEffect(() => { if (poiTypData.length > 0 && !poiTypeId) { setPoiTypeId(poiTypData[0].idPoiTyp); } }, [poiTypData]); useEffect(() => { console.log("Aktueller POI Type:", poiTypeId); }, [poiTypeId]); //------------------------------------------------------------------------------------------ const gisStationsStatic = useSelector(selectGisStationsStatic); const locationDeviceData = gisStationsStatic?.Points ?? []; console.log("gisStationsStatic aus AddPOIModal:", gisStationsStatic); useEffect(() => { if (locationDeviceData?.length > 0) { console.log("🎯 Gerätedaten erfolgreich geladen:", locationDeviceData); setDeviceName((prev) => prev || locationDeviceData[0]?.LD_Name || ""); } }, [locationDeviceData]); //------------------------------------------------------------------------------------------ //-----------------handleSubmit------------------- const handleSubmit = async (event) => { event.preventDefault(); const formData = { name, poiTypeId: Number(poiTypeId), // Umwandlung in eine Zahl latitude, longitude, idLD: locationDeviceData.find((device) => device.LD_Name === deviceName)?.IdLD, }; const response = await fetch("/api/talas_v5_DB/pois/addLocation", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(formData), }); if (response.ok) { dispatch(incrementTrigger()); onClose(); window.location.reload(); } else { console.error("Fehler beim Hinzufügen des POI"); } if (map && typeof map.closePopup === "function") { map.closePopup(); } //Seite neu laden window.location.reload(); }; //----------------- // POI-Typen aus Redux laden, wenn die Komponente gemountet wird useEffect(() => { dispatch(fetchPoiTypes()); }, [dispatch]); //--------------------- return (
e.stopPropagation()}> {/* Schließen-Button */} {/* Modal-Inhalt */}
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}
); }; export default ShowAddStationPopup;