Last active
September 1, 2015 12:36
-
-
Save justinobney/48c70247dc90b8376f0a 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
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