corda-checkers/webapp/src/reducer/polling.js
2023-11-08 09:23:20 +01:00

39 lines
892 B
JavaScript

import { useReducer } from 'react';
import { useLocalStorage } from '../util/PersistentStorage';
import { nextState } from '../util/StateHelper';
const Persistent = (() => {
const [getEnabled, setEnabled] = useLocalStorage('polling.enabled', true);
return {
getEnabled,
setEnabled
}
})(); // <<--- Execute
export const pollingInitialState = {
enabled: Persistent.getEnabled() === 'true',
games: false,
leaderboard: false
};
export function pollingReducer(curntState, action) {
switch (action.type) {
case 'toggleOnOff': return {
...curntState,
enabled: Persistent.setEnabled(!curntState.enabled)
};
case 'next':
return nextState(curntState, action);
default:
throw Error('Unknown action.type:' + action.type);
}
}
export default function usePollingReducer() {
return useReducer(pollingReducer, pollingInitialState);
}