From a1bbd22ca26e0e95f92ce3ff6e2ceda7d7ca05da Mon Sep 17 00:00:00 2001 From: Justin Shreve Date: Wed, 6 Feb 2019 11:24:40 -0500 Subject: [PATCH] Update product details report to use getItems (https://github.com/woocommerce/woocommerce-admin/pull/1488) --- .../client/analytics/report/products/index.js | 14 ++--- .../client/wc-api/products/index.js | 11 ---- .../client/wc-api/products/operations.js | 51 ------------------- .../client/wc-api/products/selectors.js | 44 ---------------- .../client/wc-api/wc-api-spec.js | 3 -- 5 files changed, 8 insertions(+), 115 deletions(-) delete mode 100644 plugins/woocommerce-admin/client/wc-api/products/index.js delete mode 100644 plugins/woocommerce-admin/client/wc-api/products/operations.js delete mode 100644 plugins/woocommerce-admin/client/wc-api/products/selectors.js diff --git a/plugins/woocommerce-admin/client/analytics/report/products/index.js b/plugins/woocommerce-admin/client/analytics/report/products/index.js index b4214e92e13..2f787f6d189 100644 --- a/plugins/woocommerce-admin/client/analytics/report/products/index.js +++ b/plugins/woocommerce-admin/client/analytics/report/products/index.js @@ -129,15 +129,17 @@ ProductsReport.propTypes = { export default compose( withSelect( ( select, props ) => { const { query } = props; - const { getProducts, isGetProductsRequesting, getProductsError } = select( 'wc-api' ); + const { getItems, isGetItemsRequesting, getItemsError } = select( 'wc-api' ); const isSingleProductView = query.products && 1 === query.products.split( ',' ).length; if ( isSingleProductView ) { - const includeArgs = { include: query.products }; + const productId = parseInt( query.products ); + const includeArgs = { include: productId }; // TODO Look at similar usage to populate tags in the Search component. - const products = getProducts( includeArgs ); - const isVariable = products[ 0 ] && 'variable' === products[ 0 ].type; - const isProductsRequesting = isGetProductsRequesting( includeArgs ); - const isProductsError = getProductsError( includeArgs ); + const products = getItems( 'products', includeArgs ); + const isVariable = + products && products.get( productId ) && 'variable' === products.get( productId ).type; + const isProductsRequesting = isGetItemsRequesting( 'products', includeArgs ); + const isProductsError = Boolean( getItemsError( 'products', includeArgs ) ); return { query: { ...query, diff --git a/plugins/woocommerce-admin/client/wc-api/products/index.js b/plugins/woocommerce-admin/client/wc-api/products/index.js deleted file mode 100644 index cd0cff2f807..00000000000 --- a/plugins/woocommerce-admin/client/wc-api/products/index.js +++ /dev/null @@ -1,11 +0,0 @@ -/** @format */ -/** - * Internal dependencies - */ -import operations from './operations'; -import selectors from './selectors'; - -export default { - operations, - selectors, -}; diff --git a/plugins/woocommerce-admin/client/wc-api/products/operations.js b/plugins/woocommerce-admin/client/wc-api/products/operations.js deleted file mode 100644 index c3e4e00ee19..00000000000 --- a/plugins/woocommerce-admin/client/wc-api/products/operations.js +++ /dev/null @@ -1,51 +0,0 @@ -/** @format */ - -/** - * External dependencies - */ -import apiFetch from '@wordpress/api-fetch'; - -/** - * WooCommerce dependencies - */ -import { stringifyQuery } from '@woocommerce/navigation'; - -/** - * Internal dependencies - */ -import { isResourcePrefix, getResourceIdentifier, getResourceName } from '../utils'; - -function read( resourceNames, fetch = apiFetch ) { - const filteredNames = resourceNames.filter( name => isResourcePrefix( name, 'product-query' ) ); - - return filteredNames.map( async resourceName => { - const query = getResourceIdentifier( resourceName ); - const url = `/wc/v3/products${ stringifyQuery( query ) }`; - - try { - const products = await fetch( { - path: url, - } ); - - const ids = products.map( product => product.id ); - const productResources = products.reduce( ( resources, product ) => { - resources[ getResourceName( 'product', product.id ) ] = { data: product }; - return resources; - }, {} ); - - return { - [ resourceName ]: { - data: ids, - totalCount: ids.length, - }, - ...productResources, - }; - } catch ( error ) { - return { [ resourceName ]: { error } }; - } - } ); -} - -export default { - read, -}; diff --git a/plugins/woocommerce-admin/client/wc-api/products/selectors.js b/plugins/woocommerce-admin/client/wc-api/products/selectors.js deleted file mode 100644 index c6b1fbdd3c5..00000000000 --- a/plugins/woocommerce-admin/client/wc-api/products/selectors.js +++ /dev/null @@ -1,44 +0,0 @@ -/** @format */ - -/** - * External dependencies - */ -import { isNil } from 'lodash'; - -/** - * Internal dependencies - */ -import { getResourceName } from '../utils'; -import { DEFAULT_REQUIREMENT } from '../constants'; - -const getProducts = ( getResource, requireResource ) => ( - query = {}, - requirement = DEFAULT_REQUIREMENT -) => { - const resourceName = getResourceName( 'product-query', query ); - const ids = requireResource( requirement, resourceName ).data || []; - const products = ids.map( id => getResource( getResourceName( 'product', id ) ).data || {} ); - return products; -}; - -const getProductsError = getResource => ( query = {} ) => { - const resourceName = getResourceName( 'product-query', query ); - return getResource( resourceName ).error; -}; - -const isGetProductsRequesting = getResource => ( query = {} ) => { - const resourceName = getResourceName( 'product-query', query ); - const { lastRequested, lastReceived } = getResource( resourceName ); - - if ( isNil( lastRequested ) || isNil( lastReceived ) ) { - return true; - } - - return lastRequested > lastReceived; -}; - -export default { - getProducts, - getProductsError, - isGetProductsRequesting, -}; 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 c9569a0bb25..c9e9132b6f1 100644 --- a/plugins/woocommerce-admin/client/wc-api/wc-api-spec.js +++ b/plugins/woocommerce-admin/client/wc-api/wc-api-spec.js @@ -5,7 +5,6 @@ */ import items from './items'; import notes from './notes'; -import products from './products'; import reportItems from './reports/items'; import reportStats from './reports/stats'; import reviews from './reviews'; @@ -21,7 +20,6 @@ function createWcApiSpec() { selectors: { ...items.selectors, ...notes.selectors, - ...products.selectors, ...reportItems.selectors, ...reportStats.selectors, ...reviews.selectors, @@ -33,7 +31,6 @@ function createWcApiSpec() { return [ ...items.operations.read( resourceNames ), ...notes.operations.read( resourceNames ), - ...products.operations.read( resourceNames ), ...reportItems.operations.read( resourceNames ), ...reportStats.operations.read( resourceNames ), ...reviews.operations.read( resourceNames ),