2018-09-17 02:26:34 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2018-10-31 00:04:00 +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
|
|
|
|
* @returns {Function} - a function of ids returning a promise
|
|
|
|
*/
|
|
|
|
export function getRequestByIdString( path, handleData = identity ) {
|
2018-10-31 00:04:00 +00:00
|
|
|
return function( queryString = '', query ) {
|
|
|
|
const pathString = 'function' === typeof path ? 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
|
|
|
};
|
|
|
|
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',
|
2019-01-24 09:35:27 +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,
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
|
2019-02-04 02:50:22 +00:00
|
|
|
export const getCouponLabels = getRequestByIdString( NAMESPACE + '/coupons', coupon => ( {
|
2019-11-13 03:22:06 +00:00
|
|
|
key: coupon.id,
|
2019-01-24 09:35:27 +00:00
|
|
|
label: coupon.code,
|
|
|
|
} ) );
|
|
|
|
|
2019-02-04 02:50:22 +00:00
|
|
|
export const getCustomerLabels = getRequestByIdString( NAMESPACE + '/customers', customer => ( {
|
2019-11-13 03:22:06 +00:00
|
|
|
key: customer.id,
|
2019-03-18 02:39:54 +00:00
|
|
|
label: customer.name,
|
2019-01-24 09:35:27 +00:00
|
|
|
} ) );
|
|
|
|
|
2019-02-04 02:50:22 +00:00
|
|
|
export const getProductLabels = getRequestByIdString( NAMESPACE + '/products', product => ( {
|
2019-11-13 03:22:06 +00:00
|
|
|
key: product.id,
|
2019-01-24 09:35:27 +00:00
|
|
|
label: product.name,
|
|
|
|
} ) );
|
|
|
|
|
2019-11-13 03:22:06 +00:00
|
|
|
export const getTaxRateLabels = getRequestByIdString( NAMESPACE + '/taxes', tax_rate => ( {
|
|
|
|
key: tax_rate.id,
|
|
|
|
label: getTaxCode( tax_rate ),
|
|
|
|
} ) );
|
|
|
|
|
2019-01-24 09:35:27 +00:00
|
|
|
export const getVariationLabels = getRequestByIdString(
|
2019-02-04 02:50:22 +00:00
|
|
|
query => NAMESPACE + `/products/${ query.products }/variations`,
|
2019-01-24 09:35:27 +00:00
|
|
|
variation => {
|
|
|
|
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 ) =>
|
|
|
|
desc + `${ attribute.option }${ arr.length === index + 1 ? '' : ', ' }`,
|
|
|
|
''
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|