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

483 lines
14 KiB
JavaScript
Raw Normal View History

/**
* External dependencies
*/
import { find, forEach, isNull, get, includes } from 'lodash';
import moment from 'moment';
/**
* WooCommerce dependencies
*/
import {
appendTimestamp,
getCurrentDates,
getIntervalForQuery,
} from 'lib/date';
import {
flattenFilters,
getActiveFiltersFromQuery,
getUrlKey,
} from '@woocommerce/navigation';
/**
* Internal dependencies
*/
import { MAX_PER_PAGE, QUERY_DEFAULTS } from 'wc-api/constants';
import * as reportsUtils from './utils';
2018-10-18 09:34:37 +00:00
2019-03-21 03:25:05 +00:00
/**
* Add filters and advanced filters values to a query object.
*
* @param {Object} options arguments
* @param {string} options.endpoint Report API Endpoint
2019-03-21 03:25:05 +00:00
* @param {Object} options.query Query parameters in the url
* @param {Array} options.limitBy Properties used to limit the results. It will be used in the API call to send the IDs.
* @param {Array} [options.filters] config filters
* @param {Object} [options.advancedFilters] config advanced filters
* @return {Object} A query object with the values from filters and advanced fitlters applied.
2019-03-21 03:25:05 +00:00
*/
export function getFilterQuery( options ) {
const {
endpoint,
query,
limitBy,
filters = [],
advancedFilters = {},
} = options;
if ( query.search ) {
const limitProperties = limitBy || [ endpoint ];
return limitProperties.reduce( ( result, limitProperty ) => {
result[ limitProperty ] = query[ limitProperty ];
return result;
}, {} );
}
2019-03-21 03:25:05 +00:00
return filters
.map( ( filter ) =>
getQueryFromConfig( filter, advancedFilters, query )
)
.reduce(
( result, configQuery ) => Object.assign( result, configQuery ),
{}
);
2018-10-29 01:30:24 +00:00
}
2018-10-18 09:34:37 +00:00
// Some stats endpoints don't have interval data, so they can ignore after/before params and omit that part of the response.
const noIntervalEndpoints = [ 'stock', 'customers' ];
2019-01-16 20:08:04 +00:00
/**
* Add timestamp to advanced filter parameters involving date. The api
* expects a timestamp for these values similar to `before` and `after`.
*
* @param {Object} config - advancedFilters config object.
* @param {Object} activeFilter - an active filter.
* @return {Object} - an active filter with timestamp added to date values.
2019-01-16 20:08:04 +00:00
*/
2019-01-15 02:40:12 +00:00
export function timeStampFilterDates( config, activeFilter ) {
2019-01-16 20:08:04 +00:00
const advancedFilterConfig = config.filters[ activeFilter.key ];
if ( get( advancedFilterConfig, [ 'input', 'component' ] ) !== 'Date' ) {
2019-01-17 00:57:11 +00:00
return activeFilter;
}
2019-01-16 20:37:43 +00:00
2019-01-17 00:57:11 +00:00
const { rule, value } = activeFilter;
const timeOfDayMap = {
after: 'start',
before: 'end',
};
// If the value is an array, it signifies "between" values which must have a timestamp
// appended to each value.
if ( Array.isArray( value ) ) {
const [ after, before ] = value;
2019-01-16 20:37:43 +00:00
return Object.assign( {}, activeFilter, {
2019-01-17 00:57:11 +00:00
value: [
appendTimestamp( moment( after ), timeOfDayMap.after ),
appendTimestamp( moment( before ), timeOfDayMap.before ),
],
2019-01-16 20:37:43 +00:00
} );
2019-01-15 02:40:12 +00:00
}
2019-01-17 00:57:11 +00:00
return Object.assign( {}, activeFilter, {
value: appendTimestamp( moment( value ), timeOfDayMap[ rule ] ),
} );
2019-01-15 02:40:12 +00:00
}
2018-10-29 01:30:24 +00:00
export function getQueryFromConfig( config, advancedFilters, query ) {
const queryValue = query[ config.param ];
if ( ! queryValue ) {
2018-10-18 09:34:37 +00:00
return {};
}
if ( queryValue === 'advanced' ) {
const activeFilters = getActiveFiltersFromQuery(
query,
advancedFilters.filters
);
2018-10-18 09:34:37 +00:00
if ( activeFilters.length === 0 ) {
return {};
}
return activeFilters
.map( ( filter ) =>
timeStampFilterDates( advancedFilters, filter )
)
.reduce(
( result, activeFilter ) => {
const { key, rule, value } = activeFilter;
result[ getUrlKey( key, rule ) ] = value;
return result;
},
{ match: query.match || 'all' }
);
2018-10-18 09:34:37 +00:00
}
const filter = find( flattenFilters( config.filters ), {
value: queryValue,
} );
2018-10-18 09:34:37 +00:00
2018-10-29 01:30:24 +00:00
if ( ! filter ) {
2018-10-18 09:34:37 +00:00
return {};
}
2018-10-29 01:30:24 +00:00
if ( filter.settings && filter.settings.param ) {
const { param } = filter.settings;
2018-10-18 09:34:37 +00:00
if ( query[ param ] ) {
return {
[ param ]: query[ param ],
};
}
return {};
}
return {
[ config.param ]: queryValue,
};
2018-10-18 09:34:37 +00:00
}
/**
* Returns true if a report object is empty.
*
* @param {Object} report Report to check
* @param {string} endpoint Endpoint slug
* @return {boolean} True if report is data is empty.
*/
export function isReportDataEmpty( report, endpoint ) {
if ( ! report ) {
return true;
}
if ( ! report.data ) {
return true;
}
if ( ! report.data.totals || isNull( report.data.totals ) ) {
return true;
}
const checkIntervals = ! includes( noIntervalEndpoints, endpoint );
if (
checkIntervals &&
( ! report.data.intervals || report.data.intervals.length === 0 )
) {
return true;
}
return false;
}
2018-10-17 23:06:33 +00:00
/**
* Constructs and returns a query associated with a Report data request.
*
* @param {Object} options arguments
* @param {string} options.endpoint Report API Endpoint
* @param {string} options.dataType 'primary' or 'secondary'.
2019-03-21 03:25:05 +00:00
* @param {Object} options.query Query parameters in the url.
* @param {Array} options.limitBy Properties used to limit the results. It will be used in the API call to send the IDs.
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} data request query parameters.
2018-10-17 23:06:33 +00:00
*/
2019-03-21 03:25:05 +00:00
function getRequestQuery( options ) {
const { endpoint, dataType, query, fields } = options;
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 interval = getIntervalForQuery( query );
2019-03-21 03:25:05 +00:00
const filterQuery = getFilterQuery( options );
const end = datesFromQuery[ dataType ].before;
const noIntervals = includes( noIntervalEndpoints, endpoint );
return noIntervals
? { ...filterQuery, fields }
: {
order: 'asc',
interval,
per_page: MAX_PER_PAGE,
after: appendTimestamp(
datesFromQuery[ dataType ].after,
'start'
),
before: appendTimestamp( end, 'end' ),
segmentby: query.segmentby,
fields,
...filterQuery,
};
}
/**
* Returns summary number totals needed to render a report page.
*
* @param {Object} options arguments
* @param {string} options.endpoint Report API Endpoint
2019-03-21 03:25:05 +00:00
* @param {Object} options.query Query parameters in the url
* @param {Object} options.select Instance of @wordpress/select
* @param {Array} options.limitBy Properties used to limit the results. It will be used in the API call to send the IDs.
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 summary number responses.
*/
2019-03-21 03:25:05 +00:00
export function getSummaryNumbers( options ) {
const { endpoint, select } = options;
const {
getReportStats,
getReportStatsError,
isReportStatsRequesting,
} = select( 'wc-api' );
const response = {
isRequesting: false,
isError: false,
totals: {
primary: null,
secondary: null,
},
};
2019-03-21 03:25:05 +00:00
const primaryQuery = getRequestQuery( { ...options, dataType: 'primary' } );
// Disable eslint rule requiring `getReportStats` to be defined below because the next two statements
// depend on `getReportStats` to have been called.
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
const primary = getReportStats( endpoint, primaryQuery );
2019-03-21 03:25:05 +00:00
if ( isReportStatsRequesting( endpoint, primaryQuery ) ) {
return { ...response, isRequesting: true };
2019-03-21 03:25:05 +00:00
} else if ( getReportStatsError( endpoint, primaryQuery ) ) {
return { ...response, isError: true };
}
const primaryTotals =
( primary && primary.data && primary.data.totals ) || null;
const secondaryQuery = getRequestQuery( {
...options,
dataType: 'secondary',
} );
// Disable eslint rule requiring `getReportStats` to be defined below because the next two statements
// depend on `getReportStats` to have been called.
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
const secondary = getReportStats( endpoint, secondaryQuery );
2019-03-21 03:25:05 +00:00
if ( isReportStatsRequesting( endpoint, secondaryQuery ) ) {
return { ...response, isRequesting: true };
2019-03-21 03:25:05 +00:00
} else if ( getReportStatsError( endpoint, secondaryQuery ) ) {
return { ...response, isError: true };
}
const secondaryTotals =
( secondary && secondary.data && secondary.data.totals ) || null;
return {
...response,
totals: { primary: primaryTotals, secondary: secondaryTotals },
};
}
/**
* Returns all of the data needed to render a chart with summary numbers on a report page.
*
* @param {Object} options arguments
* @param {string} options.endpoint Report API Endpoint
* @param {string} options.dataType 'primary' or 'secondary'
2019-03-21 03:25:05 +00:00
* @param {Object} options.query Query parameters in the url
* @param {Object} options.select Instance of @wordpress/select
* @param {Array} options.limitBy Properties used to limit the results. It will be used in the API call to send the IDs.
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 API request information (response, fetching, and error details)
*/
2019-03-21 03:25:05 +00:00
export function getReportChartData( options ) {
const { endpoint, select } = options;
const {
getReportStats,
getReportStatsError,
isReportStatsRequesting,
} = select( 'wc-api' );
const response = {
isEmpty: false,
isError: false,
isRequesting: false,
data: {
totals: {},
intervals: [],
},
};
2019-03-21 03:25:05 +00:00
const requestQuery = getRequestQuery( options );
// Disable eslint rule requiring `stats` to be defined below because the next two if statements
// depend on `getReportStats` to have been called.
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
2019-03-21 03:25:05 +00:00
const stats = getReportStats( endpoint, requestQuery );
2019-03-21 03:25:05 +00:00
if ( isReportStatsRequesting( endpoint, requestQuery ) ) {
return { ...response, isRequesting: true };
}
if ( getReportStatsError( endpoint, requestQuery ) ) {
return { ...response, isError: true };
}
if ( isReportDataEmpty( stats, endpoint ) ) {
return { ...response, isEmpty: true };
}
const totals = ( stats && stats.data && stats.data.totals ) || null;
let intervals = ( stats && stats.data && stats.data.intervals ) || [];
// If we have more than 100 results for this time period,
// we need to make additional requests to complete the response.
if ( stats.totalResults > MAX_PER_PAGE ) {
let isFetching = true;
let isError = false;
const pagedData = [];
const totalPages = Math.ceil( stats.totalResults / MAX_PER_PAGE );
let pagesFetched = 1;
for ( let i = 2; i <= totalPages; i++ ) {
const nextQuery = { ...requestQuery, page: i };
2019-03-21 03:25:05 +00:00
const _data = getReportStats( endpoint, nextQuery );
if ( isReportStatsRequesting( endpoint, nextQuery ) ) {
continue;
}
2019-03-21 03:25:05 +00:00
if ( getReportStatsError( endpoint, nextQuery ) ) {
isError = true;
isFetching = false;
break;
}
pagedData.push( _data );
pagesFetched++;
if ( pagesFetched === totalPages ) {
isFetching = false;
break;
}
}
if ( isFetching ) {
return { ...response, isRequesting: true };
} else if ( isError ) {
return { ...response, isError: true };
}
forEach( pagedData, function( _data ) {
intervals = intervals.concat( _data.data.intervals );
} );
}
return { ...response, data: { totals, intervals } };
}
/**
* Returns a formatting function or string to be used by d3-format
*
* @param {string} type Type of number, 'currency', 'number', 'percent', 'average'
* @param {Function} formatAmount format currency function
* @return {string|Function} returns a number format based on the type or an overriding formatting function
*/
export function getTooltipValueFormat( type, formatAmount ) {
switch ( type ) {
case 'currency':
return formatAmount;
case 'percent':
return '.0%';
case 'number':
return ',';
case 'average':
return ',.2r';
default:
return ',';
}
}
2019-03-21 03:25:05 +00:00
/**
* Returns query needed for a request to populate a table.
*
* @param {Object} options arguments
2019-03-21 03:25:05 +00:00
* @param {Object} options.query Query parameters in the url
* @param {Object} options.tableQuery Query parameters specific for that endpoint
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.
2019-03-21 03:25:05 +00:00
* @return {Object} Object Table data response
*/
export function getReportTableQuery( options ) {
const { query, tableQuery = {} } = options;
const filterQuery = getFilterQuery( options );
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 noIntervals = includes( noIntervalEndpoints, options.endpoint );
return {
2019-03-21 03:25:05 +00:00
orderby: query.orderby || 'date',
order: query.order || 'desc',
after: noIntervals
? undefined
: appendTimestamp( datesFromQuery.primary.after, 'start' ),
before: noIntervals
? undefined
: appendTimestamp( datesFromQuery.primary.before, 'end' ),
page: query.paged || 1,
2019-03-21 03:25:05 +00:00
per_page: query.per_page || QUERY_DEFAULTS.pageSize,
...filterQuery,
2019-03-21 03:25:05 +00:00
...tableQuery,
};
}
/**
* Returns table data needed to render a report page.
*
2019-03-21 03:25:05 +00:00
* @param {Object} options arguments
* @param {string} options.endpoint Report API Endpoint
2019-03-21 03:25:05 +00:00
* @param {Object} options.query Query parameters in the url
* @param {Object} options.select Instance of @wordpress/select
* @param {Object} options.tableQuery Query parameters specific for that endpoint
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 Table data response
*/
2019-03-21 03:25:05 +00:00
export function getReportTableData( options ) {
const { endpoint, select } = options;
const {
getReportItems,
getReportItemsError,
isReportItemsRequesting,
} = select( 'wc-api' );
2019-03-21 03:25:05 +00:00
const tableQuery = reportsUtils.getReportTableQuery( options );
const response = {
query: tableQuery,
isRequesting: false,
isError: false,
items: {
data: [],
totalResults: 0,
},
};
// Disable eslint rule requiring `items` to be defined below because the next two if statements
// depend on `getReportItems` to have been called.
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
const items = getReportItems( endpoint, tableQuery );
if ( isReportItemsRequesting( endpoint, tableQuery ) ) {
return { ...response, isRequesting: true };
} else if ( getReportItemsError( endpoint, tableQuery ) ) {
return { ...response, isError: true };
}
return { ...response, items };
}