File size: 3,607 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { USER_LICENSES_REQUEST, USER_LICENSES_COUNTS_REQUEST } from 'calypso/state/action-types';
import { registerHandlers } from 'calypso/state/data-layer/handler-registry';
import { convertToCamelCase } from 'calypso/state/data-layer/utils';
import { http } from 'calypso/state/data-layer/wpcom-http/actions';
import { dispatchRequest } from 'calypso/state/data-layer/wpcom-http/utils';
import {
	licensesReceiveAction,
	licensesRequestFailureAction,
	licensesRequestSuccessAction,
	licensesCountsReceiveAction,
	licensesCountsRequestFailureAction,
	licensesCountsRequestSuccessAction,
} from 'calypso/state/user-licensing/actions';

/**
 * @module state/data-layer/wpcom/user-licensing
 */

// -- LICENSES handlers --
/**
 * Dispatches a request to fetch user licenses
 * @param {Object} action Redux action
 * @returns {Object} original action
 */
export const requestLicenses = ( action ) =>
	http(
		{
			method: 'GET',
			apiNamespace: 'wpcom/v2',
			path: '/jetpack-licensing/user/licenses',
			query: {
				// Do not apply filters during search as search takes over (matches Calypso Blue Post search behavior).
				...( action.search
					? { search: action.search }
					: { filter: action.filter, page: action.page } ),
				sort_field: action.sortField,
				sort_direction: action.sortDirection,
				per_page: action.perPage,
			},
		},
		action
	);

/**
 * Dispatches returned user licenses data
 * @param {Object} action Redux action
 * @param {Object} licenses raw data from user licensing API
 * @returns {Array<Object>} Redux actions
 */
export const receiveLicenses = ( action, licenses ) => [
	licensesRequestSuccessAction(),
	licensesReceiveAction( licenses ),
];

/**
 * Dispatches returned error from user licenses request
 * @param {Object} action Redux action
 * @param {Object} rawError raw error from HTTP request
 * @returns {Object} Redux action
 */
export const receiveLicensesError = ( action, rawError ) =>
	licensesRequestFailureAction( rawError instanceof Error ? rawError.message : rawError );

// -- License COUNTS handlers --
/**
 * Dispatches a request to fetch user licenses counts
 * @param {Object} action Redux action
 * @returns {Object} original action
 */
export const requestCounts = ( action ) =>
	http(
		{
			method: 'GET',
			apiNamespace: 'wpcom/v2',
			path: '/jetpack-licensing/user/licenses/counts',
		},
		action
	);

/**
 * Dispatches returned user licenses counts data
 * @param {Object} action Redux action
 * @param {Object} counts raw count data from user licensing API
 * @returns {Array<Object>} Redux actions
 */
export const receiveCounts = ( action, counts ) => [
	licensesCountsRequestSuccessAction(),
	licensesCountsReceiveAction( counts ),
];

/**
 * Dispatches returned error user license counts request
 * @param {Object} action Redux action
 * @param {Object} rawError raw error from HTTP request
 * @returns {Object} Redux action
 */
export const receiveCountsError = ( action, rawError ) =>
	licensesCountsRequestFailureAction( rawError instanceof Error ? rawError.message : rawError );

export const dispatchCountsRequest = dispatchRequest( {
	fetch: requestCounts,
	onSuccess: receiveCounts,
	onError: receiveCountsError,
} );

export const dispatchLicensesRequest = dispatchRequest( {
	fetch: requestLicenses,
	onSuccess: receiveLicenses,
	onError: receiveLicensesError,
	fromApi: ( paginatedItems ) => convertToCamelCase( paginatedItems ),
} );

registerHandlers( 'state/data-layer/wpcom/user-licensing', {
	[ USER_LICENSES_REQUEST ]: [ dispatchLicensesRequest ],
	[ USER_LICENSES_COUNTS_REQUEST ]: [ dispatchCountsRequest ],
} );