Persist slice of redux state in local storage

This commit is contained in:
Peizhi Zheng
2021-01-14 20:46:41 -08:00
committed by GitHub
parent b1398742b9
commit 1df5004203
7 changed files with 61 additions and 24 deletions

24
ui/src/localStorage.ts Normal file
View File

@@ -0,0 +1,24 @@
import { AppState } from "./store";
const LOCAL_STORAGE_KEY = "asynqmon:state";
export function loadState(): AppState | undefined {
try {
const serializedState = localStorage.getItem(LOCAL_STORAGE_KEY);
if (serializedState === null) {
return undefined;
}
return JSON.parse(serializedState);
} catch (err) {
return undefined;
}
}
export function saveState(state: AppState) {
try {
const serializedState = JSON.stringify({ settings: state.settings });
localStorage.setItem(LOCAL_STORAGE_KEY, serializedState);
} catch (err) {
console.error("saveState: could not save state: ", err);
}
}