crudSlice

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
Param
Type
Description
options
object
Options to pass to saga helper
options.name
string
Required. Slice name
options.sagaApi
object
Required. Saga API instance
options.initialState
object
Extra initial state values
options.reducers
object
Extra reducers
options.sagas
function
Extra sagas
Example
1
const { crudSlice } from 'saga-slice-helpers';
2
const { sagaApi } from './myApiFile';
3
​
4
​
5
export default crudSlice({
6
name: 'todos',
7
sagaApi,
8
initialState: { done: [], incomplete: [] },
9
takers: {
10
readAll: takeLatest
11
},
12
// OR
13
takers: 'takeLatest' // Will apply takeLatest to all sagas
14
reducers: {
15
setByStatus: (state, todos) => {
16
state.done = todos.filter(t => t.status === 'done');
17
state.incomplete = todos.filter(t => t.status === 'incomplete');
18
}
19
},
20
sagas: (A) => {
21
[A.readAllDone]: {
22
saga* ({ payload: { data } }) {
23
if (data) {
24
yield put(A.setByStatus(Object.values(data)));
25
}
26
}
27
}
28
}
29
});
Copied!
Last modified 1yr ago
Copy link