2018-11-26 14:01:20 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-12-05 01:44:32 +00:00
|
|
|
import { applyFilters } from '@wordpress/hooks';
|
2018-11-26 14:01:20 +00:00
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import { compose } from '@wordpress/compose';
|
2018-12-13 19:24:54 +00:00
|
|
|
import { withDispatch } from '@wordpress/data';
|
2019-01-10 17:10:31 +00:00
|
|
|
import { get } from 'lodash';
|
2018-11-26 14:01:20 +00:00
|
|
|
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';
|
2018-12-05 17:10:54 +00:00
|
|
|
import withSelect from 'wc-api/with-select';
|
2018-12-12 14:25:36 +00:00
|
|
|
import { extendTableData } from './utils';
|
2018-11-26 14:01:20 +00:00
|
|
|
|
2018-12-05 01:44:32 +00:00
|
|
|
const TABLE_FILTER = 'woocommerce_admin_report_table';
|
|
|
|
|
2018-12-22 11:46:10 +00:00
|
|
|
/**
|
|
|
|
* Component that extends `TableCard` to facilitate its usage in reports.
|
|
|
|
*/
|
2018-11-26 14:01:20 +00:00
|
|
|
class ReportTable extends Component {
|
2018-12-22 11:46:10 +00:00
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
|
|
|
|
|
|
|
this.onColumnsChange = this.onColumnsChange.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
onColumnsChange( shownColumns ) {
|
2018-12-22 02:27:30 +00:00
|
|
|
const { columnPrefsKey, getHeadersContent } = this.props;
|
|
|
|
const columns = getHeadersContent().map( header => header.key );
|
|
|
|
const hiddenColumns = columns.filter( column => ! shownColumns.includes( column ) );
|
2018-12-13 19:24:54 +00:00
|
|
|
|
|
|
|
if ( columnPrefsKey ) {
|
|
|
|
const userDataFields = {
|
2018-12-22 02:27:30 +00:00
|
|
|
[ columnPrefsKey ]: hiddenColumns,
|
2018-12-13 19:24:54 +00:00
|
|
|
};
|
|
|
|
this.props.updateCurrentUserData( userDataFields );
|
|
|
|
}
|
2018-12-22 11:46:10 +00:00
|
|
|
}
|
2018-12-13 19:24:54 +00:00
|
|
|
|
2018-12-22 11:46:10 +00:00
|
|
|
filterShownHeaders( headers, hiddenKeys ) {
|
2018-12-22 02:27:30 +00:00
|
|
|
if ( ! hiddenKeys ) {
|
2018-12-13 19:24:54 +00:00
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
return headers.map( header => {
|
2018-12-22 02:27:30 +00:00
|
|
|
const hidden = hiddenKeys.includes( header.key ) && ! header.required;
|
2018-12-13 20:34:23 +00:00
|
|
|
return { ...header, hiddenByDefault: hidden };
|
2018-12-13 19:24:54 +00:00
|
|
|
} );
|
2018-12-22 11:46:10 +00:00
|
|
|
}
|
2018-12-13 19:24:54 +00:00
|
|
|
|
2018-11-26 14:01:20 +00:00
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
getHeadersContent,
|
|
|
|
getRowsContent,
|
|
|
|
getSummary,
|
|
|
|
itemIdField,
|
|
|
|
primaryData,
|
|
|
|
tableData,
|
2018-12-22 11:46:10 +00:00
|
|
|
// These props are not used in the render function, but are destructured
|
2018-11-26 14:01:20 +00:00
|
|
|
// so they are not included in the `tableProps` variable.
|
|
|
|
endpoint,
|
|
|
|
tableQuery,
|
2018-12-13 19:24:54 +00:00
|
|
|
userPrefColumns,
|
2018-11-26 14:01:20 +00:00
|
|
|
...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 totals = get( primaryData, [ 'data', 'totals' ], null );
|
2018-12-14 23:56:52 +00:00
|
|
|
const totalResults = items.totalResults || 0;
|
2018-12-05 01:44:32 +00:00
|
|
|
const { headers, ids, rows, summary } = applyFilters( TABLE_FILTER, {
|
|
|
|
endpoint: endpoint,
|
|
|
|
headers: getHeadersContent(),
|
2019-01-10 17:10:31 +00:00
|
|
|
ids: itemIdField ? items.data.map( item => item[ itemIdField ] ) : null,
|
|
|
|
rows: getRowsContent( items.data ),
|
2018-12-05 01:44:32 +00:00
|
|
|
totals: totals,
|
2018-12-14 23:56:52 +00:00
|
|
|
summary: getSummary ? getSummary( totals, totalResults ) : null,
|
2018-12-05 01:44:32 +00:00
|
|
|
} );
|
2018-11-26 14:01:20 +00:00
|
|
|
|
2018-12-13 19:24:54 +00:00
|
|
|
// Hide any headers based on user prefs, if loaded.
|
|
|
|
const filteredHeaders = this.filterShownHeaders( headers, userPrefColumns );
|
|
|
|
|
2018-11-26 14:01:20 +00:00
|
|
|
return (
|
|
|
|
<TableCard
|
|
|
|
downloadable
|
2018-12-13 19:24:54 +00:00
|
|
|
headers={ filteredHeaders }
|
2018-11-26 14:01:20 +00:00
|
|
|
ids={ ids }
|
|
|
|
isLoading={ isRequesting }
|
|
|
|
onQueryChange={ onQueryChange }
|
2018-12-13 19:24:54 +00:00
|
|
|
onColumnsChange={ this.onColumnsChange }
|
2018-11-26 14:01:20 +00:00
|
|
|
rows={ rows }
|
2018-12-04 00:00:13 +00:00
|
|
|
rowsPerPage={ parseInt( query.per_page ) }
|
2018-11-26 14:01:20 +00:00
|
|
|
summary={ summary }
|
2018-12-14 23:56:52 +00:00
|
|
|
totalRows={ totalResults }
|
2018-11-26 14:01:20 +00:00
|
|
|
{ ...tableProps }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReportTable.propTypes = {
|
2018-12-13 19:24:54 +00:00
|
|
|
/**
|
|
|
|
* The key for user preferences settings for column visibility.
|
|
|
|
*/
|
|
|
|
columnPrefsKey: PropTypes.string,
|
2018-11-26 14:01:20 +00:00
|
|
|
/**
|
2018-12-22 11:46:10 +00:00
|
|
|
* The endpoint to use in API calls to populate the table rows and summary.
|
|
|
|
* For example, if `taxes` is provided, data will be fetched from the report
|
2019-01-18 02:52:58 +00:00
|
|
|
* `taxes` endpoint (ie: `/wc/v4/reports/taxes` and `/wc/v4/reports/taxes/stats`).
|
2018-12-22 11:46:10 +00:00
|
|
|
* If the provided endpoint doesn't exist, an error will be shown to the user
|
|
|
|
* with `ReportError`.
|
2018-11-26 14:01:20 +00:00
|
|
|
*/
|
2018-12-03 03:40:57 +00:00
|
|
|
endpoint: PropTypes.string,
|
2018-12-12 12:55:10 +00:00
|
|
|
/**
|
2018-12-14 17:12:46 +00:00
|
|
|
* Name of the methods available via `select( 'wc-api' )` that will be used to
|
|
|
|
* load more data for table items. If omitted, no call will be made and only
|
|
|
|
* the data returned by the reports endpoint will be used.
|
2018-12-12 12:55:10 +00:00
|
|
|
*/
|
|
|
|
extendItemsMethodNames: PropTypes.shape( {
|
2018-12-15 12:15:13 +00:00
|
|
|
getError: PropTypes.string,
|
2018-12-12 12:55:10 +00:00
|
|
|
isRequesting: PropTypes.string,
|
|
|
|
load: 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
|
|
|
/**
|
2018-12-22 11:46:10 +00:00
|
|
|
* Primary data of that report. If it's not provided, it will be automatically
|
|
|
|
* loaded via the provided `endpoint`.
|
2018-11-26 14:01:20 +00:00
|
|
|
*/
|
|
|
|
primaryData: PropTypes.object.isRequired,
|
|
|
|
/**
|
2018-12-22 11:46:10 +00:00
|
|
|
* Table data of that report. If it's not provided, it will be automatically
|
|
|
|
* loaded via the provided `endpoint`.
|
2018-11-26 14:01:20 +00:00
|
|
|
*/
|
|
|
|
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-10 15:51:11 +00:00
|
|
|
tableData: {},
|
2018-11-26 14:01:20 +00:00
|
|
|
tableQuery: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( ( select, props ) => {
|
2018-12-13 19:24:54 +00:00
|
|
|
const { endpoint, getSummary, query, tableData, tableQuery, columnPrefsKey } = 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-12-12 12:55:10 +00:00
|
|
|
const extendedTableData = extendTableData( select, props, queriedTableData );
|
2018-11-26 14:01:20 +00:00
|
|
|
|
2018-12-13 19:24:54 +00:00
|
|
|
const selectProps = {
|
2018-11-26 14:01:20 +00:00
|
|
|
primaryData,
|
2018-12-12 12:55:10 +00:00
|
|
|
tableData: extendedTableData,
|
2019-01-24 01:36:11 +00:00
|
|
|
query: { ...tableQuery, ...query },
|
2018-11-26 14:01:20 +00:00
|
|
|
};
|
2018-12-13 19:24:54 +00:00
|
|
|
|
|
|
|
if ( columnPrefsKey ) {
|
|
|
|
const { getCurrentUserData } = select( 'wc-api' );
|
|
|
|
const userData = getCurrentUserData();
|
|
|
|
|
|
|
|
selectProps.userPrefColumns = userData[ columnPrefsKey ];
|
|
|
|
}
|
|
|
|
|
|
|
|
return selectProps;
|
|
|
|
} ),
|
|
|
|
withDispatch( dispatch => {
|
|
|
|
const { updateCurrentUserData } = dispatch( 'wc-api' );
|
|
|
|
|
|
|
|
return {
|
|
|
|
updateCurrentUserData,
|
|
|
|
};
|
2018-11-26 14:01:20 +00:00
|
|
|
} )
|
|
|
|
)( ReportTable );
|