Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sfrdmn
Created December 1, 2017 15:59
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 sfrdmn/261e7e5c21e5f0c60c28da318997af75 to your computer and use it in GitHub Desktop.
Save sfrdmn/261e7e5c21e5f0c60c28da318997af75 to your computer and use it in GitHub Desktop.
mcr_post_07
// Similar to the higher-order action creator, we accept a mapping of async
// states and action types, except this time, for convenience, in reverse.
// We then yield a reducer
const loadStateReducer = (asyncStates) => {
return (state = { loading: false, loaded: false, data: null }, action) {
const asyncState = asyncStates[action.type]
switch (asyncState) {
case 'pending':
return {
loading: true,
loaded: false,
data: state.data
}
case 'error':
return {
loading: false,
loaded: false,
error: action.payload,
data: state.data
}
case 'complete':
return {
loading: false,
loaded: true,
data: action.payload
}
default:
return state
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment