Reading Resources
Redux Resource provides four action types for reading resources asynchronously. They are:
1
"READ_RESOURCES_PENDING"
2
"READ_RESOURCES_FAILED"
3
"READ_RESOURCES_SUCCEEDED"
4
"READ_RESOURCES_IDLE"
Copied!
Each request will always begin with an action with type READ_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:
  • READ_RESOURCES_FAILED: Use this if the request fails for any reason. This
    could be network errors, or any
    greater than or equal to 400.
  • READ_RESOURCES_IDLE: Use this when the request is aborted.
  • READ_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.
Although it is recommended that you specify a request key when possible, there are some situations when you may not need to when fetching resources.
When fetching a single resource, you typically provide the ID to be fetched. Therefore, request objects aren't always necessary, as you can track the request on the resource's metadata directly.
For read requests that return multiple resources, it is typically preferable to specify a request key.

Successful Reads

When an action of type READ_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 readStatus: '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: Reading One Resource

This example shows an action creator to read 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 readBook(bookId) {
5
return function(dispatch) {
6
dispatch({
7
type: actionTypes.READ_RESOURCES_PENDING,
8
resourceType: 'books',
9
resources: [bookId],
10
});
11
12
const req = xhr.get(
13
`/books/${bookId}`,
14
{json: true},
15
(err, res, body) => {
16
if (req.aborted) {
17
dispatch({
18
type: actionTypes.READ_RESOURCES_IDLE,
19
resourceType: 'books',
20
resources: [bookId],
21
});
22
} else if (err || res.statusCode >= 400) {
23
dispatch({
24
type: actionTypes.READ_RESOURCES_FAILED,
25
resourceType: 'books',
26
resources: [bookId],
27
requestProperties: {
28
statusCode: res.statusCode
29
}
30
});
31
} else {
32
dispatch({
33
type: actionTypes.READ_RESOURCES_SUCCEEDED,
34
resourceType: 'books',
35
resources: [body],
36
requestProperties: {
37
statusCode: res.statusCode
38
}
39
});
40
}
41
}
42
);
43
44
return req;
45
}
46
}
Copied!

Example Action Creator: Reading Many Resource

This example shows an action creator to read multiple books. It uses the redux-thunk middleware and the library xhr for making requests. To create a query string, it uses the querystring module.
1
import { actionTypes } from 'redux-resource';
2
import xhr from 'xhr';
3
import qs from 'querystring';
4
5
export default function readBooks(query) {
6
return function(dispatch) {
7
dispatch({
8
type: actionTypes.READ_RESOURCES_PENDING,
9
resourceType: 'books',
10
requestKey: 'search',
11
requestProperties: {
12
statusCode: null
13
}
14
});
15
16
const queryString = qs.stringify(query);
17
18
const req = xhr.get(
19
`/books?${queryString}`,
20
{json: true},
21
(err, res, body) => {
22
if (req.aborted) {
23
dispatch({
24
type: actionTypes.READ_RESOURCES_IDLE,
25
resourceType: 'books',
26
requestKey: 'search',
27
requestProperties: {
28
statusCode: null
29
}
30
});
31
} else if (err || res.statusCode >= 400) {
32
dispatch({
33
type: actionTypes.READ_RESOURCES_FAILED,
34
resourceType: 'books',
35
requestKey: 'search',
36
requestProperties: {
37
statusCode: res.statusCode
38
}
39
});
40
} else {
41
dispatch({
42
type: actionTypes.READ_RESOURCES_SUCCEEDED,
43
resourceType: 'books',
44
requestKey: 'search',
45
resources: body,
46
requestProperties: {
47
statusCode: res.statusCode
48
}
49
});
50
}
51
}
52
);
53
54
return req;
55
}
56
}
Copied!