Skip to content

Instantly share code, notes, and snippets.

@zzdjk6
Last active November 21, 2019 08:41
Show Gist options
  • Save zzdjk6/995f2332bd2db7c9c10ecaf567984682 to your computer and use it in GitHub Desktop.
Save zzdjk6/995f2332bd2db7c9c10ecaf567984682 to your computer and use it in GitHub Desktop.
Listing 1: Loading Reducer
// Reducer
export default (state: LoadingState = {}, action: Action<any>): LoadingState => {
const { type } = action;
const matches = /(.*)\/(REQUEST|SUCCESS|FAILURE)/.exec(type);
// Ignore non-routine actions:
// A routine action should have one of three suffixes:
// ['/REQUEST', '/SUCCESS', '/FAILURE']
if (!matches) return state;
const [, routineType, status] = matches;
return {
...state,
// Set loading state to true only when the status is "REQUEST"
// Otherwise set the loading state to false
[routineType]: status === 'REQUEST'
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment