diff --git a/webapp/src/App.js b/webapp/src/App.js index 7f1f6ad..04ad760 100644 --- a/webapp/src/App.js +++ b/webapp/src/App.js @@ -7,12 +7,11 @@ import Leaderboard from "./components/Leaderboard" import Game from "./components/Game" import About from "./components/About" -import { pollingReducer, pollingDefaults } from './reducer/polling'; +import Polling from './reducer/polling'; function App() { - const [polling, dispatchPolling] = useReducer(pollingReducer, pollingDefaults) + const [polling, dispatchPolling] = useReducer(Polling.reducer, Polling.initialState) - console.log('polling', polling) return
diff --git a/webapp/src/reducer/polling.js b/webapp/src/reducer/polling.js index a5f343c..89ebf9a 100644 --- a/webapp/src/reducer/polling.js +++ b/webapp/src/reducer/polling.js @@ -1,32 +1,49 @@ -export function pollingReducer(polling, action) { +import { useLocalStorage } from './util/PersistentStorage' + +const Persistent = (() => { + const [getEnabled, setEnabled] = useLocalStorage('polling.enabled', true); + + return { + getEnabled, + setEnabled + } +})(); // <<--- execute + +export const pollingGetInitialState = () => { + return { + enabled: Persistent.getEnabled() === 'true', + + games: false, + leaderboard: false + } +}; + +export function pollingReducer(state, action) { switch (action.type) { - case "toggleOnOff": - return { - ...polling, - enabled: !polling.enabled - }; + case 'toggleOnOff': return { + ...state, + enabled: Persistent.setEnabled(!state.enabled) + }; - case "games": - return { - ...polling, - games: action.value - }; + case 'games': return { + ...state, + games: action.value + }; - case "leaderboard": - return { - ...polling, - leaderboard: action.value - }; + case 'leaderboard': return { + ...state, + leaderboard: action.value + }; default: - throw Error("Unknown action: " + action.type); + throw Error('Unknown action:' + action.type); } } -export const pollingDefaults = { - enabled: true, +const Polling = { + initialState: pollingGetInitialState(), // <<--- execute + reducer: pollingReducer +} - games: false, - leaderboard: false, -}; +export default Polling \ No newline at end of file diff --git a/webapp/src/util/PersistentStorage.js b/webapp/src/util/PersistentStorage.js new file mode 100644 index 0000000..ea44750 --- /dev/null +++ b/webapp/src/util/PersistentStorage.js @@ -0,0 +1,31 @@ +export function useLocalStorage(name, initialValue) { + + const get = () => localStorage.getItem(name); + const del = () => localStorage.removeItem(name); + const set = (value) => { + localStorage.setItem(name, value); + return value; + } + + if (get() === null) { + set(initialValue); + } + + return [get, set, del] +} + +export function useSessionStorage(name, initialValue) { + + const get = () => sessionStorage.getItem(name); + const del = () => sessionStorage.removeItem(name); + const set = (value) => { + sessionStorage.setItem(name, value); + return value; + } + + if (get() === null) { + set(initialValue); + } + + return [get, set, del] +} \ No newline at end of file