2018-09-03 15:25:38 +00:00
|
|
|
/** @format */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-02-14 03:31:27 +00:00
|
|
|
import { find, forEach, isNull, get, includes } from 'lodash';
|
2019-01-07 10:41:46 +00:00
|
|
|
import moment from 'moment';
|
2018-09-03 15:25:38 +00:00
|
|
|
|
2018-10-30 18:57:48 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { appendTimestamp, getCurrentDates, getIntervalForQuery } from '@woocommerce/date';
|
2018-11-12 16:17:18 +00:00
|
|
|
import { flattenFilters, getActiveFiltersFromQuery, getUrlKey } from '@woocommerce/navigation';
|
2018-11-12 14:34:51 +00:00
|
|
|
import { formatCurrency } from '@woocommerce/currency';
|
2018-10-30 18:57:48 +00:00
|
|
|
|
2018-09-03 15:25:38 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-01-30 07:22:10 +00:00
|
|
|
import { MAX_PER_PAGE, QUERY_DEFAULTS } from 'wc-api/constants';
|
2018-11-16 13:33:58 +00:00
|
|
|
import * as categoriesConfig from 'analytics/report/categories/config';
|
2018-10-18 09:34:37 +00:00
|
|
|
import * as couponsConfig from 'analytics/report/coupons/config';
|
2019-01-07 11:54:42 +00:00
|
|
|
import * as customersConfig from 'analytics/report/customers/config';
|
|
|
|
import * as downloadsConfig from 'analytics/report/downloads/config';
|
2018-10-18 09:34:37 +00:00
|
|
|
import * as ordersConfig from 'analytics/report/orders/config';
|
|
|
|
import * as productsConfig from 'analytics/report/products/config';
|
2018-11-14 21:04:59 +00:00
|
|
|
import * as taxesConfig from 'analytics/report/taxes/config';
|
2018-11-22 23:12:12 +00:00
|
|
|
import * as reportsUtils from './utils';
|
2018-10-18 09:34:37 +00:00
|
|
|
|
|
|
|
const reportConfigs = {
|
2018-11-16 13:33:58 +00:00
|
|
|
categories: categoriesConfig,
|
2018-10-18 09:34:37 +00:00
|
|
|
coupons: couponsConfig,
|
2019-01-07 11:54:42 +00:00
|
|
|
customers: customersConfig,
|
|
|
|
downloads: downloadsConfig,
|
2018-10-18 09:34:37 +00:00
|
|
|
orders: ordersConfig,
|
|
|
|
products: productsConfig,
|
2018-11-14 21:04:59 +00:00
|
|
|
taxes: taxesConfig,
|
2018-10-18 09:34:37 +00:00
|
|
|
};
|
|
|
|
|
2019-02-27 16:28:18 +00:00
|
|
|
export function getFilterQuery( endpoint, query, limitBy ) {
|
2019-02-01 09:55:19 +00:00
|
|
|
if ( query.search ) {
|
2019-03-07 02:57:22 +00:00
|
|
|
const limitProperties = limitBy || [ endpoint ];
|
|
|
|
return limitProperties.reduce( ( result, limitProperty ) => {
|
|
|
|
result[ limitProperty ] = query[ limitProperty ];
|
|
|
|
return result;
|
|
|
|
}, {} );
|
2019-02-01 09:55:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 01:30:24 +00:00
|
|
|
if ( reportConfigs[ endpoint ] ) {
|
|
|
|
const { filters = [], advancedFilters = {} } = reportConfigs[ endpoint ];
|
|
|
|
return filters
|
|
|
|
.map( filter => getQueryFromConfig( filter, advancedFilters, query ) )
|
|
|
|
.reduce( ( result, configQuery ) => Object.assign( result, configQuery ), {} );
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
2018-10-18 09:34:37 +00:00
|
|
|
|
2019-02-14 03:31:27 +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.
|
|
|
|
* @returns {object} - an active filter with timestamp added to date values.
|
|
|
|
*/
|
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 ];
|
2019-01-17 00:57:11 +00:00
|
|
|
if ( 'Date' !== get( advancedFilterConfig, [ 'input', 'component' ] ) ) {
|
|
|
|
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
|
|
|
}
|
2019-01-15 00:02:24 +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 {};
|
|
|
|
}
|
|
|
|
|
2018-10-29 01:30:24 +00:00
|
|
|
if ( 'advanced' === queryValue ) {
|
2018-10-18 09:34:37 +00:00
|
|
|
const activeFilters = getActiveFiltersFromQuery( query, advancedFilters.filters );
|
|
|
|
|
|
|
|
if ( activeFilters.length === 0 ) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-01-15 02:40:12 +00:00
|
|
|
return activeFilters.map( filter => timeStampFilterDates( advancedFilters, filter ) ).reduce(
|
2018-10-18 09:34:37 +00:00
|
|
|
( result, activeFilter ) => {
|
|
|
|
const { key, rule, value } = activeFilter;
|
|
|
|
result[ getUrlKey( key, rule ) ] = value;
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
{ match: query.match || 'all' }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-12 16:17:18 +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 {};
|
|
|
|
}
|
2018-09-03 15:25:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if a report object is empty.
|
|
|
|
*
|
2019-02-14 03:31:27 +00:00
|
|
|
* @param {Object} report Report to check
|
|
|
|
* @param {String} endpoint Endpoint slug
|
2018-09-03 15:25:38 +00:00
|
|
|
* @return {Boolean} True if report is data is empty.
|
|
|
|
*/
|
2019-02-14 03:31:27 +00:00
|
|
|
export function isReportDataEmpty( report, endpoint ) {
|
2018-09-03 15:25:38 +00:00
|
|
|
if ( ! report ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ( ! report.data ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ( ! report.data.totals || isNull( report.data.totals ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-02-14 03:31:27 +00:00
|
|
|
|
|
|
|
const checkIntervals = ! includes( noIntervalEndpoints, endpoint );
|
|
|
|
if ( checkIntervals && ( ! report.data.intervals || 0 === report.data.intervals.length ) ) {
|
2018-09-03 15:25:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-17 23:06:33 +00:00
|
|
|
/**
|
|
|
|
* Constructs and returns a query associated with a Report data request.
|
|
|
|
*
|
2019-02-27 16:28:18 +00:00
|
|
|
* @param {String} endpoint Report API Endpoint
|
|
|
|
* @param {String} dataType 'primary' or 'secondary'.
|
|
|
|
* @param {Object} query Query parameters in the url.
|
2019-03-07 02:57:22 +00:00
|
|
|
* @param {Array} [limitBy] Properties used to limit the results. It will be used in the API call to send the IDs.
|
2018-10-17 23:06:33 +00:00
|
|
|
* @returns {Object} data request query parameters.
|
|
|
|
*/
|
2019-02-27 16:28:18 +00:00
|
|
|
function getRequestQuery( endpoint, dataType, query, limitBy ) {
|
2019-01-07 10:41:46 +00:00
|
|
|
const datesFromQuery = getCurrentDates( query );
|
2018-10-17 23:02:31 +00:00
|
|
|
const interval = getIntervalForQuery( query );
|
2019-02-27 16:28:18 +00:00
|
|
|
const filterQuery = getFilterQuery( endpoint, query, limitBy );
|
2019-01-07 10:41:46 +00:00
|
|
|
const end = datesFromQuery[ dataType ].before;
|
2019-02-14 03:31:27 +00:00
|
|
|
|
|
|
|
const noIntervals = includes( noIntervalEndpoints, endpoint );
|
|
|
|
return noIntervals
|
|
|
|
? { ...filterQuery }
|
|
|
|
: {
|
|
|
|
order: 'asc',
|
|
|
|
interval,
|
|
|
|
per_page: MAX_PER_PAGE,
|
|
|
|
after: appendTimestamp( datesFromQuery[ dataType ].after, 'start' ),
|
|
|
|
before: appendTimestamp( end, 'end' ),
|
|
|
|
segmentby: query.segmentby,
|
|
|
|
...filterQuery,
|
|
|
|
};
|
2018-10-17 23:02:31 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:45:01 +00:00
|
|
|
/**
|
|
|
|
* Returns summary number totals needed to render a report page.
|
|
|
|
*
|
2019-02-27 16:28:18 +00:00
|
|
|
* @param {String} endpoint Report API Endpoint
|
|
|
|
* @param {Object} query Query parameters in the url
|
|
|
|
* @param {Object} select Instance of @wordpress/select
|
2019-03-07 02:57:22 +00:00
|
|
|
* @param {Array} [limitBy] Properties used to limit the results. It will be used in the API call to send the IDs.
|
2019-02-27 16:28:18 +00:00
|
|
|
* @return {Object} Object containing summary number responses.
|
2018-10-11 18:45:01 +00:00
|
|
|
*/
|
2019-02-27 16:28:18 +00:00
|
|
|
export function getSummaryNumbers( endpoint, query, select, limitBy ) {
|
2018-12-15 12:38:54 +00:00
|
|
|
const { getReportStats, getReportStatsError, isReportStatsRequesting } = select( 'wc-api' );
|
2018-10-11 18:45:01 +00:00
|
|
|
const response = {
|
|
|
|
isRequesting: false,
|
|
|
|
isError: false,
|
|
|
|
totals: {
|
|
|
|
primary: null,
|
|
|
|
secondary: null,
|
|
|
|
},
|
|
|
|
};
|
2019-03-06 03:38:45 +00:00
|
|
|
const mappedEndpoint = 'categories' === endpoint ? 'products' : endpoint;
|
2018-10-11 18:45:01 +00:00
|
|
|
|
2019-02-27 16:28:18 +00:00
|
|
|
const primaryQuery = getRequestQuery( endpoint, 'primary', query, limitBy );
|
2019-03-06 03:38:45 +00:00
|
|
|
const primary = getReportStats( mappedEndpoint, primaryQuery );
|
|
|
|
if ( isReportStatsRequesting( mappedEndpoint, primaryQuery ) ) {
|
2018-10-11 18:45:01 +00:00
|
|
|
return { ...response, isRequesting: true };
|
2019-03-06 03:38:45 +00:00
|
|
|
} else if ( getReportStatsError( mappedEndpoint, primaryQuery ) ) {
|
2018-10-11 18:45:01 +00:00
|
|
|
return { ...response, isError: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
const primaryTotals = ( primary && primary.data && primary.data.totals ) || null;
|
|
|
|
|
2019-03-06 03:38:45 +00:00
|
|
|
const secondaryQuery = getRequestQuery( mappedEndpoint, 'secondary', query );
|
|
|
|
const secondary = getReportStats( mappedEndpoint, secondaryQuery );
|
|
|
|
if ( isReportStatsRequesting( mappedEndpoint, secondaryQuery ) ) {
|
2018-10-11 18:45:01 +00:00
|
|
|
return { ...response, isRequesting: true };
|
2019-03-06 03:38:45 +00:00
|
|
|
} else if ( getReportStatsError( mappedEndpoint, secondaryQuery ) ) {
|
2018-10-11 18:45:01 +00:00
|
|
|
return { ...response, isError: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
const secondaryTotals = ( secondary && secondary.data && secondary.data.totals ) || null;
|
|
|
|
|
|
|
|
return { ...response, totals: { primary: primaryTotals, secondary: secondaryTotals } };
|
|
|
|
}
|
|
|
|
|
2018-09-03 15:25:38 +00:00
|
|
|
/**
|
|
|
|
* Returns all of the data needed to render a chart with summary numbers on a report page.
|
|
|
|
*
|
2019-02-27 16:28:18 +00:00
|
|
|
* @param {String} endpoint Report API Endpoint
|
|
|
|
* @param {String} dataType 'primary' or 'secondary'
|
|
|
|
* @param {Object} query Query parameters in the url
|
|
|
|
* @param {Object} select Instance of @wordpress/select
|
2019-03-07 02:57:22 +00:00
|
|
|
* @param {Array} [limitBy] Properties used to limit the results. It will be used in the API call to send the IDs.
|
2018-09-03 15:25:38 +00:00
|
|
|
* @return {Object} Object containing API request information (response, fetching, and error details)
|
|
|
|
*/
|
2019-02-27 16:28:18 +00:00
|
|
|
export function getReportChartData( endpoint, dataType, query, select, limitBy ) {
|
2018-12-15 12:38:54 +00:00
|
|
|
const { getReportStats, getReportStatsError, isReportStatsRequesting } = select( 'wc-api' );
|
2019-03-06 03:38:45 +00:00
|
|
|
const mappedEndpoint = 'categories' === endpoint ? 'products' : endpoint;
|
2018-09-03 15:25:38 +00:00
|
|
|
|
|
|
|
const response = {
|
Add loading indicators, error state, and EmptyContent to the revenue report. (#347, woocommerce/woocommerce-admin#348)
* Add loading indiciators for the revenue report.
* Improve accessibility, and fix up some documentation comments.
* Fix top border on mobile
* Add EmptyContent Component and revenue error/empty states. (https://github.com/woocommerce/woocommerce-admin/pull/348)
* Add EmptyContent Component and revenue error/empty states.
* Move relative image handling to ImageAsset, combine secondary and primary action rendering, add some missing isRequired proptypes, add empty error handling.
* Handle PR Feedback: Clean up button css, set a default for illustration, fix deprecation typo, some code cleanup.
2018-09-05 16:45:49 +00:00
|
|
|
isEmpty: false,
|
2018-09-03 15:25:38 +00:00
|
|
|
isError: false,
|
|
|
|
isRequesting: false,
|
|
|
|
data: {
|
2019-02-05 12:00:37 +00:00
|
|
|
totals: {},
|
2018-09-03 15:25:38 +00:00
|
|
|
intervals: [],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-02-27 16:28:18 +00:00
|
|
|
const requestQuery = getRequestQuery( endpoint, dataType, query, limitBy );
|
2019-03-06 03:38:45 +00:00
|
|
|
const stats = getReportStats( mappedEndpoint, requestQuery );
|
2018-09-03 15:25:38 +00:00
|
|
|
|
2019-03-06 03:38:45 +00:00
|
|
|
if ( isReportStatsRequesting( mappedEndpoint, requestQuery ) ) {
|
2018-09-03 15:25:38 +00:00
|
|
|
return { ...response, isRequesting: true };
|
2019-03-06 03:38:45 +00:00
|
|
|
} else if ( getReportStatsError( mappedEndpoint, requestQuery ) ) {
|
2018-09-03 15:25:38 +00:00
|
|
|
return { ...response, isError: true };
|
2019-03-06 03:38:45 +00:00
|
|
|
} else if ( isReportDataEmpty( stats, mappedEndpoint ) ) {
|
Add loading indicators, error state, and EmptyContent to the revenue report. (#347, woocommerce/woocommerce-admin#348)
* Add loading indiciators for the revenue report.
* Improve accessibility, and fix up some documentation comments.
* Fix top border on mobile
* Add EmptyContent Component and revenue error/empty states. (https://github.com/woocommerce/woocommerce-admin/pull/348)
* Add EmptyContent Component and revenue error/empty states.
* Move relative image handling to ImageAsset, combine secondary and primary action rendering, add some missing isRequired proptypes, add empty error handling.
* Handle PR Feedback: Clean up button css, set a default for illustration, fix deprecation typo, some code cleanup.
2018-09-05 16:45:49 +00:00
|
|
|
return { ...response, isEmpty: true };
|
2018-09-03 15:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 );
|
|
|
|
|
|
|
|
for ( let i = 2; i <= totalPages; i++ ) {
|
2018-10-17 23:02:31 +00:00
|
|
|
const nextQuery = { ...requestQuery, page: i };
|
2019-03-06 03:38:45 +00:00
|
|
|
const _data = getReportStats( mappedEndpoint, nextQuery );
|
|
|
|
if ( isReportStatsRequesting( mappedEndpoint, nextQuery ) ) {
|
2018-09-03 15:25:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-03-06 03:38:45 +00:00
|
|
|
if ( getReportStatsError( mappedEndpoint, nextQuery ) ) {
|
2018-09-03 15:25:38 +00:00
|
|
|
isError = true;
|
|
|
|
isFetching = false;
|
|
|
|
break;
|
|
|
|
}
|
2018-11-22 20:49:52 +00:00
|
|
|
|
|
|
|
pagedData.push( _data );
|
|
|
|
if ( i === totalPages ) {
|
|
|
|
isFetching = false;
|
|
|
|
break;
|
2018-09-03 15:25:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 } };
|
|
|
|
}
|
2018-11-12 14:34:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a formatting function or string to be used by d3-format
|
|
|
|
*
|
|
|
|
* @param {String} type Type of number, 'currency', 'number', 'percent', 'average'
|
|
|
|
* @return {String|Function} returns a number format based on the type or an overriding formatting function
|
|
|
|
*/
|
|
|
|
export function getTooltipValueFormat( type ) {
|
|
|
|
switch ( type ) {
|
|
|
|
case 'currency':
|
|
|
|
return formatCurrency;
|
|
|
|
case 'percent':
|
|
|
|
return '.0%';
|
|
|
|
case 'number':
|
|
|
|
return ',';
|
|
|
|
case 'average':
|
|
|
|
return ',.2r';
|
|
|
|
default:
|
|
|
|
return ',';
|
|
|
|
}
|
|
|
|
}
|
2018-11-22 23:12:12 +00:00
|
|
|
|
2018-12-10 20:01:22 +00:00
|
|
|
export function getReportTableQuery( endpoint, urlQuery, query ) {
|
|
|
|
const filterQuery = getFilterQuery( endpoint, urlQuery );
|
2018-11-22 23:12:12 +00:00
|
|
|
const datesFromQuery = getCurrentDates( urlQuery );
|
|
|
|
|
|
|
|
return {
|
|
|
|
orderby: urlQuery.orderby || 'date',
|
2019-01-17 21:14:12 +00:00
|
|
|
order: urlQuery.order || 'desc',
|
2018-11-22 23:12:12 +00:00
|
|
|
after: appendTimestamp( datesFromQuery.primary.after, 'start' ),
|
|
|
|
before: appendTimestamp( datesFromQuery.primary.before, 'end' ),
|
|
|
|
page: urlQuery.page || 1,
|
|
|
|
per_page: urlQuery.per_page || QUERY_DEFAULTS.pageSize,
|
|
|
|
...filterQuery,
|
|
|
|
...query,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns table data needed to render a report page.
|
|
|
|
*
|
|
|
|
* @param {String} endpoint Report API Endpoint
|
|
|
|
* @param {Object} urlQuery Query parameters in the url
|
2019-02-01 09:55:19 +00:00
|
|
|
* @param {Object} select Instance of @wordpress/select
|
2018-11-22 23:12:12 +00:00
|
|
|
* @param {Object} query Query parameters specific for that endpoint
|
|
|
|
* @return {Object} Object Table data response
|
|
|
|
*/
|
|
|
|
export function getReportTableData( endpoint, urlQuery, select, query = {} ) {
|
2018-12-15 12:38:54 +00:00
|
|
|
const { getReportItems, getReportItemsError, isReportItemsRequesting } = select( 'wc-api' );
|
2018-11-22 23:12:12 +00:00
|
|
|
|
2018-12-10 20:01:22 +00:00
|
|
|
const tableQuery = reportsUtils.getReportTableQuery( endpoint, urlQuery, query );
|
2018-11-22 23:12:12 +00:00
|
|
|
const response = {
|
|
|
|
query: tableQuery,
|
|
|
|
isRequesting: false,
|
|
|
|
isError: false,
|
2018-12-10 15:51:11 +00:00
|
|
|
items: {
|
|
|
|
data: [],
|
2019-02-05 12:00:37 +00:00
|
|
|
totalResults: 0,
|
2018-12-10 15:51:11 +00:00
|
|
|
},
|
2018-11-22 23:12:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const items = getReportItems( endpoint, tableQuery );
|
2018-12-05 23:12:34 +00:00
|
|
|
if ( isReportItemsRequesting( endpoint, tableQuery ) ) {
|
2018-11-22 23:12:12 +00:00
|
|
|
return { ...response, isRequesting: true };
|
2018-12-15 12:38:54 +00:00
|
|
|
} else if ( getReportItemsError( endpoint, tableQuery ) ) {
|
2018-11-22 23:12:12 +00:00
|
|
|
return { ...response, isError: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
return { ...response, items };
|
|
|
|
}
|