2019-02-01 09:55:19 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
|
2019-04-11 02:53:05 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
2019-11-26 19:39:40 +00:00
|
|
|
import { appendTimestamp, getCurrentDates } from 'lib/date';
|
2019-04-11 02:53:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns leaderboard data to render a leaderboard table.
|
|
|
|
*
|
2020-02-14 02:23:21 +00:00
|
|
|
* @param {Object} options arguments
|
|
|
|
* @param {string} options.id Leaderboard ID
|
|
|
|
* @param {number} options.per_page Per page limit
|
2019-04-11 02:53:05 +00:00
|
|
|
* @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
|
2020-03-25 03:20:17 +00:00
|
|
|
* @param {string} options.defaultDateRange User specified default date range.
|
2019-04-11 02:53:05 +00:00
|
|
|
* @return {Object} Object containing leaderboard responses.
|
|
|
|
*/
|
|
|
|
export function getLeaderboard( options ) {
|
|
|
|
const endpoint = 'leaderboards';
|
2020-02-25 01:18:52 +00:00
|
|
|
const {
|
|
|
|
per_page: perPage,
|
|
|
|
persisted_query: persistedQuery,
|
|
|
|
query,
|
|
|
|
select,
|
|
|
|
} = options;
|
2020-02-14 02:23:21 +00:00
|
|
|
const { getItems, getItemsError, isGetItemsRequesting } = select(
|
|
|
|
'wc-api'
|
|
|
|
);
|
2019-04-11 02:53:05 +00:00
|
|
|
const response = {
|
|
|
|
isRequesting: false,
|
|
|
|
isError: false,
|
|
|
|
rows: [],
|
|
|
|
};
|
|
|
|
|
2020-03-25 03:20:17 +00:00
|
|
|
const datesFromQuery = getCurrentDates( query, options.defaultDateRange );
|
2019-04-11 02:53:05 +00:00
|
|
|
const leaderboardQuery = {
|
|
|
|
after: appendTimestamp( datesFromQuery.primary.after, 'start' ),
|
|
|
|
before: appendTimestamp( datesFromQuery.primary.before, 'end' ),
|
2020-02-14 02:23:21 +00:00
|
|
|
per_page: perPage,
|
|
|
|
persisted_query: JSON.stringify( persistedQuery ),
|
2019-04-11 02:53:05 +00:00
|
|
|
};
|
|
|
|
|
2020-02-25 01:18:52 +00:00
|
|
|
// Disable eslint rule requiring `getItems` to be defined below because the next two statements
|
|
|
|
// depend on `getItems` to have been called.
|
|
|
|
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
|
|
|
|
const leaderboards = getItems( endpoint, leaderboardQuery );
|
|
|
|
|
2019-04-11 02:53:05 +00:00
|
|
|
if ( isGetItemsRequesting( endpoint, leaderboardQuery ) ) {
|
|
|
|
return { ...response, isRequesting: true };
|
|
|
|
} else if ( getItemsError( endpoint, leaderboardQuery ) ) {
|
|
|
|
return { ...response, isError: true };
|
|
|
|
}
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const leaderboard = leaderboards.get( options.id );
|
2019-04-11 02:53:05 +00:00
|
|
|
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
|
2020-02-14 02:23:21 +00:00
|
|
|
* @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 ) {
|
2020-02-14 02:23:21 +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;
|
2020-02-14 02:23:21 +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
|
|
|
}
|