Reducer Helpers
The following helpers are available for use with your saga slices. When used in combination with the other helpers, it makes development of new sagas much quicker.
readAllSuccess(state, payload)
- Sets the
isFetchingstate tofalse. - Maps the response payload into an object formatted like
{ [item.id]: item }
state.data = Object.values(payload || {}).reduce(
(a, c) => ({
...a,
[c.id]: c,
}),
{}
);
readOneSuccess(state, payload)
- Sets the
isFetchingstate tofalse. - Maps response to data by id
state.data[payload.id] = {
...(state.data[payload.id] || {}),
...payload,
};
createSuccess(state, payload)
- Sets the
isFetchingstate tofalse - Maps response to data by id
state.data[payload.id] = payload;
updateSuccess(state, payload)
- Sets the
isFetchingstate tofalse. - Maps response to data by id
state.data[payload.id] = {
...(state.data[payload.id] || {}),
...payload,
};
deleteSuccess(state, payload)
- Sets the
isFetchingstate tofalse. - Deletes item from
data
delete state.data[payload.id];
failReducer(state, payload)
- Sets the
isFetchingstate tofalse.
state.isFetching = false;
state.error = payload;
loadingReducer(state, payload)
- Sets the
isFetchingstate totrue.
notLoadingReducer(state, payload)
- Sets the
isFetchingstate to false.
setCurrent(state, payload)
- If passed a number or string, sets
state.currentto an item instate.data - If passed an object, sets
state.currenttopayload
if (payload.constructor === Object) {
state.current = payload;
}
if (payload.constructor === Number || payload.constructor === String) {
state.current = state.data[payload];
}
resetCurrent(state, payload)
- Sets
state.currenttonull
crudInitialState(extendState)
Returns a state object structured to work with other helper functions. You can extend or overwrite the current state elements by passing an extended state. This function returns:
const initialState = {
isFetching: false,
current: null,
data: {},
error: null,
...(extendState || {}),
};
crudReducers(extend, doneReducers) => CrudReducerInstance
- Generates a map of reducers for CRUD use
- Can be extended or overwritten by passing
extendoption - Done reducers are created if
doneRecudersis set to true
const actions = crudReducers({
// ... more reducers
}, true) // <-- creates `done` actions
const {
setCurrent,
resetCurrent,
readAll, // loadingReducer
readOne, // loadingReducer
create, // loadingReducer
update, // loadingReducer
patch, // loadingReducer
delete, // loadingReducer
readAllSuccess,
readOneSuccess,
createSuccess,
updateSuccess,
patchSuccess
deleteSuccess,
readAllFail, // failReducer
readOneFail, // failReducer
createFail, // failReducer
updateFail, // failReducer
patchFail, // failReducer
deleteFail, // failReducer
readAllDone, // only if doneReducers is true, noop
readOneDone, // only if doneReducers is true, noop
createDone, // only if doneReducers is true, noop
updateDone, // only if doneReducers is true, noop
deleteDone, // only if doneReducers is true, noop
} = actions;
lifecycleReducers(name, reducers)
- Creates a map of reducers specific to an ajax request lifecycle similar to what you see in
crudReducers, but only for 1 method call. reducersis an optional parameter. If the reducers are not specified, it will be provided a default. The reducers are overwritten usingmainfor the main action,successfor success action,failfor fail action, anddonecan be a boolean or a function.
| Param | Type | Required | Description | Default |
|---|---|---|---|---|
| name | string | yes | name of action | n/a |
| reducers | object | no | object of reducers | empty object |
| reducers.main | function | no | main reducer created from name argument as name | loadingReducer |
| reducers.success | function | no | success reducer created from name argument as nameSuccess | notLoadingReducer |
| reducers.fail | function | no | fail reducer created from name argument as nameFail | failReducer |
| reducers.done | function | boolean | no | optional done reducer is boolean or reducer function create as nameDone | false or noop |
Example:
const { getTodo, getTodoSuccess, getTodoFail, getTodoDone } = lifecycleReducers(
"getTodo",
{
success: (state, payload) => (state.data = payload),
done: true,
}
);
noop()
- Does nothing. Used for declaring reducers.