refactor: ausgelagert die Linien-Zeichnungslogik in einen separaten Hook
- Die Logik zum Abrufen und Zeichnen von Linien wurde in den neuen Hook `useDrawLines` ausgelagert. - Dies verbessert die Übersichtlichkeit in `MapComponent.js` und ermöglicht eine einfachere Wiederverwendung und Wartung. - Der neue Hook befindet sich im Verzeichnis `hooks/layers/`.
This commit is contained in:
37
hooks/layers/useDrawLines.js
Normal file
37
hooks/layers/useDrawLines.js
Normal file
@@ -0,0 +1,37 @@
|
||||
import { useEffect } from "react";
|
||||
const useDrawLines = (setLinePositions) => {
|
||||
// Linien auf die Karte zeichnen
|
||||
useEffect(() => {
|
||||
const endpoint = "/api/talas_v5_DB/gisLines/readGisLines";
|
||||
//const endpoint = "http://localhost/talas5/ClientData/WebServiceMap.asmx/GisLinesStatus?idMap=10";
|
||||
fetch(endpoint)
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then((data) => {
|
||||
const newLinePositions = data.map((item) => {
|
||||
//console.log("item.idLD", item.idLD);
|
||||
//console.log("item.idModul", item.idModul);
|
||||
|
||||
if (item.points && Array.isArray(item.points)) {
|
||||
return {
|
||||
coordinates: item.points.map((point) => [point.x, point.y]),
|
||||
idModul: item.idModul,
|
||||
idLD: item.idLD,
|
||||
};
|
||||
} else {
|
||||
throw new Error("Points missing or not an array");
|
||||
}
|
||||
});
|
||||
setLinePositions(newLinePositions);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error fetching data:", error.message);
|
||||
});
|
||||
}, []);
|
||||
};
|
||||
|
||||
export default useDrawLines;
|
||||
Reference in New Issue
Block a user