2018-08-22 17:54:48 +00:00
|
|
|
/** @format */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
import { dispatch } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { stringifyQuery } from 'lib/nav-utils';
|
|
|
|
import { NAMESPACE } from 'store/constants';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
async getReportStats( state, endpoint, query ) {
|
|
|
|
const statEndpoints = [ 'orders', 'revenue', 'products' ];
|
2018-08-29 15:55:56 +00:00
|
|
|
let apiPath = endpoint + stringifyQuery( query );
|
2018-08-22 17:54:48 +00:00
|
|
|
|
|
|
|
if ( statEndpoints.indexOf( endpoint ) >= 0 ) {
|
|
|
|
apiPath = NAMESPACE + 'reports/' + endpoint + '/stats' + stringifyQuery( query );
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2018-09-03 15:25:38 +00:00
|
|
|
const response = await apiFetch( {
|
2018-08-22 17:54:48 +00:00
|
|
|
path: apiPath,
|
2018-09-03 15:25:38 +00:00
|
|
|
parse: false,
|
2018-08-22 17:54:48 +00:00
|
|
|
} );
|
2018-09-03 15:25:38 +00:00
|
|
|
|
|
|
|
const report = await response.json();
|
|
|
|
const totalResults = parseInt( response.headers.get( 'x-wp-total' ) );
|
|
|
|
const totalPages = parseInt( response.headers.get( 'x-wp-totalpages' ) );
|
|
|
|
dispatch( 'wc-admin' ).setReportStats( endpoint, report, query, totalResults, totalPages );
|
2018-08-22 17:54:48 +00:00
|
|
|
} catch ( error ) {
|
|
|
|
dispatch( 'wc-admin' ).setReportStatsError( endpoint, query );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|