useXxxApi: use PollingReducer as a configuration provider provide pushAPIs, which update respective state with: pushing status push result PollingReducer: rename to configurationReducer move polling indication to it's respective state XxxState.polling = 'true/false' Reviewed-on: http://192.168.8.55:3000/HQLAx/CordaCheckers/pulls/34
44 lines
1023 B
JavaScript
44 lines
1023 B
JavaScript
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
|
|
} |