Skip to content

Instantly share code, notes, and snippets.

@whichsteveyp
Last active October 25, 2017 23:40
Show Gist options
  • Save whichsteveyp/3985d85de334e98852f8c1de332afb17 to your computer and use it in GitHub Desktop.
Save whichsteveyp/3985d85de334e98852f8c1de332afb17 to your computer and use it in GitHub Desktop.
initial API implementation WIP for action creators
import actionTypes from 'redux-resource';
const createActionCreators = (options = {}) => {
const { crudType, resourceName } = options;
const uppercaseCrud = crudType ? crudType.toUpperCase() : '';
const isValidCrudType =
uppercaseCrud === 'UPDATE' || uppercaseCrud === 'DELETE' ||
uppercaseCrud === 'READ' || uppercaseCrud === 'CREATE';
if (!isValidCrudType) {
throw new Error('hey wtf it needs to be a thing');
}
if (!resourceName) {
throw new Error('hey wtf it needs to be a thing');
}
return {
pending: (options) => ({
...options,
resourceName,
type: actionTypes[`${uppercaseCrud}_RESOURCES_PENDING`]
}),
null: (options) => ({
...options,
resourceName,
type: actionTypes[`${uppercaseCrud}_RESOURCES_NULL`]
}),
succeeded: (resources, options) => ({
...options,
resourceName,
resources,
type: actionTypes[`${uppercaseCrud}_RESOURCES_SUCCEEDED`]
}),
failed: (error, options) => ({
...options,
resourceName,
error,
type: actionTypes[`${uppercaseCrud}_RESOURCES_FAILED`]
}),
}
}
export default createActionCreators;
import createActionCreators from 'redux-resource-action-creators';
const readBookActionCreators = createActionCreators({
resourceName: 'books',
crudType: 'read'
});
const readBooks = options => dispatch => {
dispatch(readBookActionCreators.pending(options);
return fetch('/my/server/books')
.then(res => dispatch(
readBookActionCreators.succeeded(res.body, options)
))
.catch(err => dispatch(
readBookActionCreators.failed(err, options)
))
};
const readBooksReset = options => readBookActionCreators.null(options);
export readBooks;
export readBooksReset;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment