62 lines
2.1 KiB
JavaScript
62 lines
2.1 KiB
JavaScript
// redux/slices/mapLayersSlice.js
|
|
import { createSlice } from "@reduxjs/toolkit";
|
|
|
|
const initialState = {};
|
|
|
|
const mapLayersSlice = createSlice({
|
|
name: "mapLayers",
|
|
initialState,
|
|
reducers: {
|
|
toggleLayer: (state, action) => {
|
|
const layer = action.payload;
|
|
if (state[layer] !== undefined) {
|
|
state[layer] = !state[layer]; // Toggle Sichtbarkeit
|
|
}
|
|
},
|
|
setLayerVisibility: (state, action) => {
|
|
const { layer, visibility } = action.payload;
|
|
state[layer] = visibility; // Entferne die Bedingung, um sicherzustellen, dass Werte gesetzt werden
|
|
},
|
|
setInitialLayers: (state, action) => {
|
|
const systems = action.payload; // Array of GisSystem
|
|
|
|
// Versuche kartenspezifische localStorage-Werte zu laden
|
|
const mapId =
|
|
typeof localStorage !== "undefined" ? localStorage.getItem("currentMapId") : null;
|
|
const userId =
|
|
typeof localStorage !== "undefined" ? localStorage.getItem("currentUserId") : null;
|
|
const mapStorageKey =
|
|
mapId && userId ? `mapLayersVisibility_m${mapId}_u${userId}` : "mapLayersVisibility";
|
|
|
|
let existingVisibility = {};
|
|
if (typeof localStorage !== "undefined") {
|
|
try {
|
|
const stored = localStorage.getItem(mapStorageKey);
|
|
if (stored) {
|
|
existingVisibility = JSON.parse(stored);
|
|
}
|
|
} catch (error) {
|
|
console.error("Error loading stored visibility:", error);
|
|
}
|
|
}
|
|
|
|
systems.forEach(system => {
|
|
const key = `system-${system.IdSystem}`;
|
|
|
|
// Prüfe ob bereits ein localStorage-Wert existiert
|
|
if (existingVisibility.hasOwnProperty(key)) {
|
|
// Verwende gespeicherten Wert (Benutzer-Einstellung hat Priorität)
|
|
state[key] = existingVisibility[key];
|
|
} else {
|
|
// Verwende Allow-Wert als Standard (nur für neue Systeme)
|
|
state[key] = system.Allow === 1 && system.Map === 1;
|
|
}
|
|
});
|
|
},
|
|
},
|
|
});
|
|
|
|
export const { toggleLayer, setLayerVisibility, setInitialLayers } = mapLayersSlice.actions;
|
|
export const selectMapLayersState = state => state.mapLayers || initialState;
|
|
export default mapLayersSlice.reducer;
|