2019-04-12 15:47:29 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { addQueryArgs } from '@wordpress/url';
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
import { flatten, uniqBy } from 'lodash';
|
2019-08-27 15:25:32 +00:00
|
|
|
import { ENDPOINTS, IS_LARGE_CATALOG, LIMIT_TAGS } from '@woocommerce/block-settings';
|
2019-04-12 15:47:29 +00:00
|
|
|
|
2019-08-15 14:55:57 +00:00
|
|
|
const getProductsRequests = ( { selected = [], search = '', queryArgs = [] } ) => {
|
|
|
|
const defaultArgs = {
|
2019-08-17 09:14:11 +00:00
|
|
|
per_page: IS_LARGE_CATALOG ? 100 : -1,
|
2019-08-15 14:55:57 +00:00
|
|
|
catalog_visibility: 'any',
|
|
|
|
status: 'publish',
|
|
|
|
search,
|
|
|
|
orderby: 'title',
|
|
|
|
order: 'asc',
|
|
|
|
};
|
2019-04-12 15:47:29 +00:00
|
|
|
const requests = [
|
2019-08-15 14:55:57 +00:00
|
|
|
addQueryArgs(
|
|
|
|
ENDPOINTS.products,
|
|
|
|
{ ...defaultArgs, ...queryArgs }
|
|
|
|
),
|
2019-04-12 15:47:29 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// If we have a large catalog, we might not get all selected products in the first page.
|
2019-08-17 09:14:11 +00:00
|
|
|
if ( IS_LARGE_CATALOG && selected.length ) {
|
2019-04-12 15:47:29 +00:00
|
|
|
requests.push(
|
2019-08-07 14:47:01 +00:00
|
|
|
addQueryArgs( ENDPOINTS.products, {
|
2019-08-12 11:54:51 +00:00
|
|
|
catalog_visibility: 'any',
|
2019-04-12 15:47:29 +00:00
|
|
|
status: 'publish',
|
|
|
|
include: selected,
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return requests;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a promise that resolves to a list of products from the API.
|
|
|
|
*
|
2019-08-09 16:18:46 +00:00
|
|
|
* @param {Object} - A query object with the list of selected products and search term.
|
2019-04-12 15:47:29 +00:00
|
|
|
*/
|
2019-08-15 14:55:57 +00:00
|
|
|
export const getProducts = ( { selected = [], search = '', queryArgs = [] } ) => {
|
|
|
|
const requests = getProductsRequests( { selected, search, queryArgs } );
|
2019-04-12 15:47:29 +00:00
|
|
|
|
|
|
|
return Promise.all( requests.map( ( path ) => apiFetch( { path } ) ) ).then( ( data ) => {
|
|
|
|
return uniqBy( flatten( data ), 'id' );
|
|
|
|
} );
|
|
|
|
};
|
2019-07-09 13:42:22 +00:00
|
|
|
|
2019-08-02 11:56:53 +00:00
|
|
|
/**
|
|
|
|
* Get a promise that resolves to a product object from the API.
|
|
|
|
*
|
2019-08-15 09:36:24 +00:00
|
|
|
* @param {number} productId Id of the product to retrieve.
|
2019-08-02 11:56:53 +00:00
|
|
|
*/
|
|
|
|
export const getProduct = ( productId ) => {
|
|
|
|
return apiFetch( {
|
2019-08-07 14:47:01 +00:00
|
|
|
path: `${ ENDPOINTS.products }/${ productId }`,
|
2019-08-02 11:56:53 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2019-07-09 13:42:22 +00:00
|
|
|
const getProductTagsRequests = ( { selected = [], search } ) => {
|
|
|
|
const requests = [
|
2019-08-07 14:47:01 +00:00
|
|
|
addQueryArgs( `${ ENDPOINTS.products }/tags`, {
|
2019-08-17 09:14:11 +00:00
|
|
|
per_page: LIMIT_TAGS ? 100 : -1,
|
|
|
|
orderby: LIMIT_TAGS ? 'count' : 'name',
|
|
|
|
order: LIMIT_TAGS ? 'desc' : 'asc',
|
2019-07-09 13:42:22 +00:00
|
|
|
search,
|
|
|
|
} ),
|
|
|
|
];
|
|
|
|
|
|
|
|
// If we have a large catalog, we might not get all selected products in the first page.
|
2019-08-17 09:14:11 +00:00
|
|
|
if ( LIMIT_TAGS && selected.length ) {
|
2019-07-09 13:42:22 +00:00
|
|
|
requests.push(
|
2019-08-07 14:47:01 +00:00
|
|
|
addQueryArgs( `${ ENDPOINTS.products }/tags`, {
|
2019-07-09 13:42:22 +00:00
|
|
|
include: selected,
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return requests;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a promise that resolves to a list of tags from the API.
|
|
|
|
*
|
2019-08-09 16:18:46 +00:00
|
|
|
* @param {Object} - A query object with the list of selected products and search term.
|
2019-07-09 13:42:22 +00:00
|
|
|
*/
|
|
|
|
export const getProductTags = ( { selected = [], search } ) => {
|
|
|
|
const requests = getProductTagsRequests( { selected, search } );
|
|
|
|
|
|
|
|
return Promise.all( requests.map( ( path ) => apiFetch( { path } ) ) ).then( ( data ) => {
|
|
|
|
return uniqBy( flatten( data ), 'id' );
|
|
|
|
} );
|
|
|
|
};
|
2019-08-15 09:36:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a promise that resolves to a category object from the API.
|
|
|
|
*
|
|
|
|
* @param {number} categoryId Id of the product to retrieve.
|
|
|
|
*/
|
|
|
|
export const getCategory = ( categoryId ) => {
|
|
|
|
return apiFetch( {
|
|
|
|
path: `${ ENDPOINTS.categories }/${ categoryId }`,
|
|
|
|
} );
|
|
|
|
};
|