2018-12-12 14:25:36 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { first } from 'lodash';
|
|
|
|
|
2021-07-26 19:11:44 +00:00
|
|
|
export function extendTableData(
|
|
|
|
extendedStoreSelector,
|
|
|
|
props,
|
|
|
|
queriedTableData
|
|
|
|
) {
|
|
|
|
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,
|
2021-07-26 19:11:44 +00:00
|
|
|
} = extendedStoreSelector;
|
2018-12-12 14:25:36 +00:00
|
|
|
const extendQuery = {
|
2020-02-14 02:23:21 +00:00
|
|
|
include: itemsData.map( ( item ) => item[ itemIdField ] ).join( ',' ),
|
2018-12-12 14:25:36 +00:00
|
|
|
per_page: itemsData.length,
|
|
|
|
};
|
|
|
|
const extendedItems = loadMethod( extendQuery );
|
2020-02-14 02:23:21 +00:00
|
|
|
const isExtendedItemsRequesting = isRequestingMethod
|
|
|
|
? isRequestingMethod( extendQuery )
|
|
|
|
: false;
|
|
|
|
const isExtendedItemsError = getErrorMethod
|
|
|
|
? getErrorMethod( extendQuery )
|
|
|
|
: false;
|
2018-12-12 14:25:36 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const extendedItemsData = itemsData.map( ( item ) => {
|
2018-12-12 14:25:36 +00:00
|
|
|
const extendedItemData = first(
|
2020-02-14 02:23:21 +00:00
|
|
|
extendedItems.filter(
|
|
|
|
( extendedItem ) => item.id === extendedItem.id
|
|
|
|
)
|
2018-12-12 14:25:36 +00:00
|
|
|
);
|
|
|
|
return {
|
|
|
|
...item,
|
|
|
|
...extendedItemData,
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const isRequesting =
|
|
|
|
queriedTableData.isRequesting || isExtendedItemsRequesting;
|
2018-12-12 14:25:36 +00:00
|
|
|
const isError = queriedTableData.isError || isExtendedItemsError;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...queriedTableData,
|
|
|
|
isRequesting,
|
|
|
|
isError,
|
|
|
|
items: {
|
|
|
|
...queriedTableData.items,
|
|
|
|
data: extendedItemsData,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|