2018-09-17 02:26:34 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-07-16 08:56:24 +00:00
|
|
|
import { addQueryArgs } from '@wordpress/url';
|
2018-09-17 02:26:34 +00:00
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
import { identity } from 'lodash';
|
2019-07-16 08:56:24 +00:00
|
|
|
import { getIdsFromQuery } from '@woocommerce/navigation';
|
2020-09-03 21:45:40 +00:00
|
|
|
import { NAMESPACE } from '@woocommerce/data';
|
2020-09-25 13:57:48 +00:00
|
|
|
import { getSetting } from '@woocommerce/wc-admin-settings';
|
2018-09-17 02:26:34 +00:00
|
|
|
|
2019-01-24 09:35:27 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-08-13 02:05:22 +00:00
|
|
|
import { getTaxCode } from '../../analytics/report/taxes/utils';
|
2019-01-24 09:35:27 +00:00
|
|
|
|
2018-09-17 02:26:34 +00:00
|
|
|
/**
|
|
|
|
* Get a function that accepts ids as they are found in url parameter and
|
|
|
|
* returns a promise with an optional method applied to results
|
|
|
|
*
|
2020-02-14 02:23:21 +00:00
|
|
|
* @param {string|Function} path - api path string or a function of the query returning api path string
|
2018-09-17 02:26:34 +00:00
|
|
|
* @param {Function} [handleData] - function applied to each iteration of data
|
2020-02-14 02:23:21 +00:00
|
|
|
* @return {Function} - a function of ids returning a promise
|
2018-09-17 02:26:34 +00:00
|
|
|
*/
|
|
|
|
export function getRequestByIdString( path, handleData = identity ) {
|
2020-07-28 02:32:58 +00:00
|
|
|
return function ( queryString = '', query ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
const pathString = typeof path === 'function' ? path( query ) : path;
|
2018-09-17 02:26:34 +00:00
|
|
|
const idList = getIdsFromQuery( queryString );
|
|
|
|
if ( idList.length < 1 ) {
|
|
|
|
return Promise.resolve( [] );
|
|
|
|
}
|
2019-07-16 08:56:24 +00:00
|
|
|
const payload = {
|
2018-09-17 02:26:34 +00:00
|
|
|
include: idList.join( ',' ),
|
|
|
|
per_page: idList.length,
|
2019-07-16 08:56:24 +00:00
|
|
|
};
|
2020-02-14 02:23:21 +00:00
|
|
|
return apiFetch( {
|
|
|
|
path: addQueryArgs( pathString, payload ),
|
|
|
|
} ).then( ( data ) => data.map( handleData ) );
|
2018-09-17 02:26:34 +00:00
|
|
|
};
|
|
|
|
}
|
2019-01-24 09:35:27 +00:00
|
|
|
|
2020-12-17 14:59:01 +00:00
|
|
|
export const getAttributeLabels = getRequestByIdString(
|
|
|
|
NAMESPACE + '/products/attributes',
|
|
|
|
( attribute ) => ( {
|
|
|
|
key: attribute.id,
|
|
|
|
label: attribute.name,
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
|
2019-01-24 09:35:27 +00:00
|
|
|
export const getCategoryLabels = getRequestByIdString(
|
2019-02-04 02:50:22 +00:00
|
|
|
NAMESPACE + '/products/categories',
|
2020-02-14 02:23:21 +00:00
|
|
|
( category ) => ( {
|
2019-11-13 03:22:06 +00:00
|
|
|
key: category.id,
|
2019-01-24 09:35:27 +00:00
|
|
|
label: category.name,
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
export const getCouponLabels = getRequestByIdString(
|
|
|
|
NAMESPACE + '/coupons',
|
|
|
|
( coupon ) => ( {
|
|
|
|
key: coupon.id,
|
|
|
|
label: coupon.code,
|
|
|
|
} )
|
|
|
|
);
|
2019-01-24 09:35:27 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
export const getCustomerLabels = getRequestByIdString(
|
|
|
|
NAMESPACE + '/customers',
|
|
|
|
( customer ) => ( {
|
|
|
|
key: customer.id,
|
|
|
|
label: customer.name,
|
|
|
|
} )
|
|
|
|
);
|
2019-01-24 09:35:27 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
export const getProductLabels = getRequestByIdString(
|
|
|
|
NAMESPACE + '/products',
|
|
|
|
( product ) => ( {
|
|
|
|
key: product.id,
|
|
|
|
label: product.name,
|
|
|
|
} )
|
|
|
|
);
|
2019-01-24 09:35:27 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
export const getTaxRateLabels = getRequestByIdString(
|
|
|
|
NAMESPACE + '/taxes',
|
2020-07-28 02:32:58 +00:00
|
|
|
( taxRate ) => ( {
|
2020-02-14 02:23:21 +00:00
|
|
|
key: taxRate.id,
|
|
|
|
label: getTaxCode( taxRate ),
|
|
|
|
} )
|
|
|
|
);
|
2019-11-13 03:22:06 +00:00
|
|
|
|
2020-09-25 13:57:48 +00:00
|
|
|
/**
|
|
|
|
* Create a variation name by concatenating each of the variation's
|
|
|
|
* attribute option strings.
|
|
|
|
*
|
|
|
|
* @param {Object} variation - variation returned by the api
|
|
|
|
* @param {Array} variation.attributes - attribute objects, with option property.
|
|
|
|
* @param {string} variation.name - name of variation.
|
|
|
|
* @return {string} - formatted variation name
|
|
|
|
*/
|
|
|
|
export function getVariationName( { attributes, name } ) {
|
|
|
|
const separator = getSetting( 'variationTitleAttributesSeparator', ' - ' );
|
|
|
|
|
2021-09-23 11:58:10 +00:00
|
|
|
if ( name && name.indexOf( separator ) > -1 ) {
|
2020-09-25 13:57:48 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2021-09-23 11:58:10 +00:00
|
|
|
const attributeList = ( attributes || [] )
|
2020-09-25 13:57:48 +00:00
|
|
|
.map( ( { option } ) => option )
|
|
|
|
.join( ', ' );
|
|
|
|
|
|
|
|
return attributeList ? name + separator + attributeList : name;
|
|
|
|
}
|
|
|
|
|
2019-01-24 09:35:27 +00:00
|
|
|
export const getVariationLabels = getRequestByIdString(
|
2020-09-25 13:57:48 +00:00
|
|
|
( { products } ) => {
|
|
|
|
// If a product was specified, get just its variations.
|
|
|
|
if ( products ) {
|
|
|
|
return NAMESPACE + `/products/${ products }/variations`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NAMESPACE + '/variations';
|
|
|
|
},
|
2020-02-14 02:23:21 +00:00
|
|
|
( variation ) => {
|
2019-01-24 09:35:27 +00:00
|
|
|
return {
|
2019-11-13 03:22:06 +00:00
|
|
|
key: variation.id,
|
2020-09-25 13:57:48 +00:00
|
|
|
label: getVariationName( variation ),
|
2019-01-24 09:35:27 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|