#24-directory-structure #26
@ -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);
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user