Last active
March 18, 2021 01:57
-
-
Save andriangungon/218bdecbb79f3ce0c3b5001cde349e70 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
const loadingReducer = (state = {}, action) => { | |
const { type } = action; | |
const matches = /(.*)_(START|SUCCESS|FAIL)/.exec(type); | |
// not a *_START / *_SUCCESS / *_FAILURE actions, so we ignore them | |
if (!matches) return state; | |
const [, requestName, requestState] = matches; | |
return { | |
...state, | |
// Store whether a request is happening at the moment or not | |
// e.g. will be true when receiving GET_TODOS_REQUEST | |
// and false when receiving GET_TODOS_SUCCESS / GET_TODOS_FAILURE | |
[requestName]: requestState === 'START', | |
}; | |
}; | |
export default loadingReducer; |
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 _ from 'lodash'; | |
export const createLoadingSelector = (actions) => (state) => { | |
// returns true only when all actions is not loading | |
return _(actions) | |
.some((action) => _.get(state, `loading.${action}`)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment