- Integriere react-select für eine benutzerfreundlichere Auswahl der Geräte und POI-Typen.
- Anwende benutzerdefinierte Styles, um eine einheitliche und übersichtliche Darstellung der Dropdowns zu gewährleisten.
- Sicherstellung, dass Platzhalter ("Gerät auswählen..." und "Typ auswählen...") immer angezeigt werden.
- Bereinige das Layout für eine konsistente Benutzererfahrung in beiden Modalen.
166 lines
5.5 KiB
JavaScript
166 lines
5.5 KiB
JavaScript
// components/pois/AddPoiModalWindow.js
|
|
import React, { useState, useEffect } from "react";
|
|
import Select from "react-select"; // Importiere react-select
|
|
import { useSetRecoilState } from "recoil";
|
|
import { readPoiMarkersStore } from "../../store/selectors/readPoiMarkersStore";
|
|
import { poiReadFromDbTriggerAtom } from "../../store/atoms/poiReadFromDbTriggerAtom";
|
|
|
|
const AddPoiModalWindow = ({ onClose, map, latlng }) => {
|
|
const [poiTypData, setpoiTypData] = useState([]);
|
|
const [name, setName] = useState("");
|
|
const [poiTypeId, setPoiTypeId] = useState(null); // Verwende null für react-select
|
|
const [latitude] = useState(latlng.lat.toFixed(5));
|
|
const [longitude] = useState(latlng.lng.toFixed(5));
|
|
const setTrigger = useSetRecoilState(poiReadFromDbTriggerAtom);
|
|
const [locationDeviceData, setLocationDeviceData] = useState([]);
|
|
const [deviceName, setDeviceName] = useState(null); // Verwende null für react-select
|
|
|
|
useEffect(() => {
|
|
const fetchInitialData = async () => {
|
|
try {
|
|
const [poiTypResponse, locationDeviceResponse] = await Promise.all([fetch("/api/talas_v5_DB/poiTyp/readPoiTyp"), fetch("/api/talas5/location_device")]);
|
|
|
|
const poiTypData = await poiTypResponse.json();
|
|
setpoiTypData(poiTypData);
|
|
|
|
const locationDeviceData = await locationDeviceResponse.json();
|
|
setLocationDeviceData(locationDeviceData);
|
|
} catch (error) {
|
|
console.error("Fehler beim Abrufen der Daten:", error);
|
|
}
|
|
};
|
|
|
|
fetchInitialData();
|
|
}, []);
|
|
|
|
const handleSubmit = async (event) => {
|
|
event.preventDefault();
|
|
|
|
// Check for valid poiTypeId
|
|
if (!poiTypeId) {
|
|
alert("Bitte wählen Sie einen Typ aus.");
|
|
return;
|
|
}
|
|
|
|
const formData = {
|
|
name,
|
|
poiTypeId: poiTypeId.value, // Verwende den Wert von react-select
|
|
latitude,
|
|
longitude,
|
|
idLD: locationDeviceData.find((device) => device.name === deviceName?.value).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) {
|
|
setTrigger((trigger) => trigger + 1);
|
|
onClose();
|
|
// Browser aktualisieren
|
|
window.location.reload();
|
|
} else {
|
|
console.error("Fehler beim Hinzufügen des POI");
|
|
}
|
|
|
|
if (map && typeof map.closePopup === "function") {
|
|
map.closePopup();
|
|
}
|
|
};
|
|
|
|
// Erstelle Optionen für react-select
|
|
const poiTypeOptions = poiTypData.map((poiTyp) => ({
|
|
value: poiTyp.idPoiTyp,
|
|
label: poiTyp.name,
|
|
}));
|
|
|
|
const deviceOptions = locationDeviceData.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 (
|
|
<form onSubmit={handleSubmit} className="m-0 p-2 w-full">
|
|
<div className="flex flex-col mb-4">
|
|
{" "}
|
|
{/* Changed to flex-col for vertical alignment */}
|
|
<label htmlFor="name" className="block mb-2 font-bold text-sm text-gray-700">
|
|
Beschreibung :
|
|
</label>
|
|
<input type="text" id="name" name="name" value={name} onChange={(e) => setName(e.target.value)} placeholder="Beschreibung der POI" className="block p-2 w-full border-2 border-gray-200 rounded-md text-sm" />
|
|
</div>
|
|
|
|
{/* React Select for Devices */}
|
|
<div className="flex flex-col mb-4">
|
|
{" "}
|
|
{/* Ensures consistent spacing */}
|
|
<label htmlFor="deviceName" className="block mb-2 font-bold text-sm text-gray-700">
|
|
Gerät :
|
|
</label>
|
|
<Select
|
|
id="deviceName"
|
|
value={deviceName}
|
|
onChange={setDeviceName}
|
|
options={deviceOptions} // Options for filtering
|
|
placeholder="Gerät auswählen..." // Ensure the placeholder is always shown
|
|
isClearable={true} // Allow clearing the selection
|
|
styles={customStyles} // Apply custom styles
|
|
/>
|
|
</div>
|
|
|
|
{/* React Select for POI Types */}
|
|
<div className="flex flex-col mb-4">
|
|
<label htmlFor="idPoiTyp" className="block mb-2 font-bold text-sm text-gray-700">
|
|
Typ:
|
|
</label>
|
|
<Select
|
|
id="idPoiTyp"
|
|
value={poiTypeId}
|
|
onChange={setPoiTypeId}
|
|
options={poiTypeOptions} // Options for filtering
|
|
placeholder="Typ auswählen..."
|
|
styles={customStyles} // Apply custom styles
|
|
/>
|
|
</div>
|
|
|
|
<div className="flex flex-row items-center justify-between mb-4">
|
|
{" "}
|
|
{/* Ensure proper alignment */}
|
|
<div className="flex flex-col items-center">
|
|
<label htmlFor="lat" className="block mb-2 text-xs text-gray-700">
|
|
Lat : {latitude}
|
|
</label>
|
|
</div>
|
|
<div className="flex flex-col items-center">
|
|
<label htmlFor="lng" className="block mb-2 text-xs text-gray-700">
|
|
Lng : {longitude}
|
|
</label>
|
|
</div>
|
|
</div>
|
|
|
|
<button type="submit" className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded w-full">
|
|
POI hinzufügen
|
|
</button>
|
|
</form>
|
|
);
|
|
};
|
|
|
|
export default AddPoiModalWindow;
|