Created
December 14, 2018 21:15
-
-
Save dmitryshelomanov/17c3ed3f432b8db4ac382628935df200 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
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