Skip to content

Instantly share code, notes, and snippets.

@artalar
Last active July 24, 2018 09:26
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 artalar/28974d546c8add9648c01f290468ed36 to your computer and use it in GitHub Desktop.
Save artalar/28974d546c8add9648c01f290468ed36 to your computer and use it in GitHub Desktop.
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