pass in filters
This commit is contained in:
parent
56a60d17dd
commit
b29b232a46
|
@ -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 };
|
||||
} )
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -59,17 +59,18 @@ export default class CategoriesReport extends Component {
|
|||
<ReportFilters query={ query } path={ path } filters={ filters } />
|
||||
<ReportSummary
|
||||
charts={ charts }
|
||||
endpoint="categories"
|
||||
endpoint="products"
|
||||
isRequesting={ isRequesting }
|
||||
limitProperties={ isSingleCategoryView ? [ 'products', 'categories' ] : [ 'categories' ] }
|
||||
query={ chartQuery }
|
||||
selectedChart={ getSelectedChart( query.chart, charts ) }
|
||||
filters={ filters }
|
||||
/>
|
||||
<ReportChart
|
||||
filters={ filters }
|
||||
charts={ charts }
|
||||
mode={ mode }
|
||||
endpoint="categories"
|
||||
endpoint="products"
|
||||
limitProperties={ isSingleCategoryView ? [ 'products', 'categories' ] : [ 'categories' ] }
|
||||
path={ path }
|
||||
query={ chartQuery }
|
||||
|
@ -80,12 +81,17 @@ export default class CategoriesReport extends Component {
|
|||
{ isSingleCategoryView ? (
|
||||
<ProductsReportTable
|
||||
isRequesting={ isRequesting }
|
||||
query={ query }
|
||||
query={ chartQuery }
|
||||
baseSearchQuery={ { filter: 'single_category' } }
|
||||
hideCompare={ isSingleCategoryView }
|
||||
filters={ filters }
|
||||
/>
|
||||
) : (
|
||||
<CategoriesReportTable isRequesting={ isRequesting } query={ query } />
|
||||
<CategoriesReportTable
|
||||
isRequesting={ isRequesting }
|
||||
query={ query }
|
||||
filters={ filters }
|
||||
/>
|
||||
) }
|
||||
</Fragment>
|
||||
);
|
||||
|
|
|
@ -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 }
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -56,6 +56,7 @@ export default class CouponsReport extends Component {
|
|||
isRequesting={ isRequesting }
|
||||
query={ chartQuery }
|
||||
selectedChart={ getSelectedChart( query.chart, charts ) }
|
||||
filters={ filters }
|
||||
/>
|
||||
<ReportChart
|
||||
filters={ filters }
|
||||
|
@ -68,7 +69,7 @@ export default class CouponsReport extends Component {
|
|||
itemsLabel={ itemsLabel }
|
||||
selectedChart={ getSelectedChart( query.chart, charts ) }
|
||||
/>
|
||||
<CouponsReportTable isRequesting={ isRequesting } query={ query } />
|
||||
<CouponsReportTable isRequesting={ isRequesting } query={ query } filters={ filters } />
|
||||
</Fragment>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -157,7 +157,7 @@ export default class CouponsReportTable extends Component {
|
|||
}
|
||||
|
||||
render() {
|
||||
const { isRequesting, query } = this.props;
|
||||
const { isRequesting, query, filters } = this.props;
|
||||
|
||||
return (
|
||||
<ReportTable
|
||||
|
@ -177,6 +177,7 @@ export default class CouponsReportTable extends Component {
|
|||
} }
|
||||
title={ __( 'Coupons', 'woocommerce-admin' ) }
|
||||
columnPrefsKey="coupons_report_columns"
|
||||
filters={ filters }
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -34,7 +34,12 @@ export default class CustomersReport extends Component {
|
|||
showDatePicker={ false }
|
||||
advancedFilters={ advancedFilters }
|
||||
/>
|
||||
<CustomersReportTable isRequesting={ isRequesting } query={ tableQuery } />
|
||||
<CustomersReportTable
|
||||
isRequesting={ isRequesting }
|
||||
query={ tableQuery }
|
||||
filters={ filters }
|
||||
advancedFilters={ advancedFilters }
|
||||
/>
|
||||
</Fragment>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -218,7 +218,7 @@ export default class CustomersReportTable extends Component {
|
|||
}
|
||||
|
||||
render() {
|
||||
const { isRequesting, query } = this.props;
|
||||
const { isRequesting, query, filters, advancedFilters } = this.props;
|
||||
|
||||
return (
|
||||
<ReportTable
|
||||
|
@ -233,6 +233,8 @@ export default class CustomersReportTable extends Component {
|
|||
searchBy="customers"
|
||||
title={ __( 'Customers', 'woocommerce-admin' ) }
|
||||
columnPrefsKey="customers_report_columns"
|
||||
filters={ filters }
|
||||
advancedFilters={ advancedFilters }
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -36,6 +36,8 @@ export default class DownloadsReport extends Component {
|
|||
endpoint="downloads"
|
||||
query={ query }
|
||||
selectedChart={ getSelectedChart( query.chart, charts ) }
|
||||
filters={ filters }
|
||||
advancedFilters={ advancedFilters }
|
||||
/>
|
||||
<ReportChart
|
||||
charts={ charts }
|
||||
|
@ -43,8 +45,14 @@ export default class DownloadsReport extends Component {
|
|||
path={ path }
|
||||
query={ query }
|
||||
selectedChart={ getSelectedChart( query.chart, charts ) }
|
||||
filters={ filters }
|
||||
advancedFilters={ advancedFilters }
|
||||
/>
|
||||
<DownloadsReportTable
|
||||
query={ query }
|
||||
filters={ filters }
|
||||
advancedFilters={ advancedFilters }
|
||||
/>
|
||||
<DownloadsReportTable query={ query } />
|
||||
</Fragment>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -150,7 +150,7 @@ export default class CouponsReportTable extends Component {
|
|||
}
|
||||
|
||||
render() {
|
||||
const { query } = this.props;
|
||||
const { query, filters, advancedFilters } = this.props;
|
||||
|
||||
return (
|
||||
<ReportTable
|
||||
|
@ -164,6 +164,8 @@ export default class CouponsReportTable extends Component {
|
|||
} }
|
||||
title={ __( 'Downloads', 'woocommerce-admin' ) }
|
||||
columnPrefsKey="downloads_report_columns"
|
||||
filters={ filters }
|
||||
advancedFilters={ advancedFilters }
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -36,6 +36,8 @@ export default class OrdersReport extends Component {
|
|||
endpoint="orders"
|
||||
query={ query }
|
||||
selectedChart={ getSelectedChart( query.chart, charts ) }
|
||||
filters={ filters }
|
||||
advancedFilters={ advancedFilters }
|
||||
/>
|
||||
<ReportChart
|
||||
charts={ charts }
|
||||
|
@ -43,8 +45,14 @@ export default class OrdersReport extends Component {
|
|||
path={ path }
|
||||
query={ query }
|
||||
selectedChart={ getSelectedChart( query.chart, charts ) }
|
||||
filters={ filters }
|
||||
advancedFilters={ advancedFilters }
|
||||
/>
|
||||
<OrdersReportTable
|
||||
query={ query }
|
||||
filters={ filters }
|
||||
advancedFilters={ advancedFilters }
|
||||
/>
|
||||
<OrdersReportTable query={ query } />
|
||||
</Fragment>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -250,7 +250,7 @@ export default class OrdersReportTable extends Component {
|
|||
}
|
||||
|
||||
render() {
|
||||
const { query } = this.props;
|
||||
const { query, filters, advancedFilters } = this.props;
|
||||
|
||||
return (
|
||||
<ReportTable
|
||||
|
@ -264,6 +264,8 @@ export default class OrdersReportTable extends Component {
|
|||
} }
|
||||
title={ __( 'Orders', 'woocommerce-admin' ) }
|
||||
columnPrefsKey="orders_report_columns"
|
||||
filters={ filters }
|
||||
advancedFilters={ advancedFilters }
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -76,6 +76,7 @@ class ProductsReport extends Component {
|
|||
isRequesting={ isRequesting }
|
||||
query={ chartQuery }
|
||||
selectedChart={ getSelectedChart( query.chart, charts ) }
|
||||
filters={ filters }
|
||||
/>
|
||||
<ReportChart
|
||||
mode={ mode }
|
||||
|
@ -93,9 +94,10 @@ class ProductsReport extends Component {
|
|||
baseSearchQuery={ { filter: 'single_product' } }
|
||||
isRequesting={ isRequesting }
|
||||
query={ query }
|
||||
filters={ filters }
|
||||
/>
|
||||
) : (
|
||||
<ProductsReportTable isRequesting={ isRequesting } query={ query } />
|
||||
<ProductsReportTable isRequesting={ isRequesting } query={ query } filters={ filters } />
|
||||
) }
|
||||
</Fragment>
|
||||
);
|
||||
|
|
|
@ -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 }
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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 }
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ export default class StockReport extends Component {
|
|||
showDatePicker={ showDatePicker }
|
||||
filters={ filters }
|
||||
/>
|
||||
<StockReportTable query={ query } />
|
||||
<StockReportTable query={ query } filters={ filters } />
|
||||
</Fragment>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -127,7 +127,7 @@ export default class StockReportTable extends Component {
|
|||
}
|
||||
|
||||
render() {
|
||||
const { query } = this.props;
|
||||
const { query, filters } = this.props;
|
||||
|
||||
return (
|
||||
<ReportTable
|
||||
|
@ -142,6 +142,7 @@ export default class StockReportTable extends Component {
|
|||
type: query.type || 'all',
|
||||
} }
|
||||
title={ __( 'Stock', 'woocommerce-admin' ) }
|
||||
filters={ filters }
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -53,6 +53,7 @@ export default class TaxesReport extends Component {
|
|||
isRequesting={ isRequesting }
|
||||
query={ chartQuery }
|
||||
selectedChart={ getSelectedChart( query.chart, charts ) }
|
||||
filters={ filters }
|
||||
/>
|
||||
<ReportChart
|
||||
filters={ filters }
|
||||
|
@ -65,7 +66,7 @@ export default class TaxesReport extends Component {
|
|||
itemsLabel={ itemsLabel }
|
||||
selectedChart={ getSelectedChart( query.chart, charts ) }
|
||||
/>
|
||||
<TaxesReportTable isRequesting={ isRequesting } query={ query } />
|
||||
<TaxesReportTable isRequesting={ isRequesting } query={ query } filters={ filters } />
|
||||
</Fragment>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -143,7 +143,7 @@ export default class TaxesReportTable extends Component {
|
|||
}
|
||||
|
||||
render() {
|
||||
const { isRequesting, query } = this.props;
|
||||
const { isRequesting, query, filters } = this.props;
|
||||
|
||||
return (
|
||||
<ReportTable
|
||||
|
@ -161,6 +161,7 @@ export default class TaxesReportTable extends Component {
|
|||
} }
|
||||
title={ __( 'Taxes', 'woocommerce-admin' ) }
|
||||
columnPrefsKey="taxes_report_columns"
|
||||
filters={ filters }
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue