149 lines
4.4 KiB
JavaScript
149 lines
4.4 KiB
JavaScript
import React, { useState, useEffect } from "react";
|
|
|
|
const PoiUpdateModal = ({ onClose, poiData }) => {
|
|
const [poiId, setPoiId] = useState(poiData ? poiData.idPoi : "");
|
|
const [description, setDescription] = useState(
|
|
poiData ? poiData.description : ""
|
|
);
|
|
const [poiTypData, setPoiTypData] = useState([]);
|
|
const [poiTypeId, setPoiTypeId] = useState("");
|
|
const [locationDeviceData, setLocationDeviceData] = useState([]);
|
|
const [deviceName, setDeviceName] = useState("");
|
|
|
|
useEffect(() => {
|
|
const fetchPoiTypData = async () => {
|
|
try {
|
|
const response = await fetch("/api/readPoiTyp");
|
|
const data = await response.json();
|
|
setPoiTypData(data);
|
|
if (data && data.length > 0) {
|
|
setPoiTypeId(data[0].idPoiTyp); // Set the first type as default
|
|
}
|
|
} catch (error) {
|
|
console.error("Fehler beim Abrufen der poiTyp Daten:", error);
|
|
}
|
|
};
|
|
fetchPoiTypData();
|
|
|
|
const fetchData = async () => {
|
|
try {
|
|
const response = await fetch("/api/talas_v5/location_device");
|
|
const data = await response.json();
|
|
setLocationDeviceData(data);
|
|
if (data.length > 0) {
|
|
setDeviceName(data[0].name); // Set initial device name
|
|
}
|
|
} catch (error) {
|
|
console.error(
|
|
"Fehler beim Abrufen der Standort- und Gerätedaten:",
|
|
error
|
|
);
|
|
}
|
|
};
|
|
fetchData();
|
|
}, []);
|
|
|
|
const handleSubmit = async (event) => {
|
|
event.preventDefault();
|
|
const idLDResponse = await fetch(
|
|
`/api/getDeviceId?deviceName=${encodeURIComponent(deviceName)}`
|
|
);
|
|
const idLDData = await idLDResponse.json();
|
|
const idLD = idLDData.idLD;
|
|
|
|
try {
|
|
const response = await fetch("/api/updatePoi", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
idPoi: poiId,
|
|
description: description,
|
|
idPoiTyp: poiTypeId,
|
|
idLD: idLD,
|
|
}),
|
|
});
|
|
|
|
if (response.ok) {
|
|
alert("POI wurde erfolgreich aktualisiert.");
|
|
onClose(); // Schließen des Modals und Aktualisieren der Ansicht
|
|
} else {
|
|
const errorResponse = await response.json();
|
|
throw new Error(
|
|
errorResponse.error || "Fehler beim Aktualisieren des POI."
|
|
);
|
|
}
|
|
} catch (error) {
|
|
console.error("Fehler beim Aktualisieren des POI:", error);
|
|
alert("Fehler beim Aktualisieren des POI.");
|
|
}
|
|
};
|
|
|
|
return (
|
|
<form onSubmit={handleSubmit} className="m-0 p-2 w-full">
|
|
<div className="flex items-center mb-4">
|
|
<label htmlFor="description" className="block mr-2 flex-none">
|
|
Beschreibung:
|
|
</label>
|
|
<input
|
|
type="text"
|
|
id="description"
|
|
name="description"
|
|
value={description}
|
|
onChange={(e) => setDescription(e.target.value)}
|
|
placeholder="Beschreibung der Station"
|
|
className="block p-2 w-full border-2 border-gray-200 rounded-md text-sm"
|
|
/>
|
|
</div>
|
|
|
|
<div className="flex items-center mb-4">
|
|
<label htmlFor="deviceName" className="block mr-2 flex-none">
|
|
Gerät:
|
|
</label>
|
|
<select
|
|
id="deviceName"
|
|
name="deviceName"
|
|
value={deviceName}
|
|
onChange={(e) => setDeviceName(e.target.value)}
|
|
className="block p-2 w-full border-2 border-gray-200 rounded-md text-sm"
|
|
>
|
|
{locationDeviceData.map((device, index) => (
|
|
<option key={index} value={device.name}>
|
|
{device.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</div>
|
|
|
|
<div className="flex items-center mb-4">
|
|
<label htmlFor="idPoiTyp2" className="block mr-2 flex-none">
|
|
Typ:
|
|
</label>
|
|
<select
|
|
id="idPoiTyp2"
|
|
name="idPoiTyp2"
|
|
value={poiTypeId}
|
|
onChange={(e) => setPoiTypeId(e.target.value)}
|
|
className="block p-2 w-full border-2 border-gray-200 rounded-md text-sm"
|
|
>
|
|
{poiTypData.map((poiTyp, index) => (
|
|
<option key={index} value={poiTyp.idPoiTyp}>
|
|
{poiTyp.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</div>
|
|
|
|
<button
|
|
type="submit"
|
|
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded w-full"
|
|
>
|
|
POI aktualisieren
|
|
</button>
|
|
</form>
|
|
);
|
|
};
|
|
|
|
export default PoiUpdateModal;
|