Poi Aktualisierung done!

This commit is contained in:
ISA
2024-05-22 10:01:27 +02:00
parent 3f49ead2fd
commit 3bd491e825

View File

@@ -1,15 +1,41 @@
// pages/api/poiUpdateModal.js
import React, { useState, useEffect } from "react"; import React, { useState, useEffect } from "react";
const PoiUpdateModal = ({ onClose, poiData }) => { const PoiUpdateModal = ({ onClose, poiData }) => {
const [poiId, setPoiId] = useState(poiData ? poiData.idPoi : ""); const [poiId, setPoiId] = useState(poiData ? poiData.idPoi : "");
const [description, setDescription] = useState( const [name, setName] = useState(poiData ? poiData.name : "");
poiData ? poiData.description : ""
);
const [poiTypData, setPoiTypData] = useState([]); const [poiTypData, setPoiTypData] = useState([]);
const [poiTypeId, setPoiTypeId] = useState(""); const [poiTypeId, setPoiTypeId] = useState("");
const [locationDeviceData, setLocationDeviceData] = useState([]); const [locationDeviceData, setLocationDeviceData] = useState([]);
const [deviceName, setDeviceName] = useState(""); const [deviceName, setDeviceName] = useState("");
const [description, setDescription] = useState(
poiData ? poiData.description : ""
);
// Function to handle deleting a POI
const handleDeletePoi = async () => {
if (confirm("Sind Sie sicher, dass Sie diesen POI löschen möchten?")) {
try {
const response = await fetch(`/api/deletePoi?id=${poiId}`, {
method: "DELETE",
});
if (response.ok) {
alert("POI wurde erfolgreich gelöscht.");
onClose(); // Close the modal
//Browser neu laden, um die aktualisierte Liste anzuzeigen
window.location.reload();
} else {
throw new Error("Fehler beim Löschen des POI.");
}
} catch (error) {
console.error("Fehler beim Löschen des POI:", error);
alert("Fehler beim Löschen des POI.");
}
}
};
// Fetch POI types
useEffect(() => { useEffect(() => {
const fetchPoiTypData = async () => { const fetchPoiTypData = async () => {
try { try {
@@ -24,7 +50,10 @@ const PoiUpdateModal = ({ onClose, poiData }) => {
} }
}; };
fetchPoiTypData(); fetchPoiTypData();
}, []);
// Fetch device data
useEffect(() => {
const fetchData = async () => { const fetchData = async () => {
try { try {
const response = await fetch("/api/talas_v5/location_device"); const response = await fetch("/api/talas_v5/location_device");
@@ -43,6 +72,7 @@ const PoiUpdateModal = ({ onClose, poiData }) => {
fetchData(); fetchData();
}, []); }, []);
// Form submission handler
const handleSubmit = async (event) => { const handleSubmit = async (event) => {
event.preventDefault(); event.preventDefault();
const idLDResponse = await fetch( const idLDResponse = await fetch(
@@ -50,7 +80,6 @@ const PoiUpdateModal = ({ onClose, poiData }) => {
); );
const idLDData = await idLDResponse.json(); const idLDData = await idLDResponse.json();
const idLD = idLDData.idLD; const idLD = idLDData.idLD;
try { try {
const response = await fetch("/api/updatePoi", { const response = await fetch("/api/updatePoi", {
method: "POST", method: "POST",
@@ -59,7 +88,7 @@ const PoiUpdateModal = ({ onClose, poiData }) => {
}, },
body: JSON.stringify({ body: JSON.stringify({
idPoi: poiId, idPoi: poiId,
description: description, description: name,
idPoiTyp: poiTypeId, idPoiTyp: poiTypeId,
idLD: idLD, idLD: idLD,
}), }),
@@ -83,16 +112,16 @@ const PoiUpdateModal = ({ onClose, poiData }) => {
return ( return (
<form onSubmit={handleSubmit} className="m-0 p-2 w-full"> <form onSubmit={handleSubmit} className="m-0 p-2 w-full">
<div className="flex items-center mb-4"> <div className="flex items-center mb-4">
<label htmlFor="description" className="block mr-2 flex-none"> <label htmlFor="name" className="block mr-2 flex-none">
Beschreibung: Name:
</label> </label>
<input <input
type="text" type="text"
id="description" id="name"
name="description" name="name"
value={description} value={name}
onChange={(e) => setDescription(e.target.value)} onChange={(e) => setName(e.target.value)}
placeholder="Beschreibung der Station" placeholder="Name der Station"
className="block p-2 w-full border-2 border-gray-200 rounded-md text-sm" className="block p-2 w-full border-2 border-gray-200 rounded-md text-sm"
/> />
</div> </div>
@@ -135,6 +164,14 @@ const PoiUpdateModal = ({ onClose, poiData }) => {
</select> </select>
</div> </div>
<button
type="button" // Use button type to prevent form submission
onClick={handleDeletePoi}
className="bg-red-400 hover:bg-red-600 text-white font-bold py-2 px-4 rounded w-full mb-4"
>
POI löschen
</button>
<button <button
type="submit" type="submit"
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded w-full" className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded w-full"