From 1678cd162f87842362646fcdae78ddffafa303bf Mon Sep 17 00:00:00 2001 From: ISA Date: Sun, 22 Dec 2024 07:22:06 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20Fetch-Logik=20f=C3=BCr=20Webservice-Dat?= =?UTF-8?q?en=20in=20useFetchWebServiceMap=20Hook=20ausgelagert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fetch-Logik für GIS-Daten aus MapComponent.js ausgelagert. - Neuer Hook: useFetchWebServiceMap im hooks-Verzeichnis hinzugefügt. - Modularisierung und Wiederverwendbarkeit verbessert. --- components/MapComponent.js | 82 +++++++------------------------- hooks/useFetchWebServiceMap.js | 87 ++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 66 deletions(-) create mode 100644 hooks/useFetchWebServiceMap.js diff --git a/components/MapComponent.js b/components/MapComponent.js index df019b246..a143c787b 100644 --- a/components/MapComponent.js +++ b/components/MapComponent.js @@ -49,6 +49,7 @@ import { useFetchPriorityConfig } from "../hooks/useFetchPriorityConfig"; import { useUpdateGmaData } from "../hooks/useUpdateGmaData"; import { useDynamicMarkerLayers } from "../hooks/useDynamicMarkerLayers"; import { useFetchUserRights } from "../hooks/useFetchUserRights"; +import { useFetchWebServiceMap } from "../hooks/useFetchWebServiceMap"; //-------------------------------------------- import { currentPoiState } from "../redux/slices/currentPoiSlice.js"; import { selectGisStationsStaticDistrict, setGisStationsStaticDistrict } from "../redux/slices/gisStationsStaticDistrictSlice"; @@ -180,11 +181,7 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => { }); const [polylineEventsDisabled, setPolylineEventsDisabled] = useRecoilState(polylineEventsDisabledState); // Recoil State - //--------------------------------------------------------------- - - /* useEffect(() => { - fetchGisStatusStations(12, 484); // Beispielaufruf mit idMap = 10 und idUser = 484 - }, []); */ + //---------------------------------------------------------------############################################ useEffect(() => { const params = new URL(window.location.href).searchParams; @@ -192,20 +189,6 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => { setUserId(params.get("u")); }, [setMapId, setUserId]); - /* useEffect(() => { - if (map && poiLayerRef.current && isPoiTypLoaded && !menuItemAdded && isRightsLoaded) { - //console.log("Überprüfung der Berechtigung vor addItemsToMapContextMenu: ", hasRights); - addItemsToMapContextMenu(hasRights); - } - }, [ - map, - poiLayerRef, - isPoiTypLoaded, - menuItemAdded, // Hinzufügen zu den Abhängigkeiten, um den Effekt korrekt zu steuern - hasRights, // Sicherstellen, dass hasRights berücksichtigt wird - isRightsLoaded, // Überprüfung, ob die Rechte geladen sind - ]); */ - //--------------------------------------------------------------- // Benutzerrechte abrufen und setzen useFetchUserRights(setUserRights, setIsRightsLoaded, setHasRights); @@ -220,7 +203,7 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => { oms, mapLayersVisibility.GMA // Übergebe die Sichtbarkeitsbedingung als Parameter ); - +//--------------------------------------------------------------- /* useSmsfunkmodemMarkersLayer( map, oms, @@ -228,51 +211,18 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => { priorityConfig, mapLayersVisibility.SMSFunkmodem // Sichtbarkeitsstatus aus dem State ); */ - - useEffect(() => { - const fetchWebServiceMap = async () => { - try { - // Zähler für externe API-Aufrufe in localStorage speichern - let requestCount = localStorage.getItem("fetchWebServiceMap") || 0; - requestCount = parseInt(requestCount, 10); - - const fetchOptions = { - method: "GET", - headers: { - Connection: "close", // Keep-Alive-Header hinzufügen - }, - }; - - // Fetch GIS Stations Static District - await fetchGisStationsStaticDistrict(mapGisStationsStaticDistrictUrl, dispatch, fetchOptions); - requestCount++; // Zähler erhöhen - localStorage.setItem("fetchWebServiceMap", requestCount); - //console.log(`fetchWebServiceMap in MapComponent wurde ${requestCount} Mal aufgerufen.`); - - // Fetch GIS Stations Status District - await fetchGisStationsStatusDistrict(mapGisStationsStatusDistrictUrl, setGisStationsStatusDistrict, fetchOptions); - requestCount++; // Zähler erhöhen - localStorage.setItem("fetchWebServiceMap", requestCount); - //console.log(`fetchWebServiceMap in MapComponent wurde ${requestCount} Mal aufgerufen.`); - - // Fetch GIS Stations Measurements - await fetchGisStationsMeasurements(mapGisStationsMeasurementsUrl, setGisStationsMeasurements, fetchOptions); - requestCount++; // Zähler erhöhen - localStorage.setItem("fetchWebServiceMap", requestCount); - //console.log(`fetchWebServiceMap in MapComponent wurde ${requestCount} Mal aufgerufen.`); - - // Fetch GIS System Static - await fetchGisSystemStatic(mapGisSystemStaticUrl, setGisSystemStatic, setGisSystemStaticLoaded, fetchOptions); - requestCount++; // Zähler erhöhen - localStorage.setItem("fetchWebServiceMap", requestCount); - //console.log(`fetchWebServiceMap in MapComponent wurde ${requestCount} Mal aufgerufen.`); - } catch (error) { - console.error("Error fetching data:", error); - } - }; - - fetchWebServiceMap(); - }, [dispatch, mapGisStationsStaticDistrictUrl]); +//--------------------------------------------------------------- + useFetchWebServiceMap( + dispatch, + mapGisStationsStaticDistrictUrl, + mapGisStationsStatusDistrictUrl, + mapGisStationsMeasurementsUrl, + mapGisSystemStaticUrl, + setGisStationsStatusDistrict, + setGisStationsMeasurements, + setGisSystemStatic, + setGisSystemStaticLoaded + ); //-------------------------------------------------------- useDrawLines(setLinePositions); // Linien auf die Karte zeichnen @@ -445,7 +395,7 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => { sonstigeMarkers, tkComponentsMarkers, ulafMarkers, - mapLayersVisibility, + mapLayersVisibility, ]); //-------------------------------------------- diff --git a/hooks/useFetchWebServiceMap.js b/hooks/useFetchWebServiceMap.js new file mode 100644 index 000000000..97265e1b2 --- /dev/null +++ b/hooks/useFetchWebServiceMap.js @@ -0,0 +1,87 @@ +import { useEffect } from "react"; +import { + fetchGisStationsStaticDistrict, + fetchGisStationsStatusDistrict, + fetchGisStationsMeasurements, + fetchGisSystemStatic, +} from "../services/fetchData"; + +export const useFetchWebServiceMap = ( + dispatch, + mapGisStationsStaticDistrictUrl, + mapGisStationsStatusDistrictUrl, + mapGisStationsMeasurementsUrl, + mapGisSystemStaticUrl, + setGisStationsStatusDistrict, + setGisStationsMeasurements, + setGisSystemStatic, + setGisSystemStaticLoaded +) => { + useEffect(() => { + const fetchWebServiceMap = async () => { + try { + // Zähler für externe API-Aufrufe in localStorage speichern + let requestCount = localStorage.getItem("fetchWebServiceMap") || 0; + requestCount = parseInt(requestCount, 10); + + const fetchOptions = { + method: "GET", + headers: { + Connection: "close", + }, + }; + + // Fetch GIS Stations Static District + await fetchGisStationsStaticDistrict( + mapGisStationsStaticDistrictUrl, + dispatch, + fetchOptions + ); + requestCount++; + localStorage.setItem("fetchWebServiceMap", requestCount); + + // Fetch GIS Stations Status District + await fetchGisStationsStatusDistrict( + mapGisStationsStatusDistrictUrl, + setGisStationsStatusDistrict, + fetchOptions + ); + requestCount++; + localStorage.setItem("fetchWebServiceMap", requestCount); + + // Fetch GIS Stations Measurements + await fetchGisStationsMeasurements( + mapGisStationsMeasurementsUrl, + setGisStationsMeasurements, + fetchOptions + ); + requestCount++; + localStorage.setItem("fetchWebServiceMap", requestCount); + + // Fetch GIS System Static + await fetchGisSystemStatic( + mapGisSystemStaticUrl, + setGisSystemStatic, + setGisSystemStaticLoaded, + fetchOptions + ); + requestCount++; + localStorage.setItem("fetchWebServiceMap", requestCount); + } catch (error) { + console.error("Error fetching data:", error); + } + }; + + fetchWebServiceMap(); + }, [ + dispatch, + mapGisStationsStaticDistrictUrl, + mapGisStationsStatusDistrictUrl, + mapGisStationsMeasurementsUrl, + mapGisSystemStaticUrl, + setGisStationsStatusDistrict, + setGisStationsMeasurements, + setGisSystemStatic, + setGisSystemStaticLoaded, + ]); +};