2018-11-26 14:01:20 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import { withSelect } from '@wordpress/data';
|
|
|
|
import { get, orderBy } from 'lodash';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { TableCard } from '@woocommerce/components';
|
|
|
|
import { onQueryChange } from '@woocommerce/navigation';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import ReportError from 'analytics/components/report-error';
|
|
|
|
import { getReportChartData, getReportTableData } from 'store/reports/utils';
|
|
|
|
|
|
|
|
class ReportTable extends Component {
|
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
getHeadersContent,
|
|
|
|
getRowsContent,
|
|
|
|
getSummary,
|
|
|
|
itemIdField,
|
|
|
|
primaryData,
|
|
|
|
tableData,
|
|
|
|
// These two props are not used in the render function, but are destructured
|
|
|
|
// so they are not included in the `tableProps` variable.
|
|
|
|
endpoint,
|
|
|
|
tableQuery,
|
|
|
|
...tableProps
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const { items, query } = tableData;
|
|
|
|
|
|
|
|
const isError = tableData.isError || primaryData.isError;
|
|
|
|
|
|
|
|
if ( isError ) {
|
|
|
|
return <ReportError isError />;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isRequesting = tableData.isRequesting || primaryData.isRequesting;
|
|
|
|
|
|
|
|
const headers = getHeadersContent();
|
2018-11-29 14:03:04 +00:00
|
|
|
const orderedItems = orderBy( items.data, query.orderby, query.order );
|
2018-12-03 03:40:57 +00:00
|
|
|
const ids = itemIdField ? orderedItems.map( item => item[ itemIdField ] ) : null;
|
2018-11-26 14:01:20 +00:00
|
|
|
const rows = getRowsContent( orderedItems );
|
|
|
|
const totals = get( primaryData, [ 'data', 'totals' ], null );
|
2018-11-29 14:03:04 +00:00
|
|
|
const summary = getSummary ? getSummary( totals ) : null;
|
2018-11-26 14:01:20 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<TableCard
|
|
|
|
downloadable
|
|
|
|
headers={ headers }
|
|
|
|
ids={ ids }
|
|
|
|
isLoading={ isRequesting }
|
|
|
|
onQueryChange={ onQueryChange }
|
|
|
|
rows={ rows }
|
|
|
|
rowsPerPage={ query.per_page }
|
|
|
|
summary={ summary }
|
2018-11-29 14:03:04 +00:00
|
|
|
totalRows={ items.totalCount || 0 }
|
2018-11-26 14:01:20 +00:00
|
|
|
{ ...tableProps }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReportTable.propTypes = {
|
|
|
|
/**
|
|
|
|
* The endpoint to use in API calls.
|
|
|
|
*/
|
2018-12-03 03:40:57 +00:00
|
|
|
endpoint: PropTypes.string,
|
2018-11-26 14:01:20 +00:00
|
|
|
/**
|
|
|
|
* A function that returns the headers object to build the table.
|
|
|
|
*/
|
|
|
|
getHeadersContent: PropTypes.func.isRequired,
|
|
|
|
/**
|
|
|
|
* A function that returns the rows array to build the table.
|
|
|
|
*/
|
|
|
|
getRowsContent: PropTypes.func.isRequired,
|
|
|
|
/**
|
|
|
|
* A function that returns the summary object to build the table.
|
|
|
|
*/
|
|
|
|
getSummary: PropTypes.func,
|
|
|
|
/**
|
|
|
|
* The name of the property in the item object which contains the id.
|
|
|
|
*/
|
2018-12-03 03:40:57 +00:00
|
|
|
itemIdField: PropTypes.string,
|
2018-11-26 14:01:20 +00:00
|
|
|
/**
|
|
|
|
* Primary data of that report.
|
|
|
|
*/
|
|
|
|
primaryData: PropTypes.object.isRequired,
|
|
|
|
/**
|
|
|
|
* Table data of that report.
|
|
|
|
*/
|
|
|
|
tableData: PropTypes.object.isRequired,
|
|
|
|
/**
|
|
|
|
* Properties to be added to the query sent to the report table endpoint.
|
|
|
|
*/
|
|
|
|
tableQuery: PropTypes.object,
|
|
|
|
/**
|
|
|
|
* String to display as the title of the table.
|
|
|
|
*/
|
|
|
|
title: PropTypes.string.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
ReportTable.defaultProps = {
|
2018-12-03 03:40:57 +00:00
|
|
|
tableData: null,
|
2018-11-26 14:01:20 +00:00
|
|
|
tableQuery: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( ( select, props ) => {
|
2018-12-03 03:40:57 +00:00
|
|
|
const { endpoint, getSummary, query, tableData, tableQuery } = props;
|
2018-11-26 02:58:19 +00:00
|
|
|
const chartEndpoint = 'variations' === endpoint ? 'products' : endpoint;
|
2018-12-03 03:40:57 +00:00
|
|
|
const primaryData = getSummary
|
|
|
|
? getReportChartData( chartEndpoint, 'primary', query, select )
|
|
|
|
: {};
|
|
|
|
const queriedTableData = tableData || getReportTableData( endpoint, query, select, tableQuery );
|
2018-11-26 14:01:20 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
primaryData,
|
2018-12-03 03:40:57 +00:00
|
|
|
tableData: queriedTableData,
|
2018-11-26 14:01:20 +00:00
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( ReportTable );
|