2019-02-01 09:55:19 +00:00
|
|
|
/** @format */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns items based on a search query.
|
|
|
|
*
|
2019-02-20 09:21:05 +00:00
|
|
|
* @param {Object} select Instance of @wordpress/select
|
|
|
|
* @param {String} endpoint Report API Endpoint
|
|
|
|
* @param {String[]} search Array of search strings.
|
2019-02-26 09:28:50 +00:00
|
|
|
* @return {Object} Object containing API request information and the matching items.
|
2019-02-01 09:55:19 +00:00
|
|
|
*/
|
|
|
|
export function searchItemsByString( select, endpoint, search ) {
|
2019-02-26 09:28:50 +00:00
|
|
|
const { getItems, getItemsError, isGetItemsRequesting } = select( 'wc-api' );
|
2019-02-01 09:55:19 +00:00
|
|
|
|
2019-02-01 10:01:42 +00:00
|
|
|
const items = {};
|
2019-02-26 09:28:50 +00:00
|
|
|
let isRequesting = false;
|
|
|
|
let isError = false;
|
2019-02-20 09:21:05 +00:00
|
|
|
search.forEach( searchWord => {
|
2019-02-26 09:28:50 +00:00
|
|
|
const query = {
|
2019-02-01 10:01:42 +00:00
|
|
|
search: searchWord,
|
|
|
|
per_page: 10,
|
2019-02-26 09:28:50 +00:00
|
|
|
};
|
|
|
|
const newItems = getItems( endpoint, query );
|
2019-02-01 10:01:42 +00:00
|
|
|
newItems.forEach( ( item, id ) => {
|
|
|
|
items[ id ] = item;
|
|
|
|
} );
|
2019-02-26 09:28:50 +00:00
|
|
|
if ( isGetItemsRequesting( endpoint, query ) ) {
|
|
|
|
isRequesting = true;
|
|
|
|
}
|
|
|
|
if ( getItemsError( endpoint, query ) ) {
|
|
|
|
isError = true;
|
|
|
|
}
|
2019-02-01 10:01:42 +00:00
|
|
|
} );
|
2019-02-01 09:55:19 +00:00
|
|
|
|
2019-02-26 09:28:50 +00:00
|
|
|
return { items, isRequesting, isError };
|
2019-02-01 09:55:19 +00:00
|
|
|
}
|