/
state.js
93 lines (86 loc) · 2.13 KB
/
state.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*global SiteSettings */
/**
* External dependencies
*/
import { combineReducers } from 'redux';
import API from 'wordpress-rest-api-oauth-1';
const api = new API( {
url: SiteSettings.endpoint,
} );
/**
* Media actions
*/
export const MEDIA_REQUEST = 'wordpress-redux/media/REQUEST';
export const MEDIA_REQUEST_SUCCESS = 'wordpress-redux/media/REQUEST_SUCCESS';
export const MEDIA_REQUEST_FAILURE = 'wordpress-redux/media/REQUEST_FAILURE';
/*
* Tracks all known media objects, indexed by post global ID.
*
* @param {Object} state Current state
* @param {Object} action Action payload
* @return {Object} Updated state
*/
function items( state = {}, action ) {
switch ( action.type ) {
case MEDIA_REQUEST_SUCCESS:
return Object.assign( {}, state, {
[ action.media.id ]: action.media,
} );
default:
return state;
}
}
/**
* Returns the updated media requests state after an action has been
* dispatched. The state reflects a mapping of attachment ID to a
* boolean reflecting whether a request for the media is in progress.
*
* @param {Object} state Current state
* @param {Object} action Action payload
* @return {Object} Updated state
*/
function requests( state = {}, action ) {
switch ( action.type ) {
case MEDIA_REQUEST:
case MEDIA_REQUEST_SUCCESS:
case MEDIA_REQUEST_FAILURE:
return Object.assign( {}, state, { [ action.attachmentId ]: MEDIA_REQUEST === action.type } );
default:
return state;
}
}
export default combineReducers( {
items,
requests,
} );
/**
* Triggers a network request to fetch a specific media item from a site.
*
* @param {number} attachmentId Attachment ID
* @return {Function} Action thunk
*/
export function requestMedia( attachmentId ) {
return dispatch => {
dispatch( {
type: MEDIA_REQUEST,
attachmentId,
} );
const query = {
_embed: true,
};
api.get( '/wp/v2/media/' + attachmentId, query ).then( media => {
dispatch( {
type: MEDIA_REQUEST_SUCCESS,
attachmentId,
media,
} );
return null;
} ).catch( error => {
dispatch( {
type: MEDIA_REQUEST_FAILURE,
attachmentId,
error,
} );
} );
};
}