actionTypes
An object of Redux action types that the resource reducer responds to. Dispatch these from action creators to change the state of your store.
The complete object is shown below:
1
{
2
// Update resources, metadata, and lists synchronously
3
UPDATE_RESOURCES: 'UPDATE_RESOURCES',
4
// Remove resources from the store synchronously
5
DELETE_RESOURCES: 'DELETE_RESOURCES',
6
7
// The following action types are to support CRUD'ing resources
8
// asynchronously using requests
9
CREATE_RESOURCES_PENDING: 'CREATE_RESOURCES_PENDING',
10
CREATE_RESOURCES_FAILED: 'CREATE_RESOURCES_FAILED',
11
CREATE_RESOURCES_SUCCEEDED: 'CREATE_RESOURCES_SUCCEEDED',
12
CREATE_RESOURCES_IDLE: 'CREATE_RESOURCES_IDLE',
13
14
READ_RESOURCES_PENDING: 'READ_RESOURCES_PENDING',
15
READ_RESOURCES_FAILED: 'READ_RESOURCES_FAILED',
16
READ_RESOURCES_SUCCEEDED: 'READ_RESOURCES_SUCCEEDED',
17
READ_RESOURCES_IDLE: 'READ_RESOURCES_IDLE',
18
19
UPDATE_RESOURCES_PENDING: 'UPDATE_RESOURCES_PENDING',
20
UPDATE_RESOURCES_FAILED: 'UPDATE_RESOURCES_FAILED',
21
UPDATE_RESOURCES_SUCCEEDED: 'UPDATE_RESOURCES_SUCCEEDED',
22
UPDATE_RESOURCES_IDLE: 'UPDATE_RESOURCES_IDLE',
23
24
DELETE_RESOURCES_PENDING: 'DELETE_RESOURCES_PENDING',
25
DELETE_RESOURCES_FAILED: 'DELETE_RESOURCES_FAILED',
26
DELETE_RESOURCES_SUCCEEDED: 'DELETE_RESOURCES_SUCCEEDED',
27
DELETE_RESOURCES_IDLE: 'DELETE_RESOURCES_IDLE',
28
}
Copied!

Notes

The action types can be organized into two groups:
  • Direct manipulation of the store. UPDATE_RESOURCES and DELETE_RESOURCES allow you to synchronously modify the store, independent of requests.
  • The rest of the action types are for requests. Within the request action types, there are four groups of action types, one for each of the four CRUD actions. Within each CRUD action "group," the four action types reflect the four request statuses.

Reserved Action Types

An upcoming version of Redux Resource will utilize four new action types to simplify the request action types. Accordingly, these action types have been reserved, and we do not recommend that you use them in your application. The list of reserved action types is:
1
REQUEST_IDLE
2
REQUEST_PENDING
3
REQUEST_FAILED
4
REQUEST_SUCCEEDED
Copied!
Note: A warning will be logged to the console if you dispatch an action with one of these action types in your application.

Example

This example shows an action creator that reads 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(`/books/${bookId}`, {json: true}, (err, res) => {
13
if (req.aborted) {
14
dispatch({
15
type: actionTypes.READ_RESOURCES_IDLE,
16
resourceType: 'books',
17
resources: [bookId]
18
});
19
} else if (err || res.statusCode >= 400) {
20
dispatch({
21
type: actionTypes.READ_RESOURCES_FAILED,
22
resourceType: 'books',
23
resources: [bookId]
24
});
25
} else {
26
dispatch({
27
type: actionTypes.READ_RESOURCES_SUCCEEDED,
28
resourceType: 'books',
29
resources: [res.body]
30
});
31
}
32
});
33
34
return req;
35
}
36
}
Copied!

Tips

  • The {crudAction}_RESOURCES_IDLE action type is useful anytime that you want to "reset" the status of a request. One use case for this is when a request is aborted. Another use case would be for an alert that displays whenever a request is in a failed state. When the user dismisses the alert, you might trigger this action type to 'reset' the request status back to IDLE, which would then hide the alert.
    You may not always use this action type, and that's fine. But it's here if you do need it.
Last modified 3yr ago