Creating Resources
Redux Resource provides four action types for creating resources asynchronously. They are:
1
"CREATE_RESOURCES_PENDING"
2
"CREATE_RESOURCES_FAILED"
3
"CREATE_RESOURCES_SUCCEEDED"
4
"CREATE_RESOURCES_IDLE"
Copied!
Each request will always begin with an action with type CREATE_RESOURCES_PENDING. Then, one of the other three action types will be used to represent the resolution of that request. Use the other action types in the following way:
  • CREATE_RESOURCES_FAILED: Use this if the request fails for any reason. This
    could be network errors, or any
    greater than or equal to 400.
  • CREATE_RESOURCES_IDLE: Use this when the request is aborted.
  • CREATE_RESOURCES_SUCCEEDED: Use this when the request was successful.

Request Objects

Specifying a request key on the actions will create a request object in the store for this request. This object can be used to look up the status of the request.
For many create requests, you don't have the ID of the resource being created until after the operation succeeds. Therefore, to track the status of the request, you will need to specify the request key so that the status can be stored on the request object.
Many interfaces only allow one creation request at a time (although that request may be for a bulk creation). In these situations, you can just use a single request key, such as "create", for all of your creation requests.

Successful Creates

When an action of type CREATE_RESOURCES_SUCCEEDED is dispatched, three things will happen:
  1. 1.
    the resources included in the action's resources will be added to the resources section of the resource slice. Existing resources with the same ID will be merged with the new ones. To replace existing resources, rather than merge them, specify mergeResources: false on the action.
  2. 2.
    The metadata for each of the resources specified on the action will be updated with createStatus: 'SUCCEEDED'. To replace all of the existing meta, rather than merging it, specify mergeMeta: false on the action.
  3. 3.
    When a list is passed, the IDs from the resources array on the action will be added to the list. You may specify mergeListIds: false to replace the existing list instead.

Redux Resource XHR

Redux Resource XHR provides an action creator that simplifies making CRUD requests. If you'd like to build your own, then that's fine, too. The example below may help.

Example Action Creator

This example shows an action creator to create a single book. It uses the redux-thunk middleware and the library xhr for making requests.
1
import { actionTypes } from 'redux-resource';
2
import xhr from 'xhr';
3
4
export default function createBook(bookDetails) {
5
return function(dispatch) {
6
dispatch({
7
type: actionTypes.CREATE_RESOURCES_PENDING,
8
resourceType: 'books',
9
requestKey: 'create',
10
});
11
12
const req = xhr.post(
13
'/books',
14
{
15
json: bookDetails
16
},
17
(err, res, body) => {
18
if (req.aborted) {
19
dispatch({
20
type: actionTypes.CREATE_RESOURCES_IDLE,
21
resourceType: 'books',
22
requestKey: 'create',
23
});
24
} else if (err || res.statusCode >= 400) {
25
dispatch({
26
type: actionTypes.CREATE_RESOURCES_FAILED,
27
resourceType: 'books',
28
requestKey: 'create',
29
requestProperties: {
30
statusCode: res.statusCode
31
}
32
});
33
} else {
34
dispatch({
35
type: actionTypes.CREATE_RESOURCES_SUCCEEDED,
36
resourceType: 'books',
37
requestKey: 'create',
38
resources: [body],
39
requestProperties: {
40
statusCode: res.statusCode
41
}
42
});
43
}
44
}
45
);
46
47
return req;
48
}
49
}
Copied!