2018-11-22 23:12:12 +00:00
|
|
|
/** @format */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
import { dispatch } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { stringifyQuery } from '@woocommerce/navigation';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { NAMESPACE, SWAGGERNAMESPACE } from 'store/constants';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
async getReportItems( ...args ) {
|
|
|
|
const [ endpoint, query ] = args.slice( -2 );
|
|
|
|
|
2018-11-26 02:58:19 +00:00
|
|
|
const swaggerEndpoints = [ 'categories', 'coupons', 'taxes' ];
|
2018-11-22 23:12:12 +00:00
|
|
|
if ( swaggerEndpoints.indexOf( endpoint ) >= 0 ) {
|
|
|
|
try {
|
|
|
|
const response = await fetch(
|
|
|
|
SWAGGERNAMESPACE + 'reports/' + endpoint + stringifyQuery( query )
|
|
|
|
);
|
2018-11-29 14:03:04 +00:00
|
|
|
const itemsData = await response.json();
|
2018-11-22 23:12:12 +00:00
|
|
|
|
2018-11-29 14:03:04 +00:00
|
|
|
dispatch( 'wc-admin' ).setReportItems( endpoint, query, itemsData );
|
2018-11-22 23:12:12 +00:00
|
|
|
} catch ( error ) {
|
|
|
|
dispatch( 'wc-admin' ).setReportItemsError( endpoint, query );
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2018-11-29 14:03:04 +00:00
|
|
|
const response = await apiFetch( {
|
|
|
|
parse: false,
|
2018-11-22 23:12:12 +00:00
|
|
|
path: NAMESPACE + 'reports/' + endpoint + stringifyQuery( query ),
|
|
|
|
} );
|
|
|
|
|
2018-11-29 14:03:04 +00:00
|
|
|
const itemsData = await response.json();
|
|
|
|
const totalCount = parseInt( response.headers.get( 'x-wp-total' ) );
|
|
|
|
dispatch( 'wc-admin' ).setReportItems( endpoint, query, itemsData, totalCount );
|
2018-11-22 23:12:12 +00:00
|
|
|
} catch ( error ) {
|
|
|
|
dispatch( 'wc-admin' ).setReportItemsError( endpoint, query );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|