Skip to content

Instantly share code, notes, and snippets.

@OliverJAsh
Created September 10, 2018 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OliverJAsh/21200a8880e6230dfbec03b62402b09b to your computer and use it in GitHub Desktop.
Save OliverJAsh/21200a8880e6230dfbec03b62402b09b to your computer and use it in GitHub Desktop.
// 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