2018-12-12 14:25:36 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { first } from 'lodash';
|
|
|
|
|
|
|
|
export function extendTableData( select, props, queriedTableData ) {
|
2018-12-12 14:51:24 +00:00
|
|
|
const { extendItemsMethodNames, itemIdField } = props;
|
2018-12-12 14:25:36 +00:00
|
|
|
const itemsData = queriedTableData.items.data;
|
|
|
|
if (
|
|
|
|
! Array.isArray( itemsData ) ||
|
|
|
|
! itemsData.length ||
|
|
|
|
! extendItemsMethodNames ||
|
|
|
|
! itemIdField
|
|
|
|
) {
|
|
|
|
return queriedTableData;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
2018-12-15 12:15:13 +00:00
|
|
|
[ extendItemsMethodNames.getError ]: getErrorMethod,
|
2018-12-12 14:25:36 +00:00
|
|
|
[ extendItemsMethodNames.isRequesting ]: isRequestingMethod,
|
|
|
|
[ extendItemsMethodNames.load ]: loadMethod,
|
|
|
|
} = select( 'wc-api' );
|
|
|
|
const extendQuery = {
|
|
|
|
include: itemsData.map( item => item[ itemIdField ] ).join( ',' ),
|
|
|
|
per_page: itemsData.length,
|
|
|
|
};
|
|
|
|
const extendedItems = loadMethod( extendQuery );
|
|
|
|
const isExtendedItemsRequesting = isRequestingMethod ? isRequestingMethod( extendQuery ) : false;
|
2018-12-15 12:15:13 +00:00
|
|
|
const isExtendedItemsError = getErrorMethod ? getErrorMethod( extendQuery ) : false;
|
2018-12-12 14:25:36 +00:00
|
|
|
|
|
|
|
const extendedItemsData = itemsData.map( item => {
|
|
|
|
const extendedItemData = first(
|
|
|
|
extendedItems.filter( extendedItem => item.id === extendedItem.id )
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
...item,
|
|
|
|
...extendedItemData,
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
|
|
|
const isRequesting = queriedTableData.isRequesting || isExtendedItemsRequesting;
|
|
|
|
const isError = queriedTableData.isError || isExtendedItemsError;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...queriedTableData,
|
|
|
|
isRequesting,
|
|
|
|
isError,
|
|
|
|
items: {
|
|
|
|
...queriedTableData.items,
|
|
|
|
data: extendedItemsData,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|