import React, { useState, useEffect } from "react"; import ReactDOM from "react-dom"; const ShowAddStationPopup = ({ map, latlng }) => { const [poiTypData2, setPoiTypData2] = useState(); // Recoil State verwenden const [name, setName] = useState(""); const [poiTypeId, setPoiTypeId] = useState(""); // Initialize as string const [latitude] = useState(latlng.lat.toFixed(5)); const [longitude] = useState(latlng.lng.toFixed(5)); useEffect(() => { const fetchPoiTypData2 = async () => { try { const response = await fetch("/api/poiTyp"); const data = await response.json(); setPoiTypData2(data); if (data && data.length > 0) { setPoiTypeId(data[0].idPoiTyp); // Set initial poiTypeId to the id of the first poiType console.log( "Initial poiTypeId set in ShowAddStationPopup.js :", data[0].idPoiTyp ); } } catch (error) { console.error("Fehler beim Abrufen der poiTyp Daten:", error); } }; fetchPoiTypData2(); }, []); const handleSubmit = (event) => { event.preventDefault(); const formData = { name, // Name der Station poiTypeId, // Typ der Station, logged as idPoiTyp latitude, // Breitengrad longitude, // Längengrad }; fetch("/api/addLocation", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(formData), }) .then((response) => response.json()) .then((data) => console.log(data)) // Handle the response data .catch((error) => console.error(error)); // Handle any errors // Check if map is not undefined and call closePopup if (map && typeof map.closePopup === "function") { map.closePopup(); } else { console.error("Map object is undefined or closePopup is not a function"); } }; return (
); }; export default ShowAddStationPopup;