|
|
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'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const requestLicenses = ( action ) => |
|
|
http( |
|
|
{ |
|
|
method: 'GET', |
|
|
apiNamespace: 'wpcom/v2', |
|
|
path: '/jetpack-licensing/user/licenses', |
|
|
query: { |
|
|
|
|
|
...( action.search |
|
|
? { search: action.search } |
|
|
: { filter: action.filter, page: action.page } ), |
|
|
sort_field: action.sortField, |
|
|
sort_direction: action.sortDirection, |
|
|
per_page: action.perPage, |
|
|
}, |
|
|
}, |
|
|
action |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const receiveLicenses = ( action, licenses ) => [ |
|
|
licensesRequestSuccessAction(), |
|
|
licensesReceiveAction( licenses ), |
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const receiveLicensesError = ( action, rawError ) => |
|
|
licensesRequestFailureAction( rawError instanceof Error ? rawError.message : rawError ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const requestCounts = ( action ) => |
|
|
http( |
|
|
{ |
|
|
method: 'GET', |
|
|
apiNamespace: 'wpcom/v2', |
|
|
path: '/jetpack-licensing/user/licenses/counts', |
|
|
}, |
|
|
action |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const receiveCounts = ( action, counts ) => [ |
|
|
licensesCountsRequestSuccessAction(), |
|
|
licensesCountsReceiveAction( counts ), |
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 ], |
|
|
} ); |
|
|
|