Last active
July 24, 2018 09:26
-
-
Save artalar/28974d546c8add9648c01f290468ed36 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
const getAsyncType = (type, status) => `${type}_${status}`; | |
export const getMasterActionCreator = type => { | |
const reqType = getAsyncType(type, 'REQUEST'); | |
const respType = getAsyncType(type, 'SUCCESS'); | |
const errType = getAsyncType(type, 'ERROR'); | |
const masterActionCreator = params => ({ params, type }); | |
masterActionCreator.type = type; | |
masterActionCreator.request = params => ({ params, type: reqType }); | |
masterActionCreator.request.type = reqType; | |
masterActionCreator.response = response => ({ response, type: respType }); | |
masterActionCreator.response.type = respType; | |
masterActionCreator.error = error => ({ error, type: errType }); | |
masterActionCreator.error.type = errType; | |
return masterActionCreator; | |
}; | |
// usage | |
const somActionCreator = getMasterActionCreator('SOME_ACTION'); | |
// component | |
dispatch(somActionCreator()) // sync | |
// thunk | |
dispatch(somActionCreator.request()) // async | |
// reducer | |
case somAction.type: // sync | |
case somAction.request.type: // async |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment