From 16f24a31bacb7357b7b3713a09ecb41ddb618d72 Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Wed, 5 Dec 2018 10:10:54 -0700 Subject: [PATCH] Hook up ReportTable to wc-api. --- .../components/report-table/index.js | 2 +- .../client/store/reports/utils.js | 2 +- .../client/wc-api/reports/items/index.js | 11 +++ .../client/wc-api/reports/items/operations.js | 74 +++++++++++++++++++ .../client/wc-api/reports/items/selectors.js | 49 ++++++++++++ .../client/wc-api/wc-api-spec.js | 3 + 6 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 plugins/woocommerce-admin/client/wc-api/reports/items/index.js create mode 100644 plugins/woocommerce-admin/client/wc-api/reports/items/operations.js create mode 100644 plugins/woocommerce-admin/client/wc-api/reports/items/selectors.js diff --git a/plugins/woocommerce-admin/client/analytics/components/report-table/index.js b/plugins/woocommerce-admin/client/analytics/components/report-table/index.js index 8dd7bb48ffc..2072da02551 100644 --- a/plugins/woocommerce-admin/client/analytics/components/report-table/index.js +++ b/plugins/woocommerce-admin/client/analytics/components/report-table/index.js @@ -5,7 +5,6 @@ import { applyFilters } from '@wordpress/hooks'; import { Component } from '@wordpress/element'; import { compose } from '@wordpress/compose'; -import { withSelect } from '@wordpress/data'; import { get, orderBy } from 'lodash'; import PropTypes from 'prop-types'; @@ -20,6 +19,7 @@ import { onQueryChange } from '@woocommerce/navigation'; */ import ReportError from 'analytics/components/report-error'; import { getReportChartData, getReportTableData } from 'store/reports/utils'; +import withSelect from 'wc-api/with-select'; const TABLE_FILTER = 'woocommerce_admin_report_table'; diff --git a/plugins/woocommerce-admin/client/store/reports/utils.js b/plugins/woocommerce-admin/client/store/reports/utils.js index 1337f8ad974..a661a3ee558 100644 --- a/plugins/woocommerce-admin/client/store/reports/utils.js +++ b/plugins/woocommerce-admin/client/store/reports/utils.js @@ -297,7 +297,7 @@ export function getReportTableQuery( urlQuery, query ) { */ export function getReportTableData( endpoint, urlQuery, select, query = {} ) { const { getReportItems, isGetReportItemsRequesting, isGetReportItemsError } = select( - 'wc-admin' + 'wc-api' ); const tableQuery = reportsUtils.getReportTableQuery( urlQuery, query ); diff --git a/plugins/woocommerce-admin/client/wc-api/reports/items/index.js b/plugins/woocommerce-admin/client/wc-api/reports/items/index.js new file mode 100644 index 00000000000..cd0cff2f807 --- /dev/null +++ b/plugins/woocommerce-admin/client/wc-api/reports/items/index.js @@ -0,0 +1,11 @@ +/** @format */ +/** + * Internal dependencies + */ +import operations from './operations'; +import selectors from './selectors'; + +export default { + operations, + selectors, +}; diff --git a/plugins/woocommerce-admin/client/wc-api/reports/items/operations.js b/plugins/woocommerce-admin/client/wc-api/reports/items/operations.js new file mode 100644 index 00000000000..b27ffb37625 --- /dev/null +++ b/plugins/woocommerce-admin/client/wc-api/reports/items/operations.js @@ -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, +}; diff --git a/plugins/woocommerce-admin/client/wc-api/reports/items/selectors.js b/plugins/woocommerce-admin/client/wc-api/reports/items/selectors.js new file mode 100644 index 00000000000..0ae60913b0b --- /dev/null +++ b/plugins/woocommerce-admin/client/wc-api/reports/items/selectors.js @@ -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, +}; diff --git a/plugins/woocommerce-admin/client/wc-api/wc-api-spec.js b/plugins/woocommerce-admin/client/wc-api/wc-api-spec.js index ba5d961d534..7379539cad6 100644 --- a/plugins/woocommerce-admin/client/wc-api/wc-api-spec.js +++ b/plugins/woocommerce-admin/client/wc-api/wc-api-spec.js @@ -5,6 +5,7 @@ */ import notes from './notes'; import orders from './orders'; +import reportItems from './reports/items'; import reportStats from './reports/stats'; function createWcApiSpec() { @@ -12,6 +13,7 @@ function createWcApiSpec() { selectors: { ...notes.selectors, ...orders.selectors, + ...reportItems.selectors, ...reportStats.selectors, }, operations: { @@ -19,6 +21,7 @@ function createWcApiSpec() { return [ ...notes.operations.read( resourceNames ), ...orders.operations.read( resourceNames ), + ...reportItems.operations.read( resourceNames ), ...reportStats.operations.read( resourceNames ), ]; },