2019-02-01 09:55:19 +00:00
|
|
|
/** @format */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
|
2019-04-11 02:53:05 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { appendTimestamp, getCurrentDates } from '@woocommerce/date';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns leaderboard data to render a leaderboard table.
|
|
|
|
*
|
|
|
|
* @param {Objedt} options arguments
|
|
|
|
* @param {String} options.id Leaderboard ID
|
|
|
|
* @param {Integer} options.per_page Per page limit
|
|
|
|
* @param {Object} options.persisted_query Persisted query passed to endpoint
|
|
|
|
* @param {Object} options.query Query parameters in the url
|
|
|
|
* @param {Object} options.select Instance of @wordpress/select
|
|
|
|
* @return {Object} Object containing leaderboard responses.
|
|
|
|
*/
|
|
|
|
export function getLeaderboard( options ) {
|
|
|
|
const endpoint = 'leaderboards';
|
|
|
|
const { per_page, persisted_query, query, select } = options;
|
|
|
|
const { getItems, getItemsError, isGetItemsRequesting } = select( 'wc-api' );
|
|
|
|
const response = {
|
|
|
|
isRequesting: false,
|
|
|
|
isError: false,
|
|
|
|
rows: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
const datesFromQuery = getCurrentDates( query );
|
|
|
|
const leaderboardQuery = {
|
|
|
|
after: appendTimestamp( datesFromQuery.primary.after, 'start' ),
|
|
|
|
before: appendTimestamp( datesFromQuery.primary.before, 'end' ),
|
|
|
|
per_page,
|
|
|
|
persisted_query,
|
|
|
|
};
|
|
|
|
|
|
|
|
const leaderboards = getItems( endpoint, leaderboardQuery );
|
|
|
|
const leaderboard = leaderboards.get( options.id );
|
|
|
|
if ( isGetItemsRequesting( endpoint, leaderboardQuery ) ) {
|
|
|
|
return { ...response, isRequesting: true };
|
|
|
|
} else if ( getItemsError( endpoint, leaderboardQuery ) ) {
|
|
|
|
return { ...response, isError: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
return { ...response, rows: leaderboard.rows };
|
|
|
|
}
|
|
|
|
|
2019-02-01 09:55:19 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|