Created
December 1, 2017 15:59
-
-
Save sfrdmn/261e7e5c21e5f0c60c28da318997af75 to your computer and use it in GitHub Desktop.
mcr_post_07
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
// 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