import { useReducer } from 'react'; import { namedLocalStorage } from '../util/PersistentStorage'; import { nextState } from '../util/StateHelper'; const Persistent = (() => { const [getOnline, setOnline] = namedLocalStorage('config.online', true); return { getOnline, setOnline } })(); // <<--- Execute const initialState = { online: Persistent.getOnline() === 'true', intervalMode }; function dispatch(state, action) { switch (action.type) { case 'toggleOnline': return { ...state, online: Persistent.setOnline(!state.online) }; case 'next': return nextState(state, action); default: throw Error('ConfigReducer: unknown action.type', action.type); } } export default function useConfigReducer() { return useReducer(dispatch, initialState); } function intervalMode(interval_sec) { return (this.online === true) ? { interval_sec } // fetch from API every interval_sec : { interval_stop: true } // user has fliped OfflineToggel }