Last active
September 19, 2017 01:51
-
-
Save joedski/4634f14d9d1dd0568a542c0ffdd7afa2 to your computer and use it in GitHub Desktop.
hof thunks: resource thunk creator
This file contains hidden or 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 resource(resourceName, mapArgs) { | |
return (params, meta) => async (getState, dispatch) => { | |
const fetchOptions = mapArgs(params, meta); | |
dispatch(resourceActions.requestResource({ | |
name: resourceName, | |
params, | |
meta, | |
})); | |
try { | |
const result = await fetch(fetchOptions.uri, fetchOptions.options); | |
dispatch(resourceActions.receiveResource({ | |
name: resourceName, | |
params, | |
meta, | |
resource: result, | |
})); | |
return { result }; | |
} | |
catch (error) { | |
dispatch(resourceActions.rejectResource({ | |
name: resourceName, | |
params, | |
meta, | |
error, | |
})); | |
return { error }; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment