Skip to content

Commit

Permalink
frontend: persist relevant state in localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
globin committed Sep 24, 2023
1 parent 5750b07 commit 351588b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 27 deletions.
12 changes: 12 additions & 0 deletions atciss-frontend/src/app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,15 @@ export const zn = (n: number) => (x: number | string) =>
export const z2 = zn(2)
export const z3 = zn(3)
export const z4 = zn(4)

export function localStorageOrDefault<T>(item: string, fallback: T): T {
const localStorageValue = localStorage.getItem(item)

return localStorageValue ? JSON.parse(localStorageValue) : fallback
}

export function setLocalStorage<T>(item: string, value: T): T {
localStorage.setItem(item, JSON.stringify(value))

return value
}
36 changes: 26 additions & 10 deletions atciss-frontend/src/services/activePositionSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PayloadAction, createSelector, createSlice } from "@reduxjs/toolkit"
import { RootState } from "../app/store"
import { Position, SectorData, sectorApi } from "./airspaceApi"
import { Controller, controllerApi } from "./controllerApi"
import { localStorageOrDefault, setLocalStorage } from "../app/utils"

type PositionStatus = {
position: Position
Expand All @@ -20,19 +21,28 @@ const findSelectedPosition = (
positions: Positions,
syncedToOnline: boolean,
): string | null =>
previousSelected &&
positions[previousSelected][syncedToOnline ? "online" : "manual"]
? previousSelected
: Object.entries(positions).find(
([_, p]) => p[syncedToOnline ? "online" : "manual"],
)?.[0] ?? null
setLocalStorage(
"activePositions.selectedPosition",
previousSelected &&
positions[previousSelected][syncedToOnline ? "online" : "manual"]
? previousSelected
: Object.entries(positions).find(
([_, p]) => p[syncedToOnline ? "online" : "manual"],
)?.[0] ?? null,
)

const activePositionSlice = createSlice({
name: "activePositions",
initialState: {
positions: {},
selectedPosition: null,
syncedToOnline: true,
selectedPosition: localStorageOrDefault(
"activePositions.selectedPosition",
null,
),
syncedToOnline: localStorageOrDefault(
"activePositions.syncedToOnline",
true,
),
} as ActivePositionState,
reducers: {
setPosition(
Expand All @@ -49,15 +59,21 @@ const activePositionSlice = createSlice({
)
},
setSyncedToOnline(state, { payload: synced }: PayloadAction<boolean>) {
state.syncedToOnline = synced
state.syncedToOnline = setLocalStorage(
"activePositions.syncedToOnline",
synced,
)
state.selectedPosition = findSelectedPosition(
state.selectedPosition,
state.positions,
state.syncedToOnline,
)
},
setSelectedPosition(state, { payload: pos }: PayloadAction<string>) {
state.selectedPosition = pos
state.selectedPosition = setLocalStorage(
"activePositions.selectedPosition",
pos,
)
},
enableAllPositions(state) {
const positions = Object.keys(state.positions).reduce(
Expand Down
11 changes: 6 additions & 5 deletions atciss-frontend/src/services/configSlice.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
import { RootState } from "../app/store"
import { EBG_SETTINGS } from "../app/config"
import { localStorageOrDefault, setLocalStorage } from "../app/utils"

type ActivePositionState = {
activeEbg: string
Expand All @@ -9,14 +10,14 @@ type ActivePositionState = {
const configSlice = createSlice({
name: "config",
initialState: {
activeEbg: Object.keys(EBG_SETTINGS).shift(),
activeEbg: localStorageOrDefault(
"config.activeEbg",
Object.keys(EBG_SETTINGS).shift(),
),
} as ActivePositionState,
reducers: {
setActiveEbg(state, { payload }: PayloadAction<string>) {
return {
...state,
activeEbg: payload,
}
state.activeEbg = setLocalStorage("config.activeEbg", payload)
},
},
})
Expand Down
25 changes: 13 additions & 12 deletions atciss-frontend/src/services/mapSlice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
import { RootState } from "../app/store"
import { localStorageOrDefault, setLocalStorage } from "../app/utils"

type MapState = {
level: number
Expand All @@ -12,29 +13,29 @@ type MapState = {
const mapSlice = createSlice({
name: "map",
initialState: {
level: 200,
ofm: true,
dfs: false,
dwd: false,
sectors: true,
level: localStorageOrDefault("map.level", 200),
ofm: localStorageOrDefault("map.ofm", true),
dfs: localStorageOrDefault("map.dfs", false),
dwd: localStorageOrDefault("map.dwd", false),
sectors: localStorageOrDefault("map.sectors", true),
} as MapState,
reducers: {
setLevel(state, { payload: level }: PayloadAction<number>) {
state.level = level
state.level = setLocalStorage("map.level", level)
},
setOpenFlightmaps(state, { payload: active }: PayloadAction<boolean>) {
state.ofm = active
state.dfs = state.dfs && !active
state.ofm = setLocalStorage("map.ofm", active)
state.dfs = setLocalStorage("map.dfs", state.dfs && !active)
},
setDFS(state, { payload: active }: PayloadAction<boolean>) {
state.dfs = active
state.ofm = state.ofm && !active
state.dfs = setLocalStorage("map.dfs", active)
state.ofm = setLocalStorage("map.ofm", state.ofm && !active)
},
setDWD(state, { payload: active }: PayloadAction<boolean>) {
state.dwd = active
state.dwd = setLocalStorage("map.dwd", active)
},
setSectors(state, { payload: active }: PayloadAction<boolean>) {
state.sectors = active
state.sectors = setLocalStorage("map.sectors", active)
},
},
})
Expand Down

0 comments on commit 351588b

Please sign in to comment.