Skip to content

Instantly share code, notes, and snippets.

@justinobney
Last active September 1, 2015 12:36
Show Gist options
  • Save justinobney/48c70247dc90b8376f0a to your computer and use it in GitHub Desktop.
Save justinobney/48c70247dc90b8376f0a to your computer and use it in GitHub Desktop.
export function fetchActionCreator(config = {}) {
if (!config.url || !config.type) {
throw new Error('Invalid fetchActionCreator settings');
}
let beginAction = config.beginAction || `${config.type}_BEGIN`;
let successAction = config.successAction || `${config.type}_SUCCESS`;
let errorAction = config.errorAction || `${config.type}_ERROR`;
let settings = config.fetchSettings || {};
return (settingsOveride) => {
Object.assign(settings, settingsOveride);
return (dispatch, getState) => {
dispatch({type: beginAction});
fetch(config.url, settings)
.then(resp => resp.json())
.then(onSuccess, onError);
function onSuccess(json) {
dispatch({type: successAction, payload: json});
}
function onError(error) {
dispatch({type: errorAction, error});
}
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment