useLocalStorage for persistant values from Reducer
This commit is contained in:
parent
b237722e82
commit
ba7f9ce7d1
@ -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 <div className="App">
|
||||
<BrowserRouter>
|
||||
<Header polling={polling} dispatchPolling={dispatchPolling} />
|
||||
|
@ -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
|
31
webapp/src/util/PersistentStorage.js
Normal file
31
webapp/src/util/PersistentStorage.js
Normal file
@ -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]
|
||||
}
|
Loading…
Reference in New Issue
Block a user