feat: Fetch-Logik für Benutzerrechte in useFetchUserRights Hook ausgelagert
- Fetch-Logik für Benutzerrechte aus MapComponent.js in einen separaten Hook ausgelagert. - Neuer Hook: useFetchUserRights im hooks-Verzeichnis hinzugefügt. - Verbesserung der Modularität und Wiederverwendbarkeit.
This commit is contained in:
@@ -48,6 +48,7 @@ import { useFetchLineStatusData } from "../hooks/useFetchLineStatusData";
|
|||||||
import { useFetchPriorityConfig } from "../hooks/useFetchPriorityConfig";
|
import { useFetchPriorityConfig } from "../hooks/useFetchPriorityConfig";
|
||||||
import { useUpdateGmaData } from "../hooks/useUpdateGmaData";
|
import { useUpdateGmaData } from "../hooks/useUpdateGmaData";
|
||||||
import { useDynamicMarkerLayers } from "../hooks/useDynamicMarkerLayers";
|
import { useDynamicMarkerLayers } from "../hooks/useDynamicMarkerLayers";
|
||||||
|
import { useFetchUserRights } from "../hooks/useFetchUserRights";
|
||||||
//--------------------------------------------
|
//--------------------------------------------
|
||||||
import { currentPoiState } from "../redux/slices/currentPoiSlice.js";
|
import { currentPoiState } from "../redux/slices/currentPoiSlice.js";
|
||||||
import { selectGisStationsStaticDistrict, setGisStationsStaticDistrict } from "../redux/slices/gisStationsStaticDistrictSlice";
|
import { selectGisStationsStaticDistrict, setGisStationsStaticDistrict } from "../redux/slices/gisStationsStaticDistrictSlice";
|
||||||
@@ -205,18 +206,11 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => {
|
|||||||
isRightsLoaded, // Überprüfung, ob die Rechte geladen sind
|
isRightsLoaded, // Überprüfung, ob die Rechte geladen sind
|
||||||
]); */
|
]); */
|
||||||
|
|
||||||
useEffect(() => {
|
//---------------------------------------------------------------
|
||||||
const fetchAndSetUserRights = async () => {
|
// Benutzerrechte abrufen und setzen
|
||||||
const rights = await fetchUserRights();
|
useFetchUserRights(setUserRights, setIsRightsLoaded, setHasRights);
|
||||||
setUserRights(rights);
|
|
||||||
setIsRightsLoaded(true);
|
|
||||||
|
|
||||||
// Sicherstellen, dass `rights` ein Array ist, bevor `.includes()` aufgerufen wird
|
//---------------------------------------------------------------
|
||||||
setHasRights(localStorage.getItem("editMode") && Array.isArray(rights) && rights.includes(56));
|
|
||||||
};
|
|
||||||
|
|
||||||
fetchAndSetUserRights();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
useGmaMarkersLayer(
|
useGmaMarkersLayer(
|
||||||
map,
|
map,
|
||||||
@@ -451,7 +445,7 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => {
|
|||||||
sonstigeMarkers,
|
sonstigeMarkers,
|
||||||
tkComponentsMarkers,
|
tkComponentsMarkers,
|
||||||
ulafMarkers,
|
ulafMarkers,
|
||||||
mapLayersVisibility, // Neu: Abhängigkeit für Sichtbarkeitsstatus
|
mapLayersVisibility,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
//--------------------------------------------
|
//--------------------------------------------
|
||||||
|
|||||||
29
hooks/useFetchUserRights.js
Normal file
29
hooks/useFetchUserRights.js
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { useEffect } from "react";
|
||||||
|
import { fetchUserRights } from "../services/apiService";
|
||||||
|
|
||||||
|
export const useFetchUserRights = (
|
||||||
|
setUserRights,
|
||||||
|
setIsRightsLoaded,
|
||||||
|
setHasRights
|
||||||
|
) => {
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchAndSetUserRights = async () => {
|
||||||
|
try {
|
||||||
|
const rights = await fetchUserRights();
|
||||||
|
setUserRights(rights);
|
||||||
|
setIsRightsLoaded(true);
|
||||||
|
|
||||||
|
// Sicherstellen, dass `rights` ein Array ist, bevor `.includes()` aufgerufen wird
|
||||||
|
setHasRights(
|
||||||
|
localStorage.getItem("editMode") &&
|
||||||
|
Array.isArray(rights) &&
|
||||||
|
rights.includes(56)
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Fehler beim Abrufen der Benutzerrechte:", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchAndSetUserRights();
|
||||||
|
}, [setUserRights, setIsRightsLoaded, setHasRights]);
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user