-
-
Save OliverJAsh/21200a8880e6230dfbec03b62402b09b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// reducer.ts | |
import { Reducer } from 'redux'; | |
import { Action, ActionType } from './actions'; | |
import * as states from './states'; | |
const initialState: states.State = states.form(); | |
export const reducer: Reducer<states.State, Action> = ( | |
state = initialState, | |
action, | |
) => { | |
switch (state.type) { | |
// For each state, we match each valid event and perform the corresponding state transition. | |
case states.StateType.Form: | |
case states.StateType.Failed: | |
case states.StateType.Gallery: | |
switch (action.type) { | |
case ActionType.Search: | |
return states.loading({ query: action.query }); | |
default: | |
return state; | |
} | |
case states.StateType.Loading: { | |
switch (action.type) { | |
case ActionType.SearchFailure: | |
return states.failed(); | |
case ActionType.SearchSuccess: | |
return states.gallery({ items: action.items }); | |
default: | |
return state; | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment