Update product details report to use getItems (https://github.com/woocommerce/woocommerce-admin/pull/1488)

This commit is contained in:
Justin Shreve 2019-02-06 11:24:40 -05:00 committed by GitHub
parent 81fcab5709
commit a1bbd22ca2
5 changed files with 8 additions and 115 deletions

View File

@ -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,

View File

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

View File

@ -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,
};

View File

@ -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,
};

View File

@ -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 ),