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.
Sets the isFetching
state to false
.
Maps the response payload into an object formatted like { [item.id]: item }
state.data = Object.values(payload || {}).reduce((a, c) => ({...a,[c.id]: c}), {});
Sets the isFetching
state to false
.
Maps response to data by id
state.data[payload.id] = {...(state.data[payload.id] || {}),...payload};
Sets the isFetching
state to false
Maps response to data by id
state.data[payload.id] = payload;
Sets the isFetching
state to false
.
Maps response to data by id
state.data[payload.id] = {...(state.data[payload.id] || {}),...payload};
Sets the isFetching
state to false
.
Deletes item from data
delete state.data[payload.id];
Sets the isFetching
state to false
.
state.isFetching = false;state.error = payload;
Sets the isFetching
state to true
.
Sets the isFetching
state to false.
If passed a number or string, sets state.current
to an item in state.data
If passed an object, sets state.current
to payload
if (payload.constructor === Object) {state.current = payload;}if (payload.constructor === Number || payload.constructor === String) {state.current = state.data[payload];}
Sets state.current
to null
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 || {})}
Generates a map of reducers for CRUD use
Can be extended or overwritten by passing extend
option
Done reducers are created if doneRecuders
is set to true
const actions = crudReducers({// ... more reducers}, true) // <-- creates `done` actionsconst {setCurrent,resetCurrent,readAll, // loadingReducerreadOne, // loadingReducercreate, // loadingReducerupdate, // loadingReducerpatch, // loadingReducerdelete, // loadingReducerreadAllSuccess,readOneSuccess,createSuccess,updateSuccess,patchSuccessdeleteSuccess,readAllFail, // failReducerreadOneFail, // failReducercreateFail, // failReducerupdateFail, // failReducerpatchFail, // failReducerdeleteFail, // failReducerreadAllDone, // only if doneReducers is true, noopreadOneDone, // only if doneReducers is true, noopcreateDone, // only if doneReducers is true, noopupdateDone, // only if doneReducers is true, noopdeleteDone, // only if doneReducers is true, noop} = actions;
Creates a map of reducers specific to an ajax request lifecycle similar to what you see in crudReducers
, but only for 1 method call.
reducers
is an optional parameter. If the reducers are not specified, it will be provided a default. The reducers are overwritten using main
for the main action, success
for success action, fail
for fail action, and done
can be a boolean or a function.
Param | Type | Required | Description | Default |
name |
| yes | name of action | n/a |
reducers |
| no | object of reducers | empty object |
reducers.main |
| no | main reducer created from name argument as |
|
reducers.success |
| no | success reducer created from name argument as |
|
reducers.fail |
| no | fail reducer created from name argument as |
|
reducers.done |
| no | optional done reducer is boolean or reducer function create as |
|
Example:
const {getTodo,getTodoSuccess,getTodoFail,getTodoDone} = lifecycleReducers('getTodo', {success: (state, payload) => state.data = payload,done: true})
Does nothing. Used for declaring reducers.