Use unbounded per_page limit in apiFetch for categories (https://github.com/woocommerce/woocommerce-admin/pull/1149)

This commit is contained in:
Joshua T Flowers 2018-12-26 10:37:02 +08:00 committed by GitHub
parent f88311b372
commit c747d3399e
4 changed files with 14 additions and 26 deletions

View File

@ -153,10 +153,13 @@ class CategoriesReportTable extends Component {
export default compose( export default compose(
withSelect( select => { withSelect( select => {
const { getCategories, getCategoriesError, isGetCategoriesRequesting } = select( 'wc-api' ); const { getCategories, getCategoriesError, isGetCategoriesRequesting } = select( 'wc-api' );
const tableQuery = {
per_page: -1,
};
const categories = getCategories(); const categories = getCategories( tableQuery );
const isError = Boolean( getCategoriesError() ); const isError = Boolean( getCategoriesError( tableQuery ) );
const isRequesting = isGetCategoriesRequesting(); const isRequesting = isGetCategoriesRequesting( tableQuery );
return { categories, isError, isRequesting }; return { categories, isError, isRequesting };
} ) } )

View File

@ -255,10 +255,13 @@ class ProductsReportTable extends Component {
export default compose( export default compose(
withSelect( select => { withSelect( select => {
const { getCategories, getCategoriesError, isGetCategoriesRequesting } = select( 'wc-api' ); const { getCategories, getCategoriesError, isGetCategoriesRequesting } = select( 'wc-api' );
const tableQuery = {
per_page: -1,
};
const categories = getCategories(); const categories = getCategories( tableQuery );
const isError = Boolean( getCategoriesError() ); const isError = Boolean( getCategoriesError( tableQuery ) );
const isRequesting = isGetCategoriesRequesting(); const isRequesting = isGetCategoriesRequesting( tableQuery );
return { categories, isError, isRequesting }; return { categories, isError, isRequesting };
} ) } )

View File

@ -23,13 +23,10 @@ function read( resourceNames, fetch = apiFetch ) {
const url = `/wc/v3/products/categories${ stringifyQuery( query ) }`; const url = `/wc/v3/products/categories${ stringifyQuery( query ) }`;
try { try {
const response = await fetch( { const categories = await fetch( {
parse: false,
path: url, path: url,
} ); } );
const categories = await response.json();
const totalCount = parseInt( response.headers.get( 'x-wp-total' ) );
const ids = categories.map( category => category.id ); const ids = categories.map( category => category.id );
const categoryResources = categories.reduce( ( resources, category ) => { const categoryResources = categories.reduce( ( resources, category ) => {
resources[ getResourceName( 'category', category.id ) ] = { data: category }; resources[ getResourceName( 'category', category.id ) ] = { data: category };
@ -39,7 +36,7 @@ function read( resourceNames, fetch = apiFetch ) {
return { return {
[ resourceName ]: { [ resourceName ]: {
data: ids, data: ids,
totalCount, totalCount: ids.length,
}, },
...categoryResources, ...categoryResources,
}; };

View File

@ -29,10 +29,6 @@ class WC_Admin_Api_Init {
// Initialize Orders data store class's static vars. // Initialize Orders data store class's static vars.
add_action( 'woocommerce_after_register_post_type', array( 'WC_Admin_Api_Init', 'orders_data_store_init' ), 20 ); add_action( 'woocommerce_after_register_post_type', array( 'WC_Admin_Api_Init', 'orders_data_store_init' ), 20 );
// Add taxonomy support for product categories.
add_filter( 'woocommerce_taxonomy_args_product_cat', array( 'WC_Admin_Api_Init', 'show_product_categories_in_rest' ) );
// Increase per_page limit in REST response.
add_filter( 'woocommerce_rest_product_cat_query', array( 'WC_Admin_Api_Init', 'increase_per_page_limit' ) );
} }
/** /**
@ -529,17 +525,6 @@ class WC_Admin_Api_Init {
return $args; return $args;
} }
/**
* Increase per page limit for product categories
*
* @param array $prepared_args Prepared arguments for query.
* @return array
*/
public static function increase_per_page_limit( $prepared_args ) {
$prepared_args['number'] = PHP_INT_MAX;
return $prepared_args;
}
} }
new WC_Admin_Api_Init(); new WC_Admin_Api_Init();