Skip to content

Instantly share code, notes, and snippets.

@dmitryshelomanov
Created December 14, 2018 21:15
Show Gist options
  • Save dmitryshelomanov/17c3ed3f432b8db4ac382628935df200 to your computer and use it in GitHub Desktop.
Save dmitryshelomanov/17c3ed3f432b8db4ac382628935df200 to your computer and use it in GitHub Desktop.
import { fetchStatus } from 'symbiote-fetching'
export const createMultipleFetching = (baseFetchingsState) => ({
start: (state, fetchingId) => ({
...state,
[baseFetchingsState]: {
...state[baseFetchingsState],
[fetchingId]: {
status: fetchStatus.loading,
error: null,
},
},
}),
finish: (state, fetchingId) => ({
...state,
[baseFetchingsState]: {
...state[baseFetchingsState],
[fetchingId]: {
status: fetchStatus.ready,
error: null,
},
},
}),
fail: (state, error, fetchingId) => ({
...state,
...state[baseFetchingsState],
[fetchingId]: {
status: fetchStatus.failed,
error,
},
}),
})
export const handleMultipleFnFetching = (actions, fetchingId, runFn) => (
async (dispatch, getState, extra) => {
try {
dispatch(actions.start(fetchingId))
const result = await runFn(dispatch, getState, extra)
dispatch(actions.finish(fetchingId))
return result
}
catch (error) {
dispatch(actions.fail(fetchingId, error))
return undefined
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment