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(
withSelect( select => {
const { getCategories, getCategoriesError, isGetCategoriesRequesting } = select( 'wc-api' );
const tableQuery = {
per_page: -1,
};
const categories = getCategories();
const isError = Boolean( getCategoriesError() );
const isRequesting = isGetCategoriesRequesting();
const categories = getCategories( tableQuery );
const isError = Boolean( getCategoriesError( tableQuery ) );
const isRequesting = isGetCategoriesRequesting( tableQuery );
return { categories, isError, isRequesting };
} )

View File

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

View File

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

View File

@ -29,10 +29,6 @@ class WC_Admin_Api_Init {
// 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 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;
}
/**
* 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();