From 736d927ead3cdadd6911bbaabca78e24ac372678 Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Mon, 3 Dec 2018 19:58:07 -0700 Subject: [PATCH 01/14] Add fresh-data backed report stats operations and selectors. --- .../client/wc-api/reports/stats/index.js | 11 +++ .../client/wc-api/reports/stats/operations.js | 67 +++++++++++++++++++ .../client/wc-api/reports/stats/selectors.js | 49 ++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 plugins/woocommerce-admin/client/wc-api/reports/stats/index.js create mode 100644 plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js create mode 100644 plugins/woocommerce-admin/client/wc-api/reports/stats/selectors.js diff --git a/plugins/woocommerce-admin/client/wc-api/reports/stats/index.js b/plugins/woocommerce-admin/client/wc-api/reports/stats/index.js new file mode 100644 index 00000000000..cd0cff2f807 --- /dev/null +++ b/plugins/woocommerce-admin/client/wc-api/reports/stats/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/stats/operations.js b/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js new file mode 100644 index 00000000000..a74ba81476f --- /dev/null +++ b/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js @@ -0,0 +1,67 @@ +/** @format */ +/** + * External dependencies + */ +import apiFetch from '@wordpress/api-fetch'; + +/** + * WooCommerce dependencies + */ +import { stringifyQuery } from '@woocommerce/navigation'; + +/** + * Internal dependencies + */ +import { isResourcePrefix, getResourceIdentifier } from '../../utils'; +import { NAMESPACE } from '../../constants'; +import { SWAGGERNAMESPACE } from 'store/constants'; + +const statEndpoints = [ 'orders', 'revenue', 'products' ]; +// TODO: Remove once swagger endpoints are phased out. +const swaggerEndpoints = [ 'categories', 'coupons', 'taxes' ]; + +function read( resourceNames, fetch = apiFetch ) { + const filteredNames = resourceNames.filter( name => + isResourcePrefix( name, 'report-stats-query' ) + ); + + return filteredNames.map( async resourceName => { + const { endpoint, query } = getResourceIdentifier( resourceName ); + + let apiPath = endpoint + stringifyQuery( query ); + + if ( swaggerEndpoints.indexOf( endpoint ) >= 0 ) { + apiPath = SWAGGERNAMESPACE + 'reports/' + endpoint + '/stats' + stringifyQuery( query ); + } + + if ( statEndpoints.indexOf( endpoint ) >= 0 ) { + apiPath = NAMESPACE + '/reports/' + endpoint + '/stats' + 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/stats/selectors.js b/plugins/woocommerce-admin/client/wc-api/reports/stats/selectors.js new file mode 100644 index 00000000000..173bd64d346 --- /dev/null +++ b/plugins/woocommerce-admin/client/wc-api/reports/stats/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 getReportStats = ( getResource, requireResource ) => ( + endpoint, + query = {}, + requirement = DEFAULT_REQUIREMENT +) => { + const resourceName = getResourceName( 'report-stats-query', { endpoint, query } ); + const data = requireResource( requirement, resourceName ) || {}; + + return data; +}; + +const isReportStatsRequesting = getResource => ( endpoint, query = {} ) => { + const resourceName = getResourceName( 'report-stats-query', { endpoint, query } ); + const { lastRequested, lastReceived } = getResource( resourceName ); + + if ( isNil( lastRequested ) ) { + return false; + } + + if ( isNil( lastReceived ) ) { + return true; + } + + return lastRequested > lastReceived; +}; + +const isReportStatsError = getResource => ( endpoint, query = {} ) => { + const resourceName = getResourceName( 'report-stats-query', { endpoint, query } ); + return getResource( resourceName ).error; +}; + +export default { + getReportStats, + isReportStatsRequesting, + isReportStatsError, +}; From cfd44629416bf3c0d4142a3d68fb7a28480fa57f Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Mon, 3 Dec 2018 20:01:23 -0700 Subject: [PATCH 02/14] Allow nested objects in the `getResourceName()` identifier. --- plugins/woocommerce-admin/client/wc-api/utils.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/plugins/woocommerce-admin/client/wc-api/utils.js b/plugins/woocommerce-admin/client/wc-api/utils.js index eb4d148946e..c94419b153d 100644 --- a/plugins/woocommerce-admin/client/wc-api/utils.js +++ b/plugins/woocommerce-admin/client/wc-api/utils.js @@ -1,7 +1,21 @@ /** @format */ +/** + * External dependencies + */ +import { isObject } from 'lodash'; export function getResourceName( prefix, identifier ) { - const identifierString = JSON.stringify( identifier, Object.keys( identifier ).sort() ); + const keyList = []; + Object.keys( identifier ).forEach( key => { + keyList.push( key ); + + // whitelist nested object keys + if ( isObject( identifier[ key ] ) ) { + Array.prototype.push.apply( keyList, Object.keys( identifier[ key ] ) ); + } + } ); + + const identifierString = JSON.stringify( identifier, keyList.sort() ); return `${ prefix }:${ identifierString }`; } From 0d6cd1a7768cfc27fbb4283a206f7d781fa61a94 Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Mon, 3 Dec 2018 20:01:49 -0700 Subject: [PATCH 03/14] Add report stats to the `wc-api` spec. --- plugins/woocommerce-admin/client/wc-api/wc-api-spec.js | 3 +++ 1 file changed, 3 insertions(+) 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 2f6ada5a5b9..ba5d961d534 100644 --- a/plugins/woocommerce-admin/client/wc-api/wc-api-spec.js +++ b/plugins/woocommerce-admin/client/wc-api/wc-api-spec.js @@ -5,18 +5,21 @@ */ import notes from './notes'; import orders from './orders'; +import reportStats from './reports/stats'; function createWcApiSpec() { return { selectors: { ...notes.selectors, ...orders.selectors, + ...reportStats.selectors, }, operations: { read( resourceNames ) { return [ ...notes.operations.read( resourceNames ), ...orders.operations.read( resourceNames ), + ...reportStats.operations.read( resourceNames ), ]; }, }, From c8a76d6dc0f9a59e3dc5c51c1ea693d46532e115 Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Mon, 3 Dec 2018 20:02:20 -0700 Subject: [PATCH 04/14] Use fresh-data/wc-api for revenue report table. --- .../client/analytics/report/revenue/table.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/woocommerce-admin/client/analytics/report/revenue/table.js b/plugins/woocommerce-admin/client/analytics/report/revenue/table.js index e7854d53197..10ff6d1b5c4 100644 --- a/plugins/woocommerce-admin/client/analytics/report/revenue/table.js +++ b/plugins/woocommerce-admin/client/analytics/report/revenue/table.js @@ -6,7 +6,7 @@ import { __, _n } from '@wordpress/i18n'; import { Component } from '@wordpress/element'; import { format as formatDate } from '@wordpress/date'; import { compose } from '@wordpress/compose'; -import { get } from 'lodash'; +import { get, map } from 'lodash'; /** * WooCommerce dependencies @@ -224,7 +224,7 @@ export default compose( withSelect( ( select, props ) => { const { query } = props; const datesFromQuery = getCurrentDates( query ); - const { getReportStats, isReportStatsRequesting, isReportStatsError } = select( 'wc-admin' ); + const { getReportStats, isReportStatsRequesting, isReportStatsError } = select( 'wc-api' ); // TODO Support hour here when viewing a single day const tableQuery = { From de1da53aaa6916beee5f34f885f2585e6104437d Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Tue, 4 Dec 2018 08:33:30 -0700 Subject: [PATCH 05/14] Use wc-api for Report Chart data. --- .../client/analytics/components/report-chart/index.js | 2 +- plugins/woocommerce-admin/client/store/reports/utils.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/woocommerce-admin/client/analytics/components/report-chart/index.js b/plugins/woocommerce-admin/client/analytics/components/report-chart/index.js index 707ee5cc4ce..85f991720f7 100644 --- a/plugins/woocommerce-admin/client/analytics/components/report-chart/index.js +++ b/plugins/woocommerce-admin/client/analytics/components/report-chart/index.js @@ -5,7 +5,6 @@ import { Component } from '@wordpress/element'; import { compose } from '@wordpress/compose'; import { format as formatDate } from '@wordpress/date'; -import { withSelect } from '@wordpress/data'; import PropTypes from 'prop-types'; import { find, get } from 'lodash'; @@ -28,6 +27,7 @@ import { Chart } from '@woocommerce/components'; */ import { getReportChartData, getTooltipValueFormat } from 'store/reports/utils'; import ReportError from 'analytics/components/report-error'; +import withSelect from 'wc-api/with-select'; export const DEFAULT_FILTER = 'all'; diff --git a/plugins/woocommerce-admin/client/store/reports/utils.js b/plugins/woocommerce-admin/client/store/reports/utils.js index 243f8c71e6b..1d9685d7d89 100644 --- a/plugins/woocommerce-admin/client/store/reports/utils.js +++ b/plugins/woocommerce-admin/client/store/reports/utils.js @@ -182,7 +182,7 @@ export function getSummaryNumbers( endpoint, query, select ) { * @return {Object} Object containing API request information (response, fetching, and error details) */ export function getReportChartData( endpoint, dataType, query, select ) { - const { getReportStats, isReportStatsRequesting, isReportStatsError } = select( 'wc-admin' ); + const { getReportStats, isReportStatsRequesting, isReportStatsError } = select( 'wc-api' ); const response = { isEmpty: false, From 98478653e4b7c879dfe0c22aa8ee293b2be5dc38 Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Tue, 4 Dec 2018 10:22:46 -0700 Subject: [PATCH 06/14] Attempt to include report stats type in resource name prefix rather than identifier. --- .../components/report-summary/index.js | 2 +- .../client/analytics/report/revenue/index.js | 4 +-- .../client/store/reports/utils.js | 2 +- .../client/wc-api/reports/stats/operations.js | 25 ++++++++++++++----- .../client/wc-api/reports/stats/selectors.js | 13 +++++----- .../woocommerce-admin/client/wc-api/utils.js | 22 +++++----------- 6 files changed, 36 insertions(+), 32 deletions(-) diff --git a/plugins/woocommerce-admin/client/analytics/components/report-summary/index.js b/plugins/woocommerce-admin/client/analytics/components/report-summary/index.js index 6835b6083b5..22a127cdcfb 100644 --- a/plugins/woocommerce-admin/client/analytics/components/report-summary/index.js +++ b/plugins/woocommerce-admin/client/analytics/components/report-summary/index.js @@ -5,7 +5,6 @@ import { __ } from '@wordpress/i18n'; import { Component } from '@wordpress/element'; import { compose } from '@wordpress/compose'; -import { withSelect } from '@wordpress/data'; import PropTypes from 'prop-types'; /** @@ -21,6 +20,7 @@ import { SummaryList, SummaryListPlaceholder, SummaryNumber } from '@woocommerce import { getSummaryNumbers } from 'store/reports/utils'; import ReportError from 'analytics/components/report-error'; import { calculateDelta, formatValue } from './utils'; +import withSelect from 'wc-api/with-select'; export class ReportSummary extends Component { render() { diff --git a/plugins/woocommerce-admin/client/analytics/report/revenue/index.js b/plugins/woocommerce-admin/client/analytics/report/revenue/index.js index d5e70212068..cc381b9ffef 100644 --- a/plugins/woocommerce-admin/client/analytics/report/revenue/index.js +++ b/plugins/woocommerce-admin/client/analytics/report/revenue/index.js @@ -17,7 +17,7 @@ import { charts } from './config'; import getSelectedChart from 'lib/get-selected-chart'; import ReportChart from 'analytics/components/report-chart'; import ReportSummary from 'analytics/components/report-summary'; -import RevenueReportTable from './table'; +// import RevenueReportTable from './table'; export default class RevenueReport extends Component { render() { @@ -39,7 +39,7 @@ export default class RevenueReport extends Component { query={ query } selectedChart={ getSelectedChart( query.chart, charts ) } /> - + { /* */ } ); } diff --git a/plugins/woocommerce-admin/client/store/reports/utils.js b/plugins/woocommerce-admin/client/store/reports/utils.js index 1d9685d7d89..1337f8ad974 100644 --- a/plugins/woocommerce-admin/client/store/reports/utils.js +++ b/plugins/woocommerce-admin/client/store/reports/utils.js @@ -139,7 +139,7 @@ function getRequestQuery( endpoint, dataType, query ) { * @return {Object} Object containing summary number responses. */ export function getSummaryNumbers( endpoint, query, select ) { - const { getReportStats, isReportStatsRequesting, isReportStatsError } = select( 'wc-admin' ); + const { getReportStats, isReportStatsRequesting, isReportStatsError } = select( 'wc-api' ); const response = { isRequesting: false, isError: false, diff --git a/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js b/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js index a74ba81476f..e515cae2fa1 100644 --- a/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js +++ b/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js @@ -12,7 +12,7 @@ import { stringifyQuery } from '@woocommerce/navigation'; /** * Internal dependencies */ -import { isResourcePrefix, getResourceIdentifier } from '../../utils'; +import { getResourceIdentifier, getResourcePrefix } from '../../utils'; import { NAMESPACE } from '../../constants'; import { SWAGGERNAMESPACE } from 'store/constants'; @@ -20,14 +20,27 @@ const statEndpoints = [ 'orders', 'revenue', 'products' ]; // TODO: Remove once swagger endpoints are phased out. const swaggerEndpoints = [ 'categories', 'coupons', 'taxes' ]; +const typeEndpointMap = { + 'report-stats-query-orders': 'orders', + 'report-stats-query-revenue': 'revenue', + 'report-stats-query-products': 'products', + 'report-stats-query-categories': 'categories', + 'report-stats-query-coupons': 'coupons', + 'report-stats-query-taxes': 'taxes', +}; + function read( resourceNames, fetch = apiFetch ) { - const filteredNames = resourceNames.filter( name => - isResourcePrefix( name, 'report-stats-query' ) - ); + console.log( 'stats read', resourceNames ); + const filteredNames = resourceNames.filter( name => { + const prefix = getResourcePrefix( name ); + return Boolean( typeEndpointMap[ prefix ] ); + } ); return filteredNames.map( async resourceName => { - const { endpoint, query } = getResourceIdentifier( resourceName ); - + const prefix = getResourcePrefix( resourceName ); + const endpoint = typeEndpointMap[ prefix ]; + const query = getResourceIdentifier( resourceName ); + console.log( '::filtered:: read', resourceName, endpoint, query ); let apiPath = endpoint + stringifyQuery( query ); if ( swaggerEndpoints.indexOf( endpoint ) >= 0 ) { diff --git a/plugins/woocommerce-admin/client/wc-api/reports/stats/selectors.js b/plugins/woocommerce-admin/client/wc-api/reports/stats/selectors.js index 173bd64d346..c42825cbb43 100644 --- a/plugins/woocommerce-admin/client/wc-api/reports/stats/selectors.js +++ b/plugins/woocommerce-admin/client/wc-api/reports/stats/selectors.js @@ -12,18 +12,19 @@ import { getResourceName } from '../../utils'; import { DEFAULT_REQUIREMENT } from '../../constants'; const getReportStats = ( getResource, requireResource ) => ( - endpoint, + type, query = {}, requirement = DEFAULT_REQUIREMENT ) => { - const resourceName = getResourceName( 'report-stats-query', { endpoint, query } ); + const resourceName = getResourceName( `report-stats-query-${ type }`, query ); + console.log( 'getReportStats', type, query, resourceName ); const data = requireResource( requirement, resourceName ) || {}; return data; }; -const isReportStatsRequesting = getResource => ( endpoint, query = {} ) => { - const resourceName = getResourceName( 'report-stats-query', { endpoint, query } ); +const isReportStatsRequesting = getResource => ( type, query = {} ) => { + const resourceName = getResourceName( `report-stats-query-${ type }`, query ); const { lastRequested, lastReceived } = getResource( resourceName ); if ( isNil( lastRequested ) ) { @@ -37,8 +38,8 @@ const isReportStatsRequesting = getResource => ( endpoint, query = {} ) => { return lastRequested > lastReceived; }; -const isReportStatsError = getResource => ( endpoint, query = {} ) => { - const resourceName = getResourceName( 'report-stats-query', { endpoint, query } ); +const isReportStatsError = getResource => ( type, query = {} ) => { + const resourceName = getResourceName( `report-stats-query-${ type }`, query ); return getResource( resourceName ).error; }; diff --git a/plugins/woocommerce-admin/client/wc-api/utils.js b/plugins/woocommerce-admin/client/wc-api/utils.js index c94419b153d..c0abbd3fa16 100644 --- a/plugins/woocommerce-admin/client/wc-api/utils.js +++ b/plugins/woocommerce-admin/client/wc-api/utils.js @@ -1,26 +1,16 @@ /** @format */ -/** - * External dependencies - */ -import { isObject } from 'lodash'; export function getResourceName( prefix, identifier ) { - const keyList = []; - Object.keys( identifier ).forEach( key => { - keyList.push( key ); - - // whitelist nested object keys - if ( isObject( identifier[ key ] ) ) { - Array.prototype.push.apply( keyList, Object.keys( identifier[ key ] ) ); - } - } ); - - const identifierString = JSON.stringify( identifier, keyList.sort() ); + const identifierString = JSON.stringify( identifier, Object.keys( identifier ).sort() ); return `${ prefix }:${ identifierString }`; } +export function getResourcePrefix( resourceName ) { + return resourceName.substring( 0, resourceName.indexOf( ':' ) ); +} + export function isResourcePrefix( resourceName, prefix ) { - const resourcePrefix = resourceName.substring( 0, resourceName.indexOf( ':' ) ); + const resourcePrefix = getResourcePrefix( resourceName ); return resourcePrefix === prefix; } From 47703fa9b90b61d4723ca84a964f0529a5e877dd Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Wed, 5 Dec 2018 10:05:31 -0700 Subject: [PATCH 07/14] Remove debug logging, restore RevenueReportTable component. --- .../client/analytics/report/revenue/index.js | 4 ++-- .../client/analytics/report/revenue/table.js | 2 +- .../client/wc-api/reports/stats/operations.js | 3 +-- .../client/wc-api/reports/stats/selectors.js | 1 - 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/plugins/woocommerce-admin/client/analytics/report/revenue/index.js b/plugins/woocommerce-admin/client/analytics/report/revenue/index.js index cc381b9ffef..d5e70212068 100644 --- a/plugins/woocommerce-admin/client/analytics/report/revenue/index.js +++ b/plugins/woocommerce-admin/client/analytics/report/revenue/index.js @@ -17,7 +17,7 @@ import { charts } from './config'; import getSelectedChart from 'lib/get-selected-chart'; import ReportChart from 'analytics/components/report-chart'; import ReportSummary from 'analytics/components/report-summary'; -// import RevenueReportTable from './table'; +import RevenueReportTable from './table'; export default class RevenueReport extends Component { render() { @@ -39,7 +39,7 @@ export default class RevenueReport extends Component { query={ query } selectedChart={ getSelectedChart( query.chart, charts ) } /> - { /* */ } + ); } diff --git a/plugins/woocommerce-admin/client/analytics/report/revenue/table.js b/plugins/woocommerce-admin/client/analytics/report/revenue/table.js index 10ff6d1b5c4..a26ed9eaa69 100644 --- a/plugins/woocommerce-admin/client/analytics/report/revenue/table.js +++ b/plugins/woocommerce-admin/client/analytics/report/revenue/table.js @@ -6,7 +6,7 @@ import { __, _n } from '@wordpress/i18n'; import { Component } from '@wordpress/element'; import { format as formatDate } from '@wordpress/date'; import { compose } from '@wordpress/compose'; -import { get, map } from 'lodash'; +import { get } from 'lodash'; /** * WooCommerce dependencies diff --git a/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js b/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js index e515cae2fa1..19d54b14cf5 100644 --- a/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js +++ b/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js @@ -30,7 +30,6 @@ const typeEndpointMap = { }; function read( resourceNames, fetch = apiFetch ) { - console.log( 'stats read', resourceNames ); const filteredNames = resourceNames.filter( name => { const prefix = getResourcePrefix( name ); return Boolean( typeEndpointMap[ prefix ] ); @@ -40,7 +39,7 @@ function read( resourceNames, fetch = apiFetch ) { const prefix = getResourcePrefix( resourceName ); const endpoint = typeEndpointMap[ prefix ]; const query = getResourceIdentifier( resourceName ); - console.log( '::filtered:: read', resourceName, endpoint, query ); + let apiPath = endpoint + stringifyQuery( query ); if ( swaggerEndpoints.indexOf( endpoint ) >= 0 ) { diff --git a/plugins/woocommerce-admin/client/wc-api/reports/stats/selectors.js b/plugins/woocommerce-admin/client/wc-api/reports/stats/selectors.js index c42825cbb43..fe819d2aa66 100644 --- a/plugins/woocommerce-admin/client/wc-api/reports/stats/selectors.js +++ b/plugins/woocommerce-admin/client/wc-api/reports/stats/selectors.js @@ -17,7 +17,6 @@ const getReportStats = ( getResource, requireResource ) => ( requirement = DEFAULT_REQUIREMENT ) => { const resourceName = getResourceName( `report-stats-query-${ type }`, query ); - console.log( 'getReportStats', type, query, resourceName ); const data = requireResource( requirement, resourceName ) || {}; return data; From 16f24a31bacb7357b7b3713a09ecb41ddb618d72 Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Wed, 5 Dec 2018 10:10:54 -0700 Subject: [PATCH 08/14] 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 ), ]; }, From d28eb0bcc9cb6dcbcd2dac17c315c52cb839a3b0 Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Wed, 5 Dec 2018 11:39:51 -0700 Subject: [PATCH 09/14] =?UTF-8?q?Consider=20pre-flight=20wc-api=20requests?= =?UTF-8?q?=20to=20be=20=E2=80=9Crequesting=E2=80=9D.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/woocommerce-admin/client/wc-api/orders/selectors.js | 6 +----- .../client/wc-api/reports/items/selectors.js | 6 +----- .../client/wc-api/reports/stats/selectors.js | 6 +----- 3 files changed, 3 insertions(+), 15 deletions(-) diff --git a/plugins/woocommerce-admin/client/wc-api/orders/selectors.js b/plugins/woocommerce-admin/client/wc-api/orders/selectors.js index e1b30b0b077..c47e91f7a44 100644 --- a/plugins/woocommerce-admin/client/wc-api/orders/selectors.js +++ b/plugins/woocommerce-admin/client/wc-api/orders/selectors.js @@ -33,11 +33,7 @@ const isGetOrdersRequesting = getResource => ( query = {} ) => { const resourceName = getResourceName( 'order-query', query ); const { lastRequested, lastReceived } = getResource( resourceName ); - if ( isNil( lastRequested ) ) { - return false; - } - - if ( isNil( lastReceived ) ) { + if ( isNil( lastRequested ) || isNil( lastReceived ) ) { return true; } diff --git a/plugins/woocommerce-admin/client/wc-api/reports/items/selectors.js b/plugins/woocommerce-admin/client/wc-api/reports/items/selectors.js index 0ae60913b0b..f96574b82e9 100644 --- a/plugins/woocommerce-admin/client/wc-api/reports/items/selectors.js +++ b/plugins/woocommerce-admin/client/wc-api/reports/items/selectors.js @@ -26,11 +26,7 @@ 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 ) ) { + if ( isNil( lastRequested ) || isNil( lastReceived ) ) { return true; } diff --git a/plugins/woocommerce-admin/client/wc-api/reports/stats/selectors.js b/plugins/woocommerce-admin/client/wc-api/reports/stats/selectors.js index fe819d2aa66..8aa827ccf9a 100644 --- a/plugins/woocommerce-admin/client/wc-api/reports/stats/selectors.js +++ b/plugins/woocommerce-admin/client/wc-api/reports/stats/selectors.js @@ -26,11 +26,7 @@ const isReportStatsRequesting = getResource => ( type, query = {} ) => { const resourceName = getResourceName( `report-stats-query-${ type }`, query ); const { lastRequested, lastReceived } = getResource( resourceName ); - if ( isNil( lastRequested ) ) { - return false; - } - - if ( isNil( lastReceived ) ) { + if ( isNil( lastRequested ) || isNil( lastReceived ) ) { return true; } From 7aeb69916a7f436907d7516dfb2643e44d083957 Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Wed, 5 Dec 2018 12:10:43 -0700 Subject: [PATCH 10/14] Fix swagger hub requests from wc-api (url vs. path). --- .../client/wc-api/reports/items/operations.js | 14 ++++++------- .../client/wc-api/reports/stats/operations.js | 20 +++++++++---------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/plugins/woocommerce-admin/client/wc-api/reports/items/operations.js b/plugins/woocommerce-admin/client/wc-api/reports/items/operations.js index b27ffb37625..61cd06478c3 100644 --- a/plugins/woocommerce-admin/client/wc-api/reports/items/operations.js +++ b/plugins/woocommerce-admin/client/wc-api/reports/items/operations.js @@ -39,18 +39,18 @@ function read( resourceNames, fetch = apiFetch ) { const endpoint = typeEndpointMap[ prefix ]; const query = getResourceIdentifier( resourceName ); - let apiPath = NAMESPACE + '/reports/' + endpoint + stringifyQuery( query ); + const fetchArgs = { + parse: false, + }; if ( swaggerEndpoints.indexOf( endpoint ) >= 0 ) { - apiPath = SWAGGERNAMESPACE + 'reports/' + endpoint + stringifyQuery( query ); + fetchArgs.url = SWAGGERNAMESPACE + 'reports/' + endpoint + stringifyQuery( query ); + } else { + fetchArgs.path = NAMESPACE + '/reports/' + endpoint + stringifyQuery( query ); } try { - const response = await fetch( { - parse: false, - path: apiPath, - } ); - + const response = await fetch( fetchArgs ); const report = await response.json(); // TODO: exclude these if using swagger? const totalResults = parseInt( response.headers.get( 'x-wp-total' ) ); diff --git a/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js b/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js index 19d54b14cf5..7529a103a64 100644 --- a/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js +++ b/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js @@ -40,22 +40,20 @@ function read( resourceNames, fetch = apiFetch ) { const endpoint = typeEndpointMap[ prefix ]; const query = getResourceIdentifier( resourceName ); - let apiPath = endpoint + stringifyQuery( query ); + const fetchArgs = { + parse: false, + }; if ( swaggerEndpoints.indexOf( endpoint ) >= 0 ) { - apiPath = SWAGGERNAMESPACE + 'reports/' + endpoint + '/stats' + stringifyQuery( query ); - } - - if ( statEndpoints.indexOf( endpoint ) >= 0 ) { - apiPath = NAMESPACE + '/reports/' + endpoint + '/stats' + stringifyQuery( query ); + fetchArgs.url = SWAGGERNAMESPACE + 'reports/' + endpoint + '/stats' + stringifyQuery( query ); + } else if ( statEndpoints.indexOf( endpoint ) >= 0 ) { + fetchArgs.path = NAMESPACE + '/reports/' + endpoint + '/stats' + stringifyQuery( query ); + } else { + fetchArgs.path = endpoint + stringifyQuery( query ); } try { - const response = await fetch( { - parse: false, - path: apiPath, - } ); - + const response = await fetch( fetchArgs ); const report = await response.json(); // TODO: exclude these if using swagger? const totalResults = parseInt( response.headers.get( 'x-wp-total' ) ); From e3362ddcde0131f29d1573f7d4ec889af6f7fc6f Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Wed, 5 Dec 2018 16:05:40 -0700 Subject: [PATCH 11/14] Update report utils tests to use wc-api. --- .../client/store/reports/test/utils.js | 72 +++++++++---------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/plugins/woocommerce-admin/client/store/reports/test/utils.js b/plugins/woocommerce-admin/client/store/reports/test/utils.js index ce7511522f5..73581a76472 100644 --- a/plugins/woocommerce-admin/client/store/reports/test/utils.js +++ b/plugins/woocommerce-admin/client/store/reports/test/utils.js @@ -62,29 +62,29 @@ describe( 'getReportChartData()', () => { }; beforeAll( () => { - select( 'wc-admin' ).getReportStats = jest.fn().mockReturnValue( {} ); - select( 'wc-admin' ).isReportStatsRequesting = jest.fn().mockReturnValue( false ); - select( 'wc-admin' ).isReportStatsError = jest.fn().mockReturnValue( false ); + select( 'wc-api' ).getReportStats = jest.fn().mockReturnValue( {} ); + select( 'wc-api' ).isReportStatsRequesting = jest.fn().mockReturnValue( false ); + select( 'wc-api' ).isReportStatsError = jest.fn().mockReturnValue( false ); } ); afterAll( () => { - select( 'wc-admin' ).getReportStats.mockRestore(); - select( 'wc-admin' ).isReportStatsRequesting.mockRestore(); - select( 'wc-admin' ).isReportStatsError.mockRestore(); + select( 'wc-api' ).getReportStats.mockRestore(); + select( 'wc-api' ).isReportStatsRequesting.mockRestore(); + select( 'wc-api' ).isReportStatsError.mockRestore(); } ); function setGetReportStats( func ) { - select( 'wc-admin' ).getReportStats.mockImplementation( ( ...args ) => func( ...args ) ); + select( 'wc-api' ).getReportStats.mockImplementation( ( ...args ) => func( ...args ) ); } function setIsReportStatsRequesting( func ) { - select( 'wc-admin' ).isReportStatsRequesting.mockImplementation( ( ...args ) => + select( 'wc-api' ).isReportStatsRequesting.mockImplementation( ( ...args ) => func( ...args ) ); } function setIsReportStatsError( func ) { - select( 'wc-admin' ).isReportStatsError.mockImplementation( ( ...args ) => func( ...args ) ); + select( 'wc-api' ).isReportStatsError.mockImplementation( ( ...args ) => func( ...args ) ); } it( 'returns isRequesting if first request is in progress', () => { @@ -262,29 +262,29 @@ describe( 'getSummaryNumbers()', () => { }; beforeAll( () => { - select( 'wc-admin' ).getReportStats = jest.fn().mockReturnValue( {} ); - select( 'wc-admin' ).isReportStatsRequesting = jest.fn().mockReturnValue( false ); - select( 'wc-admin' ).isReportStatsError = jest.fn().mockReturnValue( false ); + select( 'wc-api' ).getReportStats = jest.fn().mockReturnValue( {} ); + select( 'wc-api' ).isReportStatsRequesting = jest.fn().mockReturnValue( false ); + select( 'wc-api' ).isReportStatsError = jest.fn().mockReturnValue( false ); } ); afterAll( () => { - select( 'wc-admin' ).getReportStats.mockRestore(); - select( 'wc-admin' ).isReportStatsRequesting.mockRestore(); - select( 'wc-admin' ).isReportStatsError.mockRestore(); + select( 'wc-api' ).getReportStats.mockRestore(); + select( 'wc-api' ).isReportStatsRequesting.mockRestore(); + select( 'wc-api' ).isReportStatsError.mockRestore(); } ); function setGetReportStats( func ) { - select( 'wc-admin' ).getReportStats.mockImplementation( ( ...args ) => func( ...args ) ); + select( 'wc-api' ).getReportStats.mockImplementation( ( ...args ) => func( ...args ) ); } function setIsReportStatsRequesting( func ) { - select( 'wc-admin' ).isReportStatsRequesting.mockImplementation( ( ...args ) => + select( 'wc-api' ).isReportStatsRequesting.mockImplementation( ( ...args ) => func( ...args ) ); } function setIsReportStatsError( func ) { - select( 'wc-admin' ).isReportStatsError.mockImplementation( ( ...args ) => func( ...args ) ); + select( 'wc-api' ).isReportStatsError.mockImplementation( ( ...args ) => func( ...args ) ); } it( 'returns isRequesting if a request is in progress', () => { @@ -460,29 +460,29 @@ describe( 'getReportTableData()', () => { }; beforeAll( () => { - select( 'wc-admin' ).getReportItems = jest.fn().mockReturnValue( {} ); - select( 'wc-admin' ).isGetReportItemsRequesting = jest.fn().mockReturnValue( false ); - select( 'wc-admin' ).isGetReportItemsError = jest.fn().mockReturnValue( false ); + select( 'wc-api' ).getReportItems = jest.fn().mockReturnValue( {} ); + select( 'wc-api' ).isGetReportItemsRequesting = jest.fn().mockReturnValue( false ); + select( 'wc-api' ).isGetReportItemsError = jest.fn().mockReturnValue( false ); } ); afterAll( () => { - select( 'wc-admin' ).getReportItems.mockRestore(); - select( 'wc-admin' ).isGetReportItemsRequesting.mockRestore(); - select( 'wc-admin' ).isGetReportItemsError.mockRestore(); + select( 'wc-api' ).getReportItems.mockRestore(); + select( 'wc-api' ).isGetReportItemsRequesting.mockRestore(); + select( 'wc-api' ).isGetReportItemsError.mockRestore(); } ); function setGetReportItems( func ) { - select( 'wc-admin' ).getReportItems.mockImplementation( ( ...args ) => func( ...args ) ); + select( 'wc-api' ).getReportItems.mockImplementation( ( ...args ) => func( ...args ) ); } function setIsGetReportItemsRequesting( func ) { - select( 'wc-admin' ).isGetReportItemsRequesting.mockImplementation( ( ...args ) => + select( 'wc-api' ).isGetReportItemsRequesting.mockImplementation( ( ...args ) => func( ...args ) ); } function setIsGetReportItemsError( func ) { - select( 'wc-admin' ).isGetReportItemsError.mockImplementation( ( ...args ) => func( ...args ) ); + select( 'wc-api' ).isGetReportItemsError.mockImplementation( ( ...args ) => func( ...args ) ); } it( 'returns isRequesting if a request is in progress', () => { @@ -491,12 +491,12 @@ describe( 'getReportTableData()', () => { const result = getReportTableData( 'coupons', query, select ); expect( result ).toEqual( { ...response, query, isRequesting: true } ); - expect( select( 'wc-admin' ).getReportItems ).toHaveBeenLastCalledWith( 'coupons', query ); - expect( select( 'wc-admin' ).isGetReportItemsRequesting ).toHaveBeenLastCalledWith( + expect( select( 'wc-api' ).getReportItems ).toHaveBeenLastCalledWith( 'coupons', query ); + expect( select( 'wc-api' ).isGetReportItemsRequesting ).toHaveBeenLastCalledWith( 'coupons', query ); - expect( select( 'wc-admin' ).isGetReportItemsError ).toHaveBeenCalledTimes( 0 ); + expect( select( 'wc-api' ).isGetReportItemsError ).toHaveBeenCalledTimes( 0 ); } ); it( 'returns isError if request errors', () => { @@ -506,12 +506,12 @@ describe( 'getReportTableData()', () => { const result = getReportTableData( 'coupons', query, select ); expect( result ).toEqual( { ...response, query, isError: true } ); - expect( select( 'wc-admin' ).getReportItems ).toHaveBeenLastCalledWith( 'coupons', query ); - expect( select( 'wc-admin' ).isGetReportItemsRequesting ).toHaveBeenLastCalledWith( + expect( select( 'wc-api' ).getReportItems ).toHaveBeenLastCalledWith( 'coupons', query ); + expect( select( 'wc-api' ).isGetReportItemsRequesting ).toHaveBeenLastCalledWith( 'coupons', query ); - expect( select( 'wc-admin' ).isGetReportItemsError ).toHaveBeenLastCalledWith( + expect( select( 'wc-api' ).isGetReportItemsError ).toHaveBeenLastCalledWith( 'coupons', query ); @@ -526,12 +526,12 @@ describe( 'getReportTableData()', () => { const result = getReportTableData( 'coupons', query, select ); expect( result ).toEqual( { ...response, query, items } ); - expect( select( 'wc-admin' ).getReportItems ).toHaveBeenLastCalledWith( 'coupons', query ); - expect( select( 'wc-admin' ).isGetReportItemsRequesting ).toHaveBeenLastCalledWith( + expect( select( 'wc-api' ).getReportItems ).toHaveBeenLastCalledWith( 'coupons', query ); + expect( select( 'wc-api' ).isGetReportItemsRequesting ).toHaveBeenLastCalledWith( 'coupons', query ); - expect( select( 'wc-admin' ).isGetReportItemsError ).toHaveBeenLastCalledWith( + expect( select( 'wc-api' ).isGetReportItemsError ).toHaveBeenLastCalledWith( 'coupons', query ); From be74e22a01b345500f14ce020d41d744752495af Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Wed, 5 Dec 2018 16:12:34 -0700 Subject: [PATCH 12/14] Remove unnecessary variable and rename Report Items selectors for consistency. --- .../client/store/reports/test/utils.js | 38 +++++++++---------- .../client/store/reports/utils.js | 6 +-- .../client/wc-api/reports/items/selectors.js | 12 +++--- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/plugins/woocommerce-admin/client/store/reports/test/utils.js b/plugins/woocommerce-admin/client/store/reports/test/utils.js index 73581a76472..6cc0881d13f 100644 --- a/plugins/woocommerce-admin/client/store/reports/test/utils.js +++ b/plugins/woocommerce-admin/client/store/reports/test/utils.js @@ -461,57 +461,57 @@ describe( 'getReportTableData()', () => { beforeAll( () => { select( 'wc-api' ).getReportItems = jest.fn().mockReturnValue( {} ); - select( 'wc-api' ).isGetReportItemsRequesting = jest.fn().mockReturnValue( false ); - select( 'wc-api' ).isGetReportItemsError = jest.fn().mockReturnValue( false ); + select( 'wc-api' ).isReportItemsRequesting = jest.fn().mockReturnValue( false ); + select( 'wc-api' ).isReportItemsError = jest.fn().mockReturnValue( false ); } ); afterAll( () => { select( 'wc-api' ).getReportItems.mockRestore(); - select( 'wc-api' ).isGetReportItemsRequesting.mockRestore(); - select( 'wc-api' ).isGetReportItemsError.mockRestore(); + select( 'wc-api' ).isReportItemsRequesting.mockRestore(); + select( 'wc-api' ).isReportItemsError.mockRestore(); } ); function setGetReportItems( func ) { select( 'wc-api' ).getReportItems.mockImplementation( ( ...args ) => func( ...args ) ); } - function setIsGetReportItemsRequesting( func ) { - select( 'wc-api' ).isGetReportItemsRequesting.mockImplementation( ( ...args ) => + function setisReportItemsRequesting( func ) { + select( 'wc-api' ).isReportItemsRequesting.mockImplementation( ( ...args ) => func( ...args ) ); } - function setIsGetReportItemsError( func ) { - select( 'wc-api' ).isGetReportItemsError.mockImplementation( ( ...args ) => func( ...args ) ); + function setisReportItemsError( func ) { + select( 'wc-api' ).isReportItemsError.mockImplementation( ( ...args ) => func( ...args ) ); } it( 'returns isRequesting if a request is in progress', () => { - setIsGetReportItemsRequesting( () => true ); + setisReportItemsRequesting( () => true ); const result = getReportTableData( 'coupons', query, select ); expect( result ).toEqual( { ...response, query, isRequesting: true } ); expect( select( 'wc-api' ).getReportItems ).toHaveBeenLastCalledWith( 'coupons', query ); - expect( select( 'wc-api' ).isGetReportItemsRequesting ).toHaveBeenLastCalledWith( + expect( select( 'wc-api' ).isReportItemsRequesting ).toHaveBeenLastCalledWith( 'coupons', query ); - expect( select( 'wc-api' ).isGetReportItemsError ).toHaveBeenCalledTimes( 0 ); + expect( select( 'wc-api' ).isReportItemsError ).toHaveBeenCalledTimes( 0 ); } ); it( 'returns isError if request errors', () => { - setIsGetReportItemsRequesting( () => false ); - setIsGetReportItemsError( () => true ); + setisReportItemsRequesting( () => false ); + setisReportItemsError( () => true ); const result = getReportTableData( 'coupons', query, select ); expect( result ).toEqual( { ...response, query, isError: true } ); expect( select( 'wc-api' ).getReportItems ).toHaveBeenLastCalledWith( 'coupons', query ); - expect( select( 'wc-api' ).isGetReportItemsRequesting ).toHaveBeenLastCalledWith( + expect( select( 'wc-api' ).isReportItemsRequesting ).toHaveBeenLastCalledWith( 'coupons', query ); - expect( select( 'wc-api' ).isGetReportItemsError ).toHaveBeenLastCalledWith( + expect( select( 'wc-api' ).isReportItemsError ).toHaveBeenLastCalledWith( 'coupons', query ); @@ -519,19 +519,19 @@ describe( 'getReportTableData()', () => { it( 'returns results after queries finish', () => { const items = [ { id: 1 }, { id: 2 }, { id: 3 } ]; - setIsGetReportItemsRequesting( () => false ); - setIsGetReportItemsError( () => false ); + setisReportItemsRequesting( () => false ); + setisReportItemsError( () => false ); setGetReportItems( () => items ); const result = getReportTableData( 'coupons', query, select ); expect( result ).toEqual( { ...response, query, items } ); expect( select( 'wc-api' ).getReportItems ).toHaveBeenLastCalledWith( 'coupons', query ); - expect( select( 'wc-api' ).isGetReportItemsRequesting ).toHaveBeenLastCalledWith( + expect( select( 'wc-api' ).isReportItemsRequesting ).toHaveBeenLastCalledWith( 'coupons', query ); - expect( select( 'wc-api' ).isGetReportItemsError ).toHaveBeenLastCalledWith( + expect( select( 'wc-api' ).isReportItemsError ).toHaveBeenLastCalledWith( 'coupons', query ); diff --git a/plugins/woocommerce-admin/client/store/reports/utils.js b/plugins/woocommerce-admin/client/store/reports/utils.js index a661a3ee558..63b1d4af8ac 100644 --- a/plugins/woocommerce-admin/client/store/reports/utils.js +++ b/plugins/woocommerce-admin/client/store/reports/utils.js @@ -296,7 +296,7 @@ export function getReportTableQuery( urlQuery, query ) { * @return {Object} Object Table data response */ export function getReportTableData( endpoint, urlQuery, select, query = {} ) { - const { getReportItems, isGetReportItemsRequesting, isGetReportItemsError } = select( + const { getReportItems, isReportItemsRequesting, isReportItemsError } = select( 'wc-api' ); @@ -309,9 +309,9 @@ export function getReportTableData( endpoint, urlQuery, select, query = {} ) { }; const items = getReportItems( endpoint, tableQuery ); - if ( isGetReportItemsRequesting( endpoint, tableQuery ) ) { + if ( isReportItemsRequesting( endpoint, tableQuery ) ) { return { ...response, isRequesting: true }; - } else if ( isGetReportItemsError( endpoint, tableQuery ) ) { + } else if ( isReportItemsError( endpoint, tableQuery ) ) { return { ...response, isError: true }; } diff --git a/plugins/woocommerce-admin/client/wc-api/reports/items/selectors.js b/plugins/woocommerce-admin/client/wc-api/reports/items/selectors.js index f96574b82e9..9fa8333c986 100644 --- a/plugins/woocommerce-admin/client/wc-api/reports/items/selectors.js +++ b/plugins/woocommerce-admin/client/wc-api/reports/items/selectors.js @@ -17,12 +17,10 @@ const getReportItems = ( getResource, requireResource ) => ( requirement = DEFAULT_REQUIREMENT ) => { const resourceName = getResourceName( `report-items-query-${ type }`, query ); - const data = requireResource( requirement, resourceName ) || {}; - - return data; + return requireResource( requirement, resourceName ) || {}; }; -const isGetReportItemsRequesting = getResource => ( type, query = {} ) => { +const isReportItemsRequesting = getResource => ( type, query = {} ) => { const resourceName = getResourceName( `report-items-query-${ type }`, query ); const { lastRequested, lastReceived } = getResource( resourceName ); @@ -33,13 +31,13 @@ const isGetReportItemsRequesting = getResource => ( type, query = {} ) => { return lastRequested > lastReceived; }; -const isGetReportItemsError = getResource => ( type, query = {} ) => { +const isReportItemsError = getResource => ( type, query = {} ) => { const resourceName = getResourceName( `report-items-query-${ type }`, query ); return getResource( resourceName ).error; }; export default { getReportItems, - isGetReportItemsRequesting, - isGetReportItemsError, + isReportItemsRequesting, + isReportItemsError, }; From d0066f0a6066b9fab60f3f2ce845ac8039f3c8c0 Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Wed, 5 Dec 2018 16:55:13 -0700 Subject: [PATCH 13/14] =?UTF-8?q?Add=20support=20for=20=E2=80=9Cvariations?= =?UTF-8?q?=E2=80=9D=20Report=20Items=20type.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../woocommerce-admin/client/wc-api/reports/items/operations.js | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/woocommerce-admin/client/wc-api/reports/items/operations.js b/plugins/woocommerce-admin/client/wc-api/reports/items/operations.js index 61cd06478c3..ca8e493d4d6 100644 --- a/plugins/woocommerce-admin/client/wc-api/reports/items/operations.js +++ b/plugins/woocommerce-admin/client/wc-api/reports/items/operations.js @@ -26,6 +26,7 @@ const typeEndpointMap = { 'report-items-query-categories': 'categories', 'report-items-query-coupons': 'coupons', 'report-items-query-taxes': 'taxes', + 'report-items-query-variations': 'variations', }; function read( resourceNames, fetch = apiFetch ) { From a8247e8e7e4cbcb3a19ef3e5b620fe66e351617c Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Mon, 10 Dec 2018 08:19:28 -0700 Subject: [PATCH 14/14] Remove defunct todo comment about swagger hub endpoint pagination headers. --- .../woocommerce-admin/client/wc-api/reports/items/operations.js | 1 - .../woocommerce-admin/client/wc-api/reports/stats/operations.js | 1 - 2 files changed, 2 deletions(-) diff --git a/plugins/woocommerce-admin/client/wc-api/reports/items/operations.js b/plugins/woocommerce-admin/client/wc-api/reports/items/operations.js index ca8e493d4d6..f10bdc8dd67 100644 --- a/plugins/woocommerce-admin/client/wc-api/reports/items/operations.js +++ b/plugins/woocommerce-admin/client/wc-api/reports/items/operations.js @@ -53,7 +53,6 @@ function read( resourceNames, fetch = apiFetch ) { try { const response = await fetch( fetchArgs ); 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' ) ); diff --git a/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js b/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js index 7529a103a64..ee3d67adb54 100644 --- a/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js +++ b/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js @@ -55,7 +55,6 @@ function read( resourceNames, fetch = apiFetch ) { try { const response = await fetch( fetchArgs ); 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' ) );