Hook up ReportTable to wc-api.

This commit is contained in:
Jeff Stieler 2018-12-05 10:10:54 -07:00
parent 47703fa9b9
commit 16f24a31ba
6 changed files with 139 additions and 2 deletions

View File

@ -5,7 +5,6 @@
import { applyFilters } from '@wordpress/hooks'; import { applyFilters } from '@wordpress/hooks';
import { Component } from '@wordpress/element'; import { Component } from '@wordpress/element';
import { compose } from '@wordpress/compose'; import { compose } from '@wordpress/compose';
import { withSelect } from '@wordpress/data';
import { get, orderBy } from 'lodash'; import { get, orderBy } from 'lodash';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
@ -20,6 +19,7 @@ import { onQueryChange } from '@woocommerce/navigation';
*/ */
import ReportError from 'analytics/components/report-error'; import ReportError from 'analytics/components/report-error';
import { getReportChartData, getReportTableData } from 'store/reports/utils'; import { getReportChartData, getReportTableData } from 'store/reports/utils';
import withSelect from 'wc-api/with-select';
const TABLE_FILTER = 'woocommerce_admin_report_table'; const TABLE_FILTER = 'woocommerce_admin_report_table';

View File

@ -297,7 +297,7 @@ export function getReportTableQuery( urlQuery, query ) {
*/ */
export function getReportTableData( endpoint, urlQuery, select, query = {} ) { export function getReportTableData( endpoint, urlQuery, select, query = {} ) {
const { getReportItems, isGetReportItemsRequesting, isGetReportItemsError } = select( const { getReportItems, isGetReportItemsRequesting, isGetReportItemsError } = select(
'wc-admin' 'wc-api'
); );
const tableQuery = reportsUtils.getReportTableQuery( urlQuery, query ); const tableQuery = reportsUtils.getReportTableQuery( urlQuery, query );

View File

@ -0,0 +1,11 @@
/** @format */
/**
* Internal dependencies
*/
import operations from './operations';
import selectors from './selectors';
export default {
operations,
selectors,
};

View File

@ -0,0 +1,74 @@
/** @format */
/**
* External dependencies
*/
import apiFetch from '@wordpress/api-fetch';
/**
* WooCommerce dependencies
*/
import { stringifyQuery } from '@woocommerce/navigation';
/**
* Internal dependencies
*/
import { getResourceIdentifier, getResourcePrefix } from '../../utils';
import { NAMESPACE } from '../../constants';
import { SWAGGERNAMESPACE } from 'store/constants';
// TODO: Remove once swagger endpoints are phased out.
const swaggerEndpoints = [ 'categories', 'coupons', 'taxes' ];
const typeEndpointMap = {
'report-items-query-orders': 'orders',
'report-items-query-revenue': 'revenue',
'report-items-query-products': 'products',
'report-items-query-categories': 'categories',
'report-items-query-coupons': 'coupons',
'report-items-query-taxes': 'taxes',
};
function read( resourceNames, fetch = apiFetch ) {
const filteredNames = resourceNames.filter( name => {
const prefix = getResourcePrefix( name );
return Boolean( typeEndpointMap[ prefix ] );
} );
return filteredNames.map( async resourceName => {
const prefix = getResourcePrefix( resourceName );
const endpoint = typeEndpointMap[ prefix ];
const query = getResourceIdentifier( resourceName );
let apiPath = NAMESPACE + '/reports/' + endpoint + stringifyQuery( query );
if ( swaggerEndpoints.indexOf( endpoint ) >= 0 ) {
apiPath = SWAGGERNAMESPACE + 'reports/' + endpoint + stringifyQuery( query );
}
try {
const response = await fetch( {
parse: false,
path: apiPath,
} );
const report = await response.json();
// TODO: exclude these if using swagger?
const totalResults = parseInt( response.headers.get( 'x-wp-total' ) );
const totalPages = parseInt( response.headers.get( 'x-wp-totalpages' ) );
return {
[ resourceName ]: {
data: report,
totalResults,
totalPages,
},
};
} catch ( error ) {
return { [ resourceName ]: { error } };
}
} );
}
export default {
read,
};

View File

@ -0,0 +1,49 @@
/** @format */
/**
* External dependencies
*/
import { isNil } from 'lodash';
/**
* Internal dependencies
*/
import { getResourceName } from '../../utils';
import { DEFAULT_REQUIREMENT } from '../../constants';
const getReportItems = ( getResource, requireResource ) => (
type,
query = {},
requirement = DEFAULT_REQUIREMENT
) => {
const resourceName = getResourceName( `report-items-query-${ type }`, query );
const data = requireResource( requirement, resourceName ) || {};
return data;
};
const isGetReportItemsRequesting = getResource => ( type, query = {} ) => {
const resourceName = getResourceName( `report-items-query-${ type }`, query );
const { lastRequested, lastReceived } = getResource( resourceName );
if ( isNil( lastRequested ) ) {
return false;
}
if ( isNil( lastReceived ) ) {
return true;
}
return lastRequested > lastReceived;
};
const isGetReportItemsError = getResource => ( type, query = {} ) => {
const resourceName = getResourceName( `report-items-query-${ type }`, query );
return getResource( resourceName ).error;
};
export default {
getReportItems,
isGetReportItemsRequesting,
isGetReportItemsError,
};

View File

@ -5,6 +5,7 @@
*/ */
import notes from './notes'; import notes from './notes';
import orders from './orders'; import orders from './orders';
import reportItems from './reports/items';
import reportStats from './reports/stats'; import reportStats from './reports/stats';
function createWcApiSpec() { function createWcApiSpec() {
@ -12,6 +13,7 @@ function createWcApiSpec() {
selectors: { selectors: {
...notes.selectors, ...notes.selectors,
...orders.selectors, ...orders.selectors,
...reportItems.selectors,
...reportStats.selectors, ...reportStats.selectors,
}, },
operations: { operations: {
@ -19,6 +21,7 @@ function createWcApiSpec() {
return [ return [
...notes.operations.read( resourceNames ), ...notes.operations.read( resourceNames ),
...orders.operations.read( resourceNames ), ...orders.operations.read( resourceNames ),
...reportItems.operations.read( resourceNames ),
...reportStats.operations.read( resourceNames ), ...reportStats.operations.read( resourceNames ),
]; ];
}, },