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';
|
|
|
|
|
|
|
|
/**
|
2018-11-05 21:02:04 +00:00
|
|
|
* WooCommerce dependencies
|
2018-09-17 02:26:34 +00:00
|
|
|
*/
|
2019-07-16 08:56:24 +00:00
|
|
|
import { getIdsFromQuery } from '@woocommerce/navigation';
|
2018-09-17 02:26:34 +00:00
|
|
|
|
2019-01-24 09:35:27 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-11-13 03:22:06 +00:00
|
|
|
import { getTaxCode } from 'analytics/report/taxes/utils';
|
2019-01-30 07:22:10 +00:00
|
|
|
import { NAMESPACE } from 'wc-api/constants';
|
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
|
|
|
|
|
|
|
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
|
|
|
|
2019-01-24 09:35:27 +00:00
|
|
|
export const getVariationLabels = getRequestByIdString(
|
2020-02-14 02:23:21 +00:00
|
|
|
( query ) => NAMESPACE + `/products/${ query.products }/variations`,
|
|
|
|
( variation ) => {
|
2019-01-24 09:35:27 +00:00
|
|
|
return {
|
2019-11-13 03:22:06 +00:00
|
|
|
key: variation.id,
|
2019-01-24 09:35:27 +00:00
|
|
|
label: variation.attributes.reduce(
|
|
|
|
( desc, attribute, index, arr ) =>
|
2020-02-14 02:23:21 +00:00
|
|
|
desc +
|
|
|
|
`${ attribute.option }${
|
|
|
|
arr.length === index + 1 ? '' : ', '
|
|
|
|
}`,
|
2019-01-24 09:35:27 +00:00
|
|
|
''
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|