2021-01-05 13:58:18 +00:00
/* eslint-disable you-dont-need-lodash-underscore/flatten -- until we have an alternative to uniqBy we'll keep flatten to avoid potential introduced bugs with alternatives */
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' ;
2020-04-24 13:36:47 +00:00
import { IS _LARGE _CATALOG , LIMIT _TAGS } from '@woocommerce/block-settings' ;
2019-04-12 15:47:29 +00:00
2019-12-10 17:17:46 +00:00
/ * *
2020-04-24 13:36:47 +00:00
* Get product query requests for the Store API .
2019-12-10 17:17:46 +00:00
*
* @ param { Object } request A query object with the list of selected products and search term .
2020-09-20 23:54:08 +00:00
* @ param { Array } request . selected Currently selected products .
2019-12-10 17:17:46 +00:00
* @ param { string } request . search Search string .
* @ param { Array } request . queryArgs Query args to pass in .
* /
2019-09-05 15:09:31 +00:00
const getProductsRequests = ( {
selected = [ ] ,
search = '' ,
queryArgs = [ ] ,
} ) => {
2019-08-15 14:55:57 +00:00
const defaultArgs = {
2020-04-24 13:36:47 +00:00
per _page : IS _LARGE _CATALOG ? 100 : 0 ,
2019-08-15 14:55:57 +00:00
catalog _visibility : 'any' ,
search ,
orderby : 'title' ,
order : 'asc' ,
} ;
2019-04-12 15:47:29 +00:00
const requests = [
2020-04-24 13:36:47 +00:00
addQueryArgs ( '/wc/store/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 (
2020-04-24 13:36:47 +00:00
addQueryArgs ( '/wc/store/products' , {
2019-08-12 11:54:51 +00:00
catalog _visibility : 'any' ,
2019-04-12 15:47:29 +00:00
include : selected ,
} )
) ;
}
return requests ;
} ;
/ * *
2020-04-24 13:36:47 +00:00
* Get a promise that resolves to a list of products from the Store API .
2019-04-12 15:47:29 +00:00
*
2019-12-10 17:17:46 +00:00
* @ param { Object } request A query object with the list of selected products and search term .
2020-09-20 23:54:08 +00:00
* @ param { Array } request . selected Currently selected products .
2019-12-10 17:17:46 +00:00
* @ param { string } request . search Search string .
* @ param { Array } request . queryArgs Query args to pass in .
2019-04-12 15:47:29 +00:00
* /
2019-09-05 15:09:31 +00:00
export const getProducts = ( {
selected = [ ] ,
search = '' ,
queryArgs = [ ] ,
} ) => {
2019-08-15 14:55:57 +00:00
const requests = getProductsRequests ( { selected , search , queryArgs } ) ;
2019-04-12 15:47:29 +00:00
2019-08-30 09:36:06 +00:00
return Promise . all ( requests . map ( ( path ) => apiFetch ( { path } ) ) )
2019-09-25 14:22:36 +00:00
. then ( ( data ) => {
const products = uniqBy ( flatten ( data ) , 'id' ) ;
const list = products . map ( ( product ) => ( {
... product ,
parent : 0 ,
} ) ) ;
return list ;
} )
2019-08-30 09:36:06 +00:00
. catch ( ( e ) => {
throw e ;
} ) ;
2019-04-12 15:47:29 +00:00
} ;
2019-07-09 13:42:22 +00:00
2019-08-02 11:56:53 +00:00
/ * *
2020-04-24 13:36:47 +00:00
* Get a promise that resolves to a product object from the Store API .
2019-08-02 11:56:53 +00:00
*
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 ( {
2020-04-24 13:36:47 +00:00
path : ` /wc/store/products/ ${ productId } ` ,
2019-08-02 11:56:53 +00:00
} ) ;
} ;
2020-04-24 13:36:47 +00:00
/ * *
* Get a promise that resolves to a list of attribute objects from the Store API .
* /
export const getAttributes = ( ) => {
return apiFetch ( {
path : ` wc/store/products/attributes ` ,
} ) ;
} ;
/ * *
* Get a promise that resolves to a list of attribute term objects from the Store API .
*
* @ param { number } attribute Id of the attribute to retrieve terms for .
* /
export const getTerms = ( attribute ) => {
return apiFetch ( {
path : ` wc/store/products/attributes/ ${ attribute } /terms ` ,
} ) ;
} ;
/ * *
* Get product tag query requests for the Store API .
*
* @ param { Object } request A query object with the list of selected products and search term .
2020-09-20 23:54:08 +00:00
* @ param { Array } request . selected Currently selected tags .
2020-04-24 13:36:47 +00:00
* @ param { string } request . search Search string .
* /
2019-07-09 13:42:22 +00:00
const getProductTagsRequests = ( { selected = [ ] , search } ) => {
const requests = [
2020-04-24 13:36:47 +00:00
addQueryArgs ( ` wc/store/products/tags ` , {
per _page : LIMIT _TAGS ? 100 : 0 ,
2019-08-17 09:14:11 +00:00
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 (
2020-04-24 13:36:47 +00:00
addQueryArgs ( ` wc/store/products/tags ` , {
2019-07-09 13:42:22 +00:00
include : selected ,
} )
) ;
}
return requests ;
} ;
/ * *
2020-04-24 13:36:47 +00:00
* Get a promise that resolves to a list of tags from the Store API .
2019-07-09 13:42:22 +00:00
*
2020-09-20 23:54:08 +00:00
* @ param { Object } props A query object with the list of selected products and search term .
* @ param { Array } props . selected
* @ param { string } props . search
2019-07-09 13:42:22 +00:00
* /
export const getProductTags = ( { selected = [ ] , search } ) => {
const requests = getProductTagsRequests ( { selected , search } ) ;
2019-09-05 15:09:31 +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-15 09:36:24 +00:00
/ * *
2020-04-24 13:36:47 +00:00
* Get a promise that resolves to a list of category objects from the Store API .
2019-12-10 17:17:46 +00:00
*
* @ param { Object } queryArgs Query args to pass in .
2019-09-04 16:07:00 +00:00
* /
2019-11-19 15:22:16 +00:00
export const getCategories = ( queryArgs ) => {
2019-09-04 16:07:00 +00:00
return apiFetch ( {
2020-04-24 13:36:47 +00:00
path : addQueryArgs ( ` wc/store/products/categories ` , {
per _page : 0 ,
2019-11-19 15:22:16 +00:00
... queryArgs ,
2019-09-05 15:09:31 +00:00
} ) ,
2019-09-04 16:07:00 +00:00
} ) ;
} ;
2020-04-24 13:36:47 +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 ) => {
2019-09-25 14:22:36 +00:00
return apiFetch ( {
2020-04-24 13:36:47 +00:00
path : ` wc/store/products/categories/ ${ categoryId } ` ,
2019-09-25 14:22:36 +00:00
} ) ;
} ;
2020-04-24 13:36:47 +00:00
/ * *
* Get a promise that resolves to a list of variation objects from the Store API .
*
* @ param { number } product Product ID .
* /
export const getProductVariations = ( product ) => {
2019-09-04 16:07:00 +00:00
return apiFetch ( {
2020-04-24 13:36:47 +00:00
path : addQueryArgs ( ` wc/store/products ` , {
per _page : 0 ,
type : 'variation' ,
parent : product ,
2019-09-05 15:09:31 +00:00
} ) ,
2019-09-04 16:07:00 +00:00
} ) ;
} ;
2020-04-22 16:44:12 +00:00
/ * *
* Given a page object and an array of page , format the title .
*
* @ param { Object } page Page object .
* @ param { Object } page . title Page title object .
* @ param { string } page . title . raw Page title .
* @ param { string } page . slug Page slug .
* @ param { Array } pages Array of all pages .
* @ return { string } Formatted page title to display .
* /
export const formatTitle = ( page , pages ) => {
if ( ! page . title . raw ) {
return page . slug ;
}
const isUnique =
pages . filter ( ( p ) => p . title . raw === page . title . raw ) . length === 1 ;
return page . title . raw + ( ! isUnique ? ` - ${ page . slug } ` : '' ) ;
} ;