CRUD Slice Helper

crudSlice(options) โ‡’ SagaSlice

This helper implements all the CRUD helpers in this library and instantiates them into a saga slice. Almost identical to the example you saw in crudSaga, this brings all of these helpers into 1 master helper. The reason for splitting up the helpers and giving access to them is because sometimes you don't want an entire CRUD, or, sometimes you're not following REST standard and want to have more flexibility. That's OK, so we split it up. In the cases where you're following REST and want a quick CRUD saga workflow, you use crudSlice

Returns: SagaSlice - A saga slice module

ParamTypeDescription
optionsobjectOptions to pass to saga helper
options.namestringRequired. Slice name
options.sagaApiobjectRequired. Saga API instance
options.initialStateobjectExtra initial state values
options.reducersobjectExtra reducers
options.sagasfunctionExtra sagas

Example

const { crudSlice } from 'saga-slice-helpers';
const { sagaApi } from './myApiFile';
export default crudSlice({
name: 'todos',
sagaApi,
initialState: { done: [], incomplete: [] },
takers: {
readAll: takeLatest
},
// OR
takers: 'takeLatest' // Will apply takeLatest to all sagas
reducers: {
setByStatus: (state, todos) => {
state.done = todos.filter(t => t.status === 'done');
state.incomplete = todos.filter(t => t.status === 'incomplete');
}
},
sagas: (A) => {
[A.readAllDone]: {
saga* ({ payload: { data } }) {
if (data) {
yield put(A.setByStatus(Object.values(data)));
}
}
}
}
});