Merge pull request woocommerce/woocommerce-admin#1954 from woocommerce/fix/page-page-not-working

getItems: modify parse flag by isUnboundedRequest
This commit is contained in:
Paul Sealock 2019-04-02 10:15:33 +13:00 committed by GitHub
commit 17ef1fc9c6
1 changed files with 23 additions and 3 deletions

View File

@ -36,16 +36,36 @@ function read( resourceNames, fetch = apiFetch ) {
const endpoint = typeEndpointMap[ prefix ]; const endpoint = typeEndpointMap[ prefix ];
const query = getResourceIdentifier( resourceName ); const query = getResourceIdentifier( resourceName );
const url = NAMESPACE + `/${ endpoint }${ stringifyQuery( query ) }`; const url = NAMESPACE + `/${ endpoint }${ stringifyQuery( query ) }`;
const isUnboundedRequest = -1 === query.per_page;
try { try {
const response = await fetch( { const response = await fetch( {
parse: false, /* eslint-disable max-len */
/**
* A false parse flag allows a full response including headers which are useful
* to determine totalCount. However, this invalidates an unbounded request, ie
* `per_page: -1` by skipping middleware in apiFetch.
*
* See the Gutenberg code for more:
* https://github.com/WordPress/gutenberg/blob/dee3dcf49028717b4af3164e3096bfe747c41ed2/packages/api-fetch/src/middlewares/fetch-all-middleware.js#L39-L45
*/
/* eslint-enable max-len */
parse: isUnboundedRequest,
path: url, path: url,
} ); } );
const items = await response.json(); let items;
let totalCount;
if ( isUnboundedRequest ) {
items = response;
totalCount = items.length;
} else {
items = await response.json();
totalCount = parseInt( response.headers.get( 'x-wp-total' ) );
}
const ids = items.map( item => item.id ); const ids = items.map( item => item.id );
const totalCount = parseInt( response.headers.get( 'x-wp-total' ) );
const itemResources = items.reduce( ( resources, item ) => { const itemResources = items.reduce( ( resources, item ) => {
resources[ getResourceName( `${ prefix }-item`, item.id ) ] = { data: item }; resources[ getResourceName( `${ prefix }-item`, item.id ) ] = { data: item };
return resources; return resources;