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-07 14:47:01 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { ENDPOINTS } from '../../constants';
|
|
|
|
|
2019-04-12 15:47:29 +00:00
|
|
|
export const isLargeCatalog = wc_product_block_data.isLargeCatalog || false;
|
2019-07-09 13:42:22 +00:00
|
|
|
export const limitTags = wc_product_block_data.limitTags || false;
|
|
|
|
export const hasTags = wc_product_block_data.hasTags || false;
|
2019-04-12 15:47:29 +00:00
|
|
|
|
|
|
|
const getProductsRequests = ( { selected = [], search } ) => {
|
|
|
|
const requests = [
|
2019-08-07 14:47:01 +00:00
|
|
|
addQueryArgs( ENDPOINTS.products, {
|
2019-04-12 15:47:29 +00:00
|
|
|
per_page: isLargeCatalog ? 100 : -1,
|
2019-08-12 11:54:51 +00:00
|
|
|
catalog_visibility: 'any',
|
2019-04-12 15:47:29 +00:00
|
|
|
status: 'publish',
|
|
|
|
search,
|
|
|
|
} ),
|
|
|
|
];
|
|
|
|
|
|
|
|
// If we have a large catalog, we might not get all selected products in the first page.
|
|
|
|
if ( isLargeCatalog && selected.length ) {
|
|
|
|
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
|
|
|
*/
|
|
|
|
export const getProducts = ( { selected = [], search } ) => {
|
|
|
|
const requests = getProductsRequests( { selected, search } );
|
|
|
|
|
|
|
|
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-09 16:18:46 +00:00
|
|
|
* @param {Object} 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-07-09 13:42:22 +00:00
|
|
|
per_page: limitTags ? 100 : -1,
|
|
|
|
orderby: limitTags ? 'count' : 'name',
|
|
|
|
order: limitTags ? 'desc' : 'asc',
|
|
|
|
search,
|
|
|
|
} ),
|
|
|
|
];
|
|
|
|
|
|
|
|
// If we have a large catalog, we might not get all selected products in the first page.
|
|
|
|
if ( limitTags && selected.length ) {
|
|
|
|
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' );
|
|
|
|
} );
|
|
|
|
};
|