diff --git a/plugins/woocommerce-admin/client/analytics/components/leaderboard/index.js b/plugins/woocommerce-admin/client/analytics/components/leaderboard/index.js index 88d9b83a554..10a14ead0fb 100644 --- a/plugins/woocommerce-admin/client/analytics/components/leaderboard/index.js +++ b/plugins/woocommerce-admin/client/analytics/components/leaderboard/index.js @@ -99,7 +99,12 @@ Leaderboard.propTypes = { export default compose( withSelect( ( select, props ) => { const { endpoint, tableQuery, query } = props; - const tableData = getReportTableData( endpoint, query, select, tableQuery ); + const tableData = getReportTableData( { + endpoint, + query, + select, + tableQuery, + } ); return { ...tableData }; } ) diff --git a/plugins/woocommerce-admin/client/analytics/components/report-chart/index.js b/plugins/woocommerce-admin/client/analytics/components/report-chart/index.js index 408ea9ea9bb..e7e04033256 100644 --- a/plugins/woocommerce-admin/client/analytics/components/report-chart/index.js +++ b/plugins/woocommerce-admin/client/analytics/components/report-chart/index.js @@ -252,7 +252,7 @@ ReportChart.defaultProps = { export default compose( withSelect( ( select, props ) => { - const { endpoint, filters, isRequesting, limitProperties, query } = props; + const { endpoint, filters, isRequesting, limitProperties, query, advancedFilters } = props; const limitBy = limitProperties || [ endpoint ]; const selectedFilter = getSelectedFilter( filters, query ); const filterParam = get( selectedFilter, [ 'settings', 'param' ] ); @@ -276,16 +276,32 @@ export default compose( }; } + const primaryData = getReportChartData( { + endpoint, + dataType: 'primary', + query, + select, + limitBy, + filters, + advancedFilters, + } ); + if ( 'item-comparison' === chartMode ) { - const primaryData = getReportChartData( endpoint, 'primary', query, select, limitBy ); return { ...newProps, primaryData, }; } - const primaryData = getReportChartData( endpoint, 'primary', query, select, limitBy ); - const secondaryData = getReportChartData( endpoint, 'secondary', query, select, limitBy ); + const secondaryData = getReportChartData( { + endpoint, + dataType: 'secondary', + query, + select, + limitBy, + filters, + advancedFilters, + } ); return { ...newProps, primaryData, diff --git a/plugins/woocommerce-admin/client/analytics/components/report-summary/index.js b/plugins/woocommerce-admin/client/analytics/components/report-summary/index.js index 3100c064b93..94a2440e172 100644 --- a/plugins/woocommerce-admin/client/analytics/components/report-summary/index.js +++ b/plugins/woocommerce-admin/client/analytics/components/report-summary/index.js @@ -149,7 +149,7 @@ ReportSummary.defaultProps = { export default compose( withSelect( ( select, props ) => { - const { endpoint, isRequesting, limitProperties, query } = props; + const { endpoint, isRequesting, limitProperties, query, filters, advancedFilters } = props; const limitBy = limitProperties || [ endpoint ]; if ( isRequesting ) { @@ -164,7 +164,14 @@ export default compose( }; } - const summaryData = getSummaryNumbers( endpoint, query, select, limitBy ); + const summaryData = getSummaryNumbers( { + endpoint, + query, + select, + limitBy, + filters, + advancedFilters, + } ); return { summaryData, diff --git a/plugins/woocommerce-admin/client/analytics/components/report-table/index.js b/plugins/woocommerce-admin/client/analytics/components/report-table/index.js index 9ec59ffac7e..16a3ba1cbb8 100644 --- a/plugins/woocommerce-admin/client/analytics/components/report-table/index.js +++ b/plugins/woocommerce-admin/client/analytics/components/report-table/index.js @@ -199,6 +199,8 @@ export default compose( tableData, tableQuery, columnPrefsKey, + filters, + advancedFilters, } = props; let userPrefColumns = []; @@ -214,13 +216,24 @@ export default compose( userPrefColumns, }; } + // Variations and Category charts are powered by the /reports/products/stats endpoint. const chartEndpoint = [ 'variations', 'categories' ].includes( endpoint ) ? 'products' : endpoint; const primaryData = getSummary - ? getReportChartData( chartEndpoint, 'primary', query, select ) + ? getReportChartData( { + endpoint: chartEndpoint, + dataType: 'primary', + query, + select, + filters, + advancedFilters, + tableQuery, + } ) : {}; - const queriedTableData = tableData || getReportTableData( endpoint, query, select, tableQuery ); + const queriedTableData = + tableData || + getReportTableData( { endpoint, query, select, tableQuery, filters, advancedFilters } ); const extendedTableData = extendTableData( select, props, queriedTableData ); return { diff --git a/plugins/woocommerce-admin/client/analytics/report/categories/index.js b/plugins/woocommerce-admin/client/analytics/report/categories/index.js index 6d013324482..82c8f93e390 100644 --- a/plugins/woocommerce-admin/client/analytics/report/categories/index.js +++ b/plugins/woocommerce-admin/client/analytics/report/categories/index.js @@ -59,17 +59,18 @@ export default class CategoriesReport extends Component { ) : ( - + ) } ); diff --git a/plugins/woocommerce-admin/client/analytics/report/categories/table.js b/plugins/woocommerce-admin/client/analytics/report/categories/table.js index 509b3d83b40..23388bbcff9 100644 --- a/plugins/woocommerce-admin/client/analytics/report/categories/table.js +++ b/plugins/woocommerce-admin/client/analytics/report/categories/table.js @@ -134,7 +134,7 @@ class CategoriesReportTable extends Component { } render() { - const { isRequesting, query } = this.props; + const { isRequesting, query, filters } = this.props; const labels = { helpText: __( 'Check at least two categories below to compare', 'woocommerce-admin' ), @@ -160,6 +160,7 @@ class CategoriesReportTable extends Component { } } title={ __( 'Categories', 'woocommerce-admin' ) } columnPrefsKey="categories_report_columns" + filters={ filters } /> ); } diff --git a/plugins/woocommerce-admin/client/analytics/report/coupons/index.js b/plugins/woocommerce-admin/client/analytics/report/coupons/index.js index 756fc25e243..e5373d77a7c 100644 --- a/plugins/woocommerce-admin/client/analytics/report/coupons/index.js +++ b/plugins/woocommerce-admin/client/analytics/report/coupons/index.js @@ -56,6 +56,7 @@ export default class CouponsReport extends Component { isRequesting={ isRequesting } query={ chartQuery } selectedChart={ getSelectedChart( query.chart, charts ) } + filters={ filters } /> - + ); } diff --git a/plugins/woocommerce-admin/client/analytics/report/coupons/table.js b/plugins/woocommerce-admin/client/analytics/report/coupons/table.js index b1871274d9d..2d87977be22 100644 --- a/plugins/woocommerce-admin/client/analytics/report/coupons/table.js +++ b/plugins/woocommerce-admin/client/analytics/report/coupons/table.js @@ -157,7 +157,7 @@ export default class CouponsReportTable extends Component { } render() { - const { isRequesting, query } = this.props; + const { isRequesting, query, filters } = this.props; return ( ); } diff --git a/plugins/woocommerce-admin/client/analytics/report/customers/index.js b/plugins/woocommerce-admin/client/analytics/report/customers/index.js index 9d9a8a9fa22..c234639f3e1 100644 --- a/plugins/woocommerce-admin/client/analytics/report/customers/index.js +++ b/plugins/woocommerce-admin/client/analytics/report/customers/index.js @@ -34,7 +34,12 @@ export default class CustomersReport extends Component { showDatePicker={ false } advancedFilters={ advancedFilters } /> - + ); } diff --git a/plugins/woocommerce-admin/client/analytics/report/customers/table.js b/plugins/woocommerce-admin/client/analytics/report/customers/table.js index 7479a68f9e4..9259d3556c7 100644 --- a/plugins/woocommerce-admin/client/analytics/report/customers/table.js +++ b/plugins/woocommerce-admin/client/analytics/report/customers/table.js @@ -218,7 +218,7 @@ export default class CustomersReportTable extends Component { } render() { - const { isRequesting, query } = this.props; + const { isRequesting, query, filters, advancedFilters } = this.props; return ( ); } diff --git a/plugins/woocommerce-admin/client/analytics/report/downloads/index.js b/plugins/woocommerce-admin/client/analytics/report/downloads/index.js index 0a274981034..d4ade18fb6c 100644 --- a/plugins/woocommerce-admin/client/analytics/report/downloads/index.js +++ b/plugins/woocommerce-admin/client/analytics/report/downloads/index.js @@ -36,6 +36,8 @@ export default class DownloadsReport extends Component { endpoint="downloads" query={ query } selectedChart={ getSelectedChart( query.chart, charts ) } + filters={ filters } + advancedFilters={ advancedFilters } /> + - ); } diff --git a/plugins/woocommerce-admin/client/analytics/report/downloads/table.js b/plugins/woocommerce-admin/client/analytics/report/downloads/table.js index febaeccc321..2ccb9b9539b 100644 --- a/plugins/woocommerce-admin/client/analytics/report/downloads/table.js +++ b/plugins/woocommerce-admin/client/analytics/report/downloads/table.js @@ -150,7 +150,7 @@ export default class CouponsReportTable extends Component { } render() { - const { query } = this.props; + const { query, filters, advancedFilters } = this.props; return ( ); } diff --git a/plugins/woocommerce-admin/client/analytics/report/orders/index.js b/plugins/woocommerce-admin/client/analytics/report/orders/index.js index 84d6946fae2..2a73fad101b 100644 --- a/plugins/woocommerce-admin/client/analytics/report/orders/index.js +++ b/plugins/woocommerce-admin/client/analytics/report/orders/index.js @@ -36,6 +36,8 @@ export default class OrdersReport extends Component { endpoint="orders" query={ query } selectedChart={ getSelectedChart( query.chart, charts ) } + filters={ filters } + advancedFilters={ advancedFilters } /> + - ); } diff --git a/plugins/woocommerce-admin/client/analytics/report/orders/table.js b/plugins/woocommerce-admin/client/analytics/report/orders/table.js index ece9d6e8689..06cead1a1b5 100644 --- a/plugins/woocommerce-admin/client/analytics/report/orders/table.js +++ b/plugins/woocommerce-admin/client/analytics/report/orders/table.js @@ -250,7 +250,7 @@ export default class OrdersReportTable extends Component { } render() { - const { query } = this.props; + const { query, filters, advancedFilters } = this.props; return ( ); } diff --git a/plugins/woocommerce-admin/client/analytics/report/products/index.js b/plugins/woocommerce-admin/client/analytics/report/products/index.js index 80ac27bf5dc..d29ec93043e 100644 --- a/plugins/woocommerce-admin/client/analytics/report/products/index.js +++ b/plugins/woocommerce-admin/client/analytics/report/products/index.js @@ -76,6 +76,7 @@ class ProductsReport extends Component { isRequesting={ isRequesting } query={ chartQuery } selectedChart={ getSelectedChart( query.chart, charts ) } + filters={ filters } /> ) : ( - + ) } ); diff --git a/plugins/woocommerce-admin/client/analytics/report/products/table-variations.js b/plugins/woocommerce-admin/client/analytics/report/products/table-variations.js index 7adf3b6bf03..951d0bae1e8 100644 --- a/plugins/woocommerce-admin/client/analytics/report/products/table-variations.js +++ b/plugins/woocommerce-admin/client/analytics/report/products/table-variations.js @@ -169,7 +169,7 @@ export default class VariationsReportTable extends Component { } render() { - const { baseSearchQuery, isRequesting, query } = this.props; + const { baseSearchQuery, isRequesting, query, filters } = this.props; const labels = { helpText: __( 'Check at least two variations below to compare', 'woocommerce-admin' ), @@ -199,6 +199,7 @@ export default class VariationsReportTable extends Component { } } title={ __( 'Variations', 'woocommerce-admin' ) } columnPrefsKey="variations_report_columns" + filters={ filters } /> ); } diff --git a/plugins/woocommerce-admin/client/analytics/report/products/table.js b/plugins/woocommerce-admin/client/analytics/report/products/table.js index a10c14070ec..7166b752716 100644 --- a/plugins/woocommerce-admin/client/analytics/report/products/table.js +++ b/plugins/woocommerce-admin/client/analytics/report/products/table.js @@ -235,7 +235,7 @@ class ProductsReportTable extends Component { } render() { - const { query, isRequesting, baseSearchQuery, hideCompare } = this.props; + const { query, isRequesting, baseSearchQuery, hideCompare, filters } = this.props; const labels = { helpText: __( 'Check at least two products below to compare', 'woocommerce-admin' ), @@ -259,15 +259,11 @@ class ProductsReportTable extends Component { orderby: query.orderby || 'items_sold', order: query.order || 'desc', extended_info: true, - /** - * @TODO: Add this parameter because the filterQuery will be derived from the wrong config - * because it will always look for products config, not categories. The solution is to pass - * down the configs explicitly. - */ - categories: query.categories, + segmentby: query.segmentby, } } title={ __( 'Products', 'woocommerce-admin' ) } columnPrefsKey="products_report_columns" + filters={ filters } /> ); } diff --git a/plugins/woocommerce-admin/client/analytics/report/stock/index.js b/plugins/woocommerce-admin/client/analytics/report/stock/index.js index 7a69b1619c2..9bfb94895d2 100644 --- a/plugins/woocommerce-admin/client/analytics/report/stock/index.js +++ b/plugins/woocommerce-admin/client/analytics/report/stock/index.js @@ -28,7 +28,7 @@ export default class StockReport extends Component { showDatePicker={ showDatePicker } filters={ filters } /> - + ); } diff --git a/plugins/woocommerce-admin/client/analytics/report/stock/table.js b/plugins/woocommerce-admin/client/analytics/report/stock/table.js index db575349b63..4ad49b2208b 100644 --- a/plugins/woocommerce-admin/client/analytics/report/stock/table.js +++ b/plugins/woocommerce-admin/client/analytics/report/stock/table.js @@ -127,7 +127,7 @@ export default class StockReportTable extends Component { } render() { - const { query } = this.props; + const { query, filters } = this.props; return ( ); } diff --git a/plugins/woocommerce-admin/client/analytics/report/taxes/index.js b/plugins/woocommerce-admin/client/analytics/report/taxes/index.js index 2910f87c7a7..88f6e280002 100644 --- a/plugins/woocommerce-admin/client/analytics/report/taxes/index.js +++ b/plugins/woocommerce-admin/client/analytics/report/taxes/index.js @@ -53,6 +53,7 @@ export default class TaxesReport extends Component { isRequesting={ isRequesting } query={ chartQuery } selectedChart={ getSelectedChart( query.chart, charts ) } + filters={ filters } /> - + ); } diff --git a/plugins/woocommerce-admin/client/analytics/report/taxes/table.js b/plugins/woocommerce-admin/client/analytics/report/taxes/table.js index 76757947be6..79f8c6b6f25 100644 --- a/plugins/woocommerce-admin/client/analytics/report/taxes/table.js +++ b/plugins/woocommerce-admin/client/analytics/report/taxes/table.js @@ -143,7 +143,7 @@ export default class TaxesReportTable extends Component { } render() { - const { isRequesting, query } = this.props; + const { isRequesting, query, filters } = this.props; return ( ); } diff --git a/plugins/woocommerce-admin/client/wc-api/reports/utils.js b/plugins/woocommerce-admin/client/wc-api/reports/utils.js index 9a9f4a06c61..213f4b054a4 100644 --- a/plugins/woocommerce-admin/client/wc-api/reports/utils.js +++ b/plugins/woocommerce-admin/client/wc-api/reports/utils.js @@ -17,26 +17,21 @@ import { formatCurrency } from '@woocommerce/currency'; * Internal dependencies */ import { MAX_PER_PAGE, QUERY_DEFAULTS } from 'wc-api/constants'; -import * as categoriesConfig from 'analytics/report/categories/config'; -import * as couponsConfig from 'analytics/report/coupons/config'; -import * as customersConfig from 'analytics/report/customers/config'; -import * as downloadsConfig from 'analytics/report/downloads/config'; -import * as ordersConfig from 'analytics/report/orders/config'; -import * as productsConfig from 'analytics/report/products/config'; -import * as taxesConfig from 'analytics/report/taxes/config'; import * as reportsUtils from './utils'; -const reportConfigs = { - categories: categoriesConfig, - coupons: couponsConfig, - customers: customersConfig, - downloads: downloadsConfig, - orders: ordersConfig, - products: productsConfig, - taxes: taxesConfig, -}; - -export function getFilterQuery( endpoint, query, limitBy ) { +/** + * Add filters and advanced filters values to a query object. + * + * @param {Objedt} options arguments + * @param {String} options.endpoint Report API Endpoint + * @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 + * @returns {Object} A query object with the values from filters and advanced fitlters applied. + */ +export function getFilterQuery( options ) { + const { endpoint, query, limitBy, filters = [], advancedFilters = {} } = options; if ( query.search ) { const limitProperties = limitBy || [ endpoint ]; return limitProperties.reduce( ( result, limitProperty ) => { @@ -45,13 +40,9 @@ export function getFilterQuery( endpoint, query, limitBy ) { }, {} ); } - if ( reportConfigs[ endpoint ] ) { - const { filters = [], advancedFilters = {} } = reportConfigs[ endpoint ]; - return filters - .map( filter => getQueryFromConfig( filter, advancedFilters, query ) ) - .reduce( ( result, configQuery ) => Object.assign( result, configQuery ), {} ); - } - return {}; + return filters + .map( filter => getQueryFromConfig( filter, advancedFilters, query ) ) + .reduce( ( result, configQuery ) => Object.assign( result, configQuery ), {} ); } // Some stats endpoints don't have interval data, so they can ignore after/before params and omit that part of the response. @@ -165,17 +156,18 @@ export function isReportDataEmpty( report, endpoint ) { /** * Constructs and returns a query associated with a Report data request. - * - * @param {String} endpoint Report API Endpoint - * @param {String} dataType 'primary' or 'secondary'. - * @param {Object} query Query parameters in the url. - * @param {Array} [limitBy] Properties used to limit the results. It will be used in the API call to send the IDs. + * @param {Objedt} options arguments + * @param {String} options.endpoint Report API Endpoint + * @param {String} options.dataType 'primary' or 'secondary'. + * @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. * @returns {Object} data request query parameters. */ -function getRequestQuery( endpoint, dataType, query, limitBy ) { +function getRequestQuery( options ) { + const { endpoint, dataType, query } = options; const datesFromQuery = getCurrentDates( query ); const interval = getIntervalForQuery( query ); - const filterQuery = getFilterQuery( endpoint, query, limitBy ); + const filterQuery = getFilterQuery( options ); const end = datesFromQuery[ dataType ].before; const noIntervals = includes( noIntervalEndpoints, endpoint ); @@ -195,13 +187,15 @@ function getRequestQuery( endpoint, dataType, query, limitBy ) { /** * Returns summary number totals needed to render a report page. * - * @param {String} endpoint Report API Endpoint - * @param {Object} query Query parameters in the url - * @param {Object} select Instance of @wordpress/select - * @param {Array} [limitBy] Properties used to limit the results. It will be used in the API call to send the IDs. + * @param {Objedt} options arguments + * @param {String} options.endpoint Report API Endpoint + * @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. * @return {Object} Object containing summary number responses. */ -export function getSummaryNumbers( endpoint, query, select, limitBy ) { +export function getSummaryNumbers( options ) { + const { endpoint, select } = options; const { getReportStats, getReportStatsError, isReportStatsRequesting } = select( 'wc-api' ); const response = { isRequesting: false, @@ -211,23 +205,22 @@ export function getSummaryNumbers( endpoint, query, select, limitBy ) { secondary: null, }, }; - const mappedEndpoint = 'categories' === endpoint ? 'products' : endpoint; - const primaryQuery = getRequestQuery( endpoint, 'primary', query, limitBy ); - const primary = getReportStats( mappedEndpoint, primaryQuery ); - if ( isReportStatsRequesting( mappedEndpoint, primaryQuery ) ) { + const primaryQuery = getRequestQuery( { ...options, dataType: 'primary' } ); + const primary = getReportStats( endpoint, primaryQuery ); + if ( isReportStatsRequesting( endpoint, primaryQuery ) ) { return { ...response, isRequesting: true }; - } else if ( getReportStatsError( mappedEndpoint, primaryQuery ) ) { + } else if ( getReportStatsError( endpoint, primaryQuery ) ) { return { ...response, isError: true }; } const primaryTotals = ( primary && primary.data && primary.data.totals ) || null; - const secondaryQuery = getRequestQuery( mappedEndpoint, 'secondary', query ); - const secondary = getReportStats( mappedEndpoint, secondaryQuery ); - if ( isReportStatsRequesting( mappedEndpoint, secondaryQuery ) ) { + const secondaryQuery = getRequestQuery( { ...options, dataType: 'secondary' } ); + const secondary = getReportStats( endpoint, secondaryQuery ); + if ( isReportStatsRequesting( endpoint, secondaryQuery ) ) { return { ...response, isRequesting: true }; - } else if ( getReportStatsError( mappedEndpoint, secondaryQuery ) ) { + } else if ( getReportStatsError( endpoint, secondaryQuery ) ) { return { ...response, isError: true }; } @@ -238,17 +231,17 @@ export function getSummaryNumbers( endpoint, query, select, limitBy ) { /** * Returns all of the data needed to render a chart with summary numbers on a report page. - * - * @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 - * @param {Array} [limitBy] Properties used to limit the results. It will be used in the API call to send the IDs. + * @param {Objedt} options arguments + * @param {String} options.endpoint Report API Endpoint + * @param {String} options.dataType 'primary' or 'secondary' + * @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. * @return {Object} Object containing API request information (response, fetching, and error details) */ -export function getReportChartData( endpoint, dataType, query, select, limitBy ) { +export function getReportChartData( options ) { + const { endpoint, select } = options; const { getReportStats, getReportStatsError, isReportStatsRequesting } = select( 'wc-api' ); - const mappedEndpoint = 'categories' === endpoint ? 'products' : endpoint; const response = { isEmpty: false, @@ -260,14 +253,14 @@ export function getReportChartData( endpoint, dataType, query, select, limitBy ) }, }; - const requestQuery = getRequestQuery( endpoint, dataType, query, limitBy ); - const stats = getReportStats( mappedEndpoint, requestQuery ); + const requestQuery = getRequestQuery( options ); + const stats = getReportStats( endpoint, requestQuery ); - if ( isReportStatsRequesting( mappedEndpoint, requestQuery ) ) { + if ( isReportStatsRequesting( endpoint, requestQuery ) ) { return { ...response, isRequesting: true }; - } else if ( getReportStatsError( mappedEndpoint, requestQuery ) ) { + } else if ( getReportStatsError( endpoint, requestQuery ) ) { return { ...response, isError: true }; - } else if ( isReportDataEmpty( stats, mappedEndpoint ) ) { + } else if ( isReportDataEmpty( stats, endpoint ) ) { return { ...response, isEmpty: true }; } @@ -284,11 +277,11 @@ export function getReportChartData( endpoint, dataType, query, select, limitBy ) for ( let i = 2; i <= totalPages; i++ ) { const nextQuery = { ...requestQuery, page: i }; - const _data = getReportStats( mappedEndpoint, nextQuery ); - if ( isReportStatsRequesting( mappedEndpoint, nextQuery ) ) { + const _data = getReportStats( endpoint, nextQuery ); + if ( isReportStatsRequesting( endpoint, nextQuery ) ) { continue; } - if ( getReportStatsError( mappedEndpoint, nextQuery ) ) { + if ( getReportStatsError( endpoint, nextQuery ) ) { isError = true; isFetching = false; break; @@ -336,35 +329,46 @@ export function getTooltipValueFormat( type ) { } } -export function getReportTableQuery( endpoint, urlQuery, query ) { - const filterQuery = getFilterQuery( endpoint, urlQuery ); - const datesFromQuery = getCurrentDates( urlQuery ); +/** + * Returns query needed for a request to populate a table. + * + * @param {Objedt} options arguments + * @param {Object} options.query Query parameters in the url + * @param {Object} options.tableQuery Query parameters specific for that endpoint + * @return {Object} Object Table data response + */ +export function getReportTableQuery( options ) { + const { query, tableQuery = {} } = options; + const filterQuery = getFilterQuery( options ); + const datesFromQuery = getCurrentDates( query ); return { - orderby: urlQuery.orderby || 'date', - order: urlQuery.order || 'desc', + orderby: query.orderby || 'date', + order: query.order || 'desc', after: appendTimestamp( datesFromQuery.primary.after, 'start' ), before: appendTimestamp( datesFromQuery.primary.before, 'end' ), - page: urlQuery.page || 1, - per_page: urlQuery.per_page || QUERY_DEFAULTS.pageSize, + page: query.page || 1, + per_page: query.per_page || QUERY_DEFAULTS.pageSize, ...filterQuery, - ...query, + ...tableQuery, }; } /** * Returns table data needed to render a report page. * - * @param {String} endpoint Report API Endpoint - * @param {Object} urlQuery Query parameters in the url - * @param {Object} select Instance of @wordpress/select - * @param {Object} query Query parameters specific for that endpoint + * @param {Object} options arguments + * @param {String} options.endpoint Report API Endpoint + * @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 * @return {Object} Object Table data response */ -export function getReportTableData( endpoint, urlQuery, select, query = {} ) { +export function getReportTableData( options ) { + const { endpoint, select } = options; const { getReportItems, getReportItemsError, isReportItemsRequesting } = select( 'wc-api' ); - const tableQuery = reportsUtils.getReportTableQuery( endpoint, urlQuery, query ); + const tableQuery = reportsUtils.getReportTableQuery( options ); const response = { query: tableQuery, isRequesting: false,