woocommerce/plugins/woocommerce-admin/client/wc-api/items/utils.js

99 lines
2.9 KiB
JavaScript
Raw Normal View History

/**
* External dependencies
*/
/**
* WooCommerce dependencies
*/
import { appendTimestamp, getCurrentDates } from 'lib/date';
/**
* Returns leaderboard data to render a leaderboard table.
*
* @param {Object} options arguments
* @param {string} options.id Leaderboard ID
* @param {number} 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
wp.data Settings refactor add data store for settings using wp.data add use-select-with-refresh example replace fresh-data usage with new settings data store for settings page Add data package move to packages Fix isDirty after save Add isBusy to primary button when saving update Readme remove comment readme to use useSelect Revert "update Readme" This reverts commit 7402fd49b8f384fde5878e0bee0616f0a87bb4f6. Data Layer: Settings page to use Settings store (https://github.com/woocommerce/woocommerce-admin/pull/3430) * Data Layer: Settings store as source of truth for settings page This reverts commit 7402fd49b8f384fde5878e0bee0616f0a87bb4f6. * fixup * save on reset * non mutable constants * add set/getSettings * save using setSettings * separate HOC * cleanup * remove settingsToData * withHydration * remove withSettings HOC * renmove useSettins for now * withSettingsHydration updates * Revert "withSettingsHydration updates" This reverts commit f2adf108fbe19b574978fea5925a1a18e7ed3007. * rename withSettingsHydration * redo withSettingsHydration simplification * restore * useSettings * render using useSettings * handleInputChange working * get setIsDirty working * saving works * reset and cleanup * cleanup * use snake case on hook files * use clearIsDirty * Avoid mutation on setting update * remove @todo * persiting -> isPersisting * better reducer ternaries * add wcSettings as arg to withSettingsHydration reset package-lock Settings: split out mutable wcAdminSettings (https://github.com/woocommerce/woocommerce-admin/pull/3675) Settings: handle async settings groups (https://github.com/woocommerce/woocommerce-admin/pull/3707)
2020-03-25 03:20:17 +00:00
* @param {string} options.defaultDateRange User specified default date range.
* @return {Object} Object containing leaderboard responses.
*/
export function getLeaderboard( options ) {
const endpoint = 'leaderboards';
const {
per_page: perPage,
persisted_query: persistedQuery,
query,
select,
filterQuery,
} = options;
const { getItems, getItemsError, isGetItemsRequesting } = select(
'wc-api'
);
const response = {
isRequesting: false,
isError: false,
rows: [],
};
wp.data Settings refactor add data store for settings using wp.data add use-select-with-refresh example replace fresh-data usage with new settings data store for settings page Add data package move to packages Fix isDirty after save Add isBusy to primary button when saving update Readme remove comment readme to use useSelect Revert "update Readme" This reverts commit 7402fd49b8f384fde5878e0bee0616f0a87bb4f6. Data Layer: Settings page to use Settings store (https://github.com/woocommerce/woocommerce-admin/pull/3430) * Data Layer: Settings store as source of truth for settings page This reverts commit 7402fd49b8f384fde5878e0bee0616f0a87bb4f6. * fixup * save on reset * non mutable constants * add set/getSettings * save using setSettings * separate HOC * cleanup * remove settingsToData * withHydration * remove withSettings HOC * renmove useSettins for now * withSettingsHydration updates * Revert "withSettingsHydration updates" This reverts commit f2adf108fbe19b574978fea5925a1a18e7ed3007. * rename withSettingsHydration * redo withSettingsHydration simplification * restore * useSettings * render using useSettings * handleInputChange working * get setIsDirty working * saving works * reset and cleanup * cleanup * use snake case on hook files * use clearIsDirty * Avoid mutation on setting update * remove @todo * persiting -> isPersisting * better reducer ternaries * add wcSettings as arg to withSettingsHydration reset package-lock Settings: split out mutable wcAdminSettings (https://github.com/woocommerce/woocommerce-admin/pull/3675) Settings: handle async settings groups (https://github.com/woocommerce/woocommerce-admin/pull/3707)
2020-03-25 03:20:17 +00:00
const datesFromQuery = getCurrentDates( query, options.defaultDateRange );
const leaderboardQuery = {
...filterQuery,
after: appendTimestamp( datesFromQuery.primary.after, 'start' ),
before: appendTimestamp( datesFromQuery.primary.before, 'end' ),
per_page: perPage,
persisted_query: JSON.stringify( persistedQuery ),
};
// 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 );
if ( isGetItemsRequesting( endpoint, leaderboardQuery ) ) {
return { ...response, isRequesting: true };
} else if ( getItemsError( endpoint, leaderboardQuery ) ) {
return { ...response, isError: true };
}
const leaderboard = leaderboards.get( options.id );
return { ...response, rows: leaderboard.rows };
}
/**
* Returns items based on a search query.
*
* @param {Object} select Instance of @wordpress/select
* @param {string} endpoint Report API Endpoint
* @param {string[]} search Array of search strings.
* @return {Object} Object containing API request information and the matching items.
*/
export function searchItemsByString( select, endpoint, search ) {
const { getItems, getItemsError, isGetItemsRequesting } = select(
'wc-api'
);
const items = {};
let isRequesting = false;
let isError = false;
search.forEach( ( searchWord ) => {
const query = {
search: searchWord,
per_page: 10,
};
const newItems = getItems( endpoint, query );
newItems.forEach( ( item, id ) => {
items[ id ] = item;
} );
if ( isGetItemsRequesting( endpoint, query ) ) {
isRequesting = true;
}
if ( getItemsError( endpoint, query ) ) {
isError = true;
}
} );
return { items, isRequesting, isError };
}