Skip to content

Instantly share code, notes, and snippets.

@andriangungon
Last active March 18, 2021 01:57
Show Gist options
  • Save andriangungon/218bdecbb79f3ce0c3b5001cde349e70 to your computer and use it in GitHub Desktop.
Save andriangungon/218bdecbb79f3ce0c3b5001cde349e70 to your computer and use it in GitHub Desktop.
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;
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