diff --git a/webapp/src/App.js b/webapp/src/App.js index 0b8f5a3..3aa63ee 100644 --- a/webapp/src/App.js +++ b/webapp/src/App.js @@ -13,7 +13,7 @@ import useLeaderboardApi from './api/leaderboard'; import useUserApi from './api/user'; function App() { - const pollingFlux = useReducer(Polling.reducer, Polling.restoreState); + const pollingFlux = useReducer(Polling.reducer, Polling.initialState); const userFlux = useReducer(User.reducer, User.initialState); const leaderboardApi = useLeaderboardApi(pollingFlux); diff --git a/webapp/src/api/leaderboard.js b/webapp/src/api/leaderboard.js index 5b2e565..d32b4b6 100644 --- a/webapp/src/api/leaderboard.js +++ b/webapp/src/api/leaderboard.js @@ -12,7 +12,7 @@ export default function useLeaderboardApi([polling, dispatchPolling]) { const [leaderboard, isFetching] = usePolling(uri, mode); if (polling.leaderboard !== isFetching) { - dispatchPolling({ type: 'setLeaderboard', value: isFetching }); + dispatchPolling({ type: 'next', leaderboard: isFetching }); } return leaderboard; diff --git a/webapp/src/flux/polling.js b/webapp/src/flux/polling.js index 2f8cbd8..c4ce599 100644 --- a/webapp/src/flux/polling.js +++ b/webapp/src/flux/polling.js @@ -1,4 +1,5 @@ import { useLocalStorage } from '../util/PersistentStorage' +import { nextState } from '../util/StateHelper'; const Persistent = (() => { const [getEnabled, setEnabled] = useLocalStorage('polling.enabled', true); @@ -9,36 +10,32 @@ const Persistent = (() => { } })(); // <<--- Execute -const restoreState = { - enabled: Persistent.getEnabled() === 'true', +export const pollingInitialState = { + enabled: Persistent.getEnabled() === 'true', - games: false, - leaderboard: false + games: false, + leaderboard: false }; -function reducer(state, action) { +export function pollingReducer(curntState, action) { switch (action.type) { case 'toggleOnOff': return { - ...state, - enabled: Persistent.setEnabled(!state.enabled) + ...curntState, + enabled: Persistent.setEnabled(!curntState.enabled) }; - case 'setGames': return { - ...state, - games: action.value - }; - - case 'setLeaderboard': return { - ...state, - leaderboard: action.value - }; + case 'next': + return nextState(curntState, action); default: throw Error('Unknown action.type:' + action.type); } } -const Polling = { reducer, restoreState } +const Polling = { + reducer: pollingReducer, + initialState: pollingInitialState +}; export default Polling \ No newline at end of file