2018-11-26 14:01:20 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-10-08 22:02:26 +00:00
|
|
|
import { IconButton } from '@wordpress/components';
|
2018-12-05 01:44:32 +00:00
|
|
|
import { applyFilters } from '@wordpress/hooks';
|
2019-04-30 09:43:55 +00:00
|
|
|
import { Component, createRef, Fragment } from '@wordpress/element';
|
2018-11-26 14:01:20 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
2019-04-30 09:43:55 +00:00
|
|
|
import { focus } from '@wordpress/dom';
|
2018-12-13 19:24:54 +00:00
|
|
|
import { withDispatch } from '@wordpress/data';
|
2019-10-08 22:02:26 +00:00
|
|
|
import { get, isEqual, noop, partial, uniq } from 'lodash';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import classnames from 'classnames';
|
2018-11-26 14:01:20 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
2019-10-08 22:02:26 +00:00
|
|
|
import { CompareButton, Search, TableCard } from '@woocommerce/components';
|
|
|
|
import DownloadIcon from './download-icon';
|
|
|
|
import {
|
|
|
|
getIdsFromQuery,
|
|
|
|
getSearchWords,
|
|
|
|
onQueryChange,
|
|
|
|
updateQueryString,
|
|
|
|
} from '@woocommerce/navigation';
|
|
|
|
import {
|
|
|
|
downloadCSVFile,
|
|
|
|
generateCSVDataFromTable,
|
|
|
|
generateCSVFileName,
|
|
|
|
} from '@woocommerce/csv-export';
|
2018-11-26 14:01:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import ReportError from 'analytics/components/report-error';
|
2019-01-30 07:22:10 +00:00
|
|
|
import { getReportChartData, getReportTableData } from 'wc-api/reports/utils';
|
2019-02-05 12:00:37 +00:00
|
|
|
import { QUERY_DEFAULTS } from 'wc-api/constants';
|
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';
|
2019-07-02 10:05:37 +00:00
|
|
|
import { recordEvent } from 'lib/tracks';
|
2019-04-30 09:43:55 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
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 );
|
|
|
|
|
2019-10-08 22:02:26 +00:00
|
|
|
const { query, compareBy } = this.props;
|
|
|
|
const selectedRows = query.filter ? getIdsFromQuery( query[ compareBy ] ) : [];
|
|
|
|
this.state = { selectedRows };
|
|
|
|
|
2018-12-22 11:46:10 +00:00
|
|
|
this.onColumnsChange = this.onColumnsChange.bind( this );
|
2019-04-30 09:43:55 +00:00
|
|
|
this.onPageChange = this.onPageChange.bind( this );
|
2019-07-09 20:38:53 +00:00
|
|
|
this.onSort = this.onSort.bind( this );
|
2019-04-30 09:43:55 +00:00
|
|
|
this.scrollPointRef = createRef();
|
2019-07-10 14:37:02 +00:00
|
|
|
this.trackTableSearch = this.trackTableSearch.bind( this );
|
2019-10-08 22:02:26 +00:00
|
|
|
this.onClickDownload = this.onClickDownload.bind( this );
|
|
|
|
this.onCompare = this.onCompare.bind( this );
|
|
|
|
this.onSearchChange = this.onSearchChange.bind( this );
|
|
|
|
this.selectRow = this.selectRow.bind( this );
|
|
|
|
this.selectAllRows = this.selectAllRows.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate( { query: prevQuery } ) {
|
|
|
|
const { compareBy, query } = this.props;
|
|
|
|
|
|
|
|
if ( query.filter || prevQuery.filter ) {
|
|
|
|
const prevIds = prevQuery.filter ? getIdsFromQuery( prevQuery[ compareBy ] ) : [];
|
|
|
|
const currentIds = query.filter ? getIdsFromQuery( query[ compareBy ] ) : [];
|
|
|
|
if ( ! isEqual( prevIds.sort(), currentIds.sort() ) ) {
|
|
|
|
/* eslint-disable react/no-did-update-set-state */
|
|
|
|
this.setState( {
|
|
|
|
selectedRows: currentIds,
|
|
|
|
} );
|
|
|
|
/* eslint-enable react/no-did-update-set-state */
|
|
|
|
}
|
|
|
|
}
|
2018-12-22 11:46:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 19:55:51 +00:00
|
|
|
onColumnsChange( shownColumns, toggledColumn ) {
|
|
|
|
const { columnPrefsKey, endpoint, getHeadersContent, updateCurrentUserData } = this.props;
|
2018-12-22 02:27:30 +00:00
|
|
|
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
|
|
|
};
|
2019-02-25 19:25:19 +00:00
|
|
|
updateCurrentUserData( userDataFields );
|
2018-12-13 19:24:54 +00:00
|
|
|
}
|
2019-07-09 19:55:51 +00:00
|
|
|
|
|
|
|
if ( toggledColumn ) {
|
|
|
|
const eventProps = {
|
|
|
|
report: endpoint,
|
|
|
|
column: toggledColumn,
|
|
|
|
status: shownColumns.includes( toggledColumn ) ? 'on' : 'off',
|
|
|
|
};
|
|
|
|
|
|
|
|
recordEvent( 'analytics_table_header_toggle', eventProps );
|
|
|
|
}
|
2018-12-22 11:46:10 +00:00
|
|
|
}
|
2018-12-13 19:24:54 +00:00
|
|
|
|
2019-07-11 17:53:47 +00:00
|
|
|
onPageChange( newPage, source ) {
|
|
|
|
const { endpoint } = this.props;
|
2019-04-30 09:43:55 +00:00
|
|
|
this.scrollPointRef.current.scrollIntoView();
|
|
|
|
const tableElement = this.scrollPointRef.current.nextSibling.querySelector(
|
|
|
|
'.woocommerce-table__table'
|
|
|
|
);
|
|
|
|
const focusableElements = focus.focusable.find( tableElement );
|
|
|
|
|
|
|
|
if ( focusableElements.length ) {
|
|
|
|
focusableElements[ 0 ].focus();
|
|
|
|
}
|
2019-07-11 17:53:47 +00:00
|
|
|
|
|
|
|
if ( source ) {
|
|
|
|
if ( 'goto' === source ) {
|
|
|
|
recordEvent( 'analytics_table_go_to_page', { report: endpoint, page: newPage } );
|
|
|
|
} else {
|
|
|
|
recordEvent( 'analytics_table_page_click', { report: endpoint, direction: source } );
|
|
|
|
}
|
|
|
|
}
|
2019-04-30 09:43:55 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 14:37:02 +00:00
|
|
|
trackTableSearch() {
|
|
|
|
const { endpoint } = this.props;
|
|
|
|
|
|
|
|
// @todo: decide if this should only fire for new tokens (not any/all changes).
|
|
|
|
recordEvent( 'analytics_table_filter', { report: endpoint } );
|
|
|
|
}
|
|
|
|
|
2019-07-09 20:38:53 +00:00
|
|
|
onSort( key, direction ) {
|
|
|
|
onQueryChange( 'sort' )( key, direction );
|
|
|
|
|
|
|
|
const { endpoint } = this.props;
|
|
|
|
const eventProps = {
|
|
|
|
report: endpoint,
|
|
|
|
column: key,
|
|
|
|
direction,
|
|
|
|
};
|
|
|
|
|
|
|
|
recordEvent( 'analytics_table_sort', eventProps );
|
|
|
|
}
|
|
|
|
|
2018-12-22 11:46:10 +00:00
|
|
|
filterShownHeaders( headers, hiddenKeys ) {
|
2019-08-16 19:05:43 +00:00
|
|
|
// If no user preferences, set visibilty based on column default.
|
|
|
|
if ( ! hiddenKeys ) {
|
|
|
|
return headers.map( header => ( {
|
|
|
|
...header,
|
|
|
|
visible: header.required || ! header.hiddenByDefault,
|
|
|
|
} ) );
|
2018-12-13 19:24:54 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 19:05:43 +00:00
|
|
|
// Set visibilty based on user preferences.
|
|
|
|
return headers.map( header => ( {
|
|
|
|
...header,
|
|
|
|
visible: header.required || ! hiddenKeys.includes( header.key ),
|
|
|
|
} ) );
|
2018-12-22 11:46:10 +00:00
|
|
|
}
|
2018-12-13 19:24:54 +00:00
|
|
|
|
2019-10-08 22:02:26 +00:00
|
|
|
onClickDownload() {
|
|
|
|
const {
|
|
|
|
endpoint,
|
|
|
|
getHeadersContent,
|
|
|
|
getRowsContent,
|
|
|
|
initiateReportExport,
|
|
|
|
query,
|
|
|
|
searchBy,
|
|
|
|
tableData,
|
|
|
|
title,
|
|
|
|
} = this.props;
|
|
|
|
const params = Object.assign( {}, query );
|
|
|
|
const { items, query: reportQuery } = tableData;
|
|
|
|
const { data, totalResults } = items;
|
|
|
|
let downloadType = 'browser';
|
|
|
|
|
|
|
|
// Delete unnecessary items from filename.
|
|
|
|
delete params.extended_info;
|
|
|
|
if ( params.search ) {
|
|
|
|
delete params[ searchBy ];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( data && data.length === totalResults ) {
|
|
|
|
downloadCSVFile(
|
|
|
|
generateCSVFileName( title, params ),
|
|
|
|
generateCSVDataFromTable( getHeadersContent(), getRowsContent( data ) )
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
downloadType = 'email';
|
|
|
|
initiateReportExport( endpoint, title, reportQuery );
|
|
|
|
}
|
|
|
|
|
|
|
|
recordEvent( 'analytics_table_download', {
|
|
|
|
report: endpoint,
|
|
|
|
rows: totalResults,
|
|
|
|
downloadType,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
onCompare() {
|
|
|
|
const { compareBy, compareParam } = this.props;
|
|
|
|
const { selectedRows } = this.state;
|
|
|
|
if ( compareBy ) {
|
|
|
|
onQueryChange( 'compare' )( compareBy, compareParam, selectedRows.join( ',' ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onSearchChange( values ) {
|
|
|
|
const { baseSearchQuery, compareParam, searchBy } = this.props;
|
|
|
|
// A comma is used as a separator between search terms, so we want to escape
|
|
|
|
// any comma they contain.
|
|
|
|
const labels = values.map( v => v.label.replace( ',', '%2C' ) );
|
|
|
|
if ( labels.length ) {
|
|
|
|
updateQueryString( {
|
|
|
|
filter: undefined,
|
|
|
|
[ compareParam ]: undefined,
|
|
|
|
[ searchBy ]: undefined,
|
|
|
|
...baseSearchQuery,
|
|
|
|
search: uniq( labels ).join( ',' ),
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
updateQueryString( {
|
|
|
|
search: undefined,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.trackTableSearch();
|
|
|
|
}
|
|
|
|
|
|
|
|
selectAllRows( event ) {
|
|
|
|
const { ids } = this.props;
|
|
|
|
if ( event.target.checked ) {
|
|
|
|
this.setState( {
|
|
|
|
selectedRows: ids,
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
this.setState( {
|
|
|
|
selectedRows: [],
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
selectRow( i, event ) {
|
|
|
|
const { ids } = this.props;
|
|
|
|
if ( event.target.checked ) {
|
|
|
|
this.setState( ( { selectedRows } ) => ( {
|
|
|
|
selectedRows: uniq( [ ids[ i ], ...selectedRows ] ),
|
|
|
|
} ) );
|
|
|
|
} else {
|
|
|
|
this.setState( ( { selectedRows } ) => {
|
|
|
|
const index = selectedRows.indexOf( ids[ i ] );
|
|
|
|
return {
|
|
|
|
selectedRows: [ ...selectedRows.slice( 0, index ), ...selectedRows.slice( index + 1 ) ],
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getCheckbox( i ) {
|
|
|
|
const { ids = [] } = this.props;
|
|
|
|
const { selectedRows } = this.state;
|
|
|
|
const isChecked = -1 !== selectedRows.indexOf( ids[ i ] );
|
|
|
|
return {
|
|
|
|
display: (
|
|
|
|
<input type="checkbox" onChange={ partial( this.selectRow, i ) } checked={ isChecked } />
|
|
|
|
),
|
|
|
|
value: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getAllCheckbox() {
|
|
|
|
const { ids = [] } = this.props;
|
|
|
|
const { selectedRows } = this.state;
|
|
|
|
const hasData = ids.length > 0;
|
|
|
|
const isAllChecked = hasData && ids.length === selectedRows.length;
|
|
|
|
return {
|
|
|
|
cellClassName: 'is-checkbox-column',
|
|
|
|
key: 'compare',
|
|
|
|
label: (
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
onChange={ this.selectAllRows }
|
|
|
|
aria-label={ __( 'Select All' ) }
|
|
|
|
checked={ isAllChecked }
|
|
|
|
disabled={ ! hasData }
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
required: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-26 14:01:20 +00:00
|
|
|
render() {
|
2019-10-08 22:02:26 +00:00
|
|
|
const { selectedRows } = this.state;
|
2018-11-26 14:01:20 +00:00
|
|
|
const {
|
|
|
|
getHeadersContent,
|
|
|
|
getRowsContent,
|
|
|
|
getSummary,
|
2019-02-26 09:28:50 +00:00
|
|
|
isRequesting,
|
2018-11-26 14:01:20 +00:00
|
|
|
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,
|
2019-10-08 22:02:26 +00:00
|
|
|
compareBy,
|
|
|
|
searchBy,
|
|
|
|
labels = {},
|
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 />;
|
|
|
|
}
|
|
|
|
|
2019-02-26 09:28:50 +00:00
|
|
|
const isLoading = isRequesting || tableData.isRequesting || primaryData.isRequesting;
|
2019-02-05 12:00:37 +00:00
|
|
|
const totals = get( primaryData, [ 'data', 'totals' ], {} );
|
|
|
|
const totalResults = items.totalResults;
|
2019-04-22 03:15:01 +00:00
|
|
|
const downloadable = 0 < totalResults;
|
2019-10-08 22:02:26 +00:00
|
|
|
const searchWords = getSearchWords( query );
|
|
|
|
const searchedLabels = searchWords.map( v => ( { id: v, label: v } ) );
|
2019-06-17 04:38:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter report table.
|
|
|
|
*
|
|
|
|
* Enables manipulation of data used to create a report table.
|
|
|
|
*
|
|
|
|
* @param {object} reportTableData - data used to create the table.
|
|
|
|
* @param {string} reportTableData.endpoint - table api endpoint.
|
|
|
|
* @param {array} reportTableData.headers - table headers data.
|
|
|
|
* @param {array} reportTableData.rows - table rows data.
|
|
|
|
* @param {object} reportTableData.totals - total aggregates for request.
|
|
|
|
* @param {array} reportTableData.summary - summary numbers data.
|
2019-06-23 23:58:29 +00:00
|
|
|
* @param {object} reportTableData.items - response from api requerst.
|
2019-06-17 04:38:42 +00:00
|
|
|
*/
|
2019-10-08 22:02:26 +00:00
|
|
|
const filteredTableProps = applyFilters( TABLE_FILTER, {
|
2019-06-17 04:38:42 +00:00
|
|
|
endpoint,
|
2018-12-05 01:44:32 +00:00
|
|
|
headers: getHeadersContent(),
|
2019-01-10 17:10:31 +00:00
|
|
|
rows: getRowsContent( items.data ),
|
2019-06-17 04:38:42 +00:00
|
|
|
totals,
|
2018-12-14 23:56:52 +00:00
|
|
|
summary: getSummary ? getSummary( totals, totalResults ) : null,
|
2019-06-17 04:38:42 +00:00
|
|
|
items,
|
2018-12-05 01:44:32 +00:00
|
|
|
} );
|
2019-10-08 22:02:26 +00:00
|
|
|
let { headers, rows } = filteredTableProps;
|
|
|
|
const { summary } = filteredTableProps;
|
|
|
|
|
|
|
|
// Add in selection for comparisons.
|
|
|
|
if ( compareBy ) {
|
|
|
|
rows = rows.map( ( row, i ) => {
|
|
|
|
return [ this.getCheckbox( i ), ...row ];
|
|
|
|
} );
|
|
|
|
headers = [ this.getAllCheckbox(), ...headers ];
|
|
|
|
}
|
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 );
|
2019-10-08 22:02:26 +00:00
|
|
|
const className = classnames( 'woocommerce-report-table', 'woocommerce-analytics__card', {
|
|
|
|
'has-compare': !! compareBy,
|
|
|
|
'has-search': !! searchBy,
|
|
|
|
} );
|
2018-12-13 19:24:54 +00:00
|
|
|
|
2018-11-26 14:01:20 +00:00
|
|
|
return (
|
2019-04-30 09:43:55 +00:00
|
|
|
<Fragment>
|
|
|
|
<div
|
|
|
|
className="woocommerce-report-table__scroll-point"
|
|
|
|
ref={ this.scrollPointRef }
|
|
|
|
aria-hidden
|
|
|
|
/>
|
|
|
|
<TableCard
|
2019-10-08 22:02:26 +00:00
|
|
|
className={ className }
|
|
|
|
actions={ [
|
|
|
|
compareBy && (
|
|
|
|
<CompareButton
|
|
|
|
key="compare"
|
|
|
|
className="woocommerce-table__compare"
|
|
|
|
count={ selectedRows.length }
|
|
|
|
helpText={
|
|
|
|
labels.helpText ||
|
|
|
|
__( 'Check at least two items below to compare', 'woocommerce-admin' )
|
|
|
|
}
|
|
|
|
onClick={ this.onCompare }
|
|
|
|
disabled={ ! downloadable }
|
|
|
|
>
|
|
|
|
{ labels.compareButton || __( 'Compare', 'woocommerce-admin' ) }
|
|
|
|
</CompareButton>
|
|
|
|
),
|
|
|
|
searchBy && (
|
|
|
|
<Search
|
|
|
|
allowFreeTextSearch={ true }
|
|
|
|
inlineTags
|
|
|
|
key="search"
|
|
|
|
onChange={ this.onSearchChange }
|
|
|
|
placeholder={
|
|
|
|
labels.placeholder || __( 'Search by item name', 'woocommerce-admin' )
|
|
|
|
}
|
|
|
|
selected={ searchedLabels }
|
|
|
|
showClearButton={ true }
|
|
|
|
type={ searchBy }
|
|
|
|
disabled={ ! downloadable }
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
downloadable && (
|
|
|
|
<IconButton
|
|
|
|
key="download"
|
|
|
|
className="woocommerce-table__download-button"
|
|
|
|
disabled={ isLoading }
|
|
|
|
onClick={ this.onClickDownload }
|
|
|
|
isLink
|
|
|
|
>
|
|
|
|
<DownloadIcon />
|
|
|
|
<span className="woocommerce-table__download-button__label">
|
|
|
|
{ labels.downloadButton || __( 'Download', 'woocommerce-admin' ) }
|
|
|
|
</span>
|
|
|
|
</IconButton>
|
|
|
|
),
|
|
|
|
] }
|
2019-04-30 09:43:55 +00:00
|
|
|
headers={ filteredHeaders }
|
|
|
|
isLoading={ isLoading }
|
|
|
|
onQueryChange={ onQueryChange }
|
|
|
|
onColumnsChange={ this.onColumnsChange }
|
2019-07-09 20:38:53 +00:00
|
|
|
onSort={ this.onSort }
|
2019-04-30 09:43:55 +00:00
|
|
|
onPageChange={ this.onPageChange }
|
|
|
|
rows={ rows }
|
|
|
|
rowsPerPage={ parseInt( query.per_page ) || QUERY_DEFAULTS.pageSize }
|
|
|
|
summary={ summary }
|
|
|
|
totalRows={ totalResults }
|
|
|
|
{ ...tableProps }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
2018-11-26 14:01:20 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReportTable.propTypes = {
|
2019-10-08 22:02:26 +00:00
|
|
|
/**
|
|
|
|
* Pass in query parameters to be included in the path when onSearch creates a new url.
|
|
|
|
*/
|
|
|
|
baseSearchQuery: PropTypes.object,
|
|
|
|
/**
|
|
|
|
* The string to use as a query parameter when comparing row items.
|
|
|
|
*/
|
|
|
|
compareBy: PropTypes.string,
|
|
|
|
/**
|
|
|
|
* Url query parameter compare function operates on
|
|
|
|
*/
|
|
|
|
compareParam: PropTypes.string,
|
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-11-12 18:15:55 +00:00
|
|
|
* `taxes` endpoint (ie: `/wc-analytics/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,
|
2019-10-08 22:02:26 +00:00
|
|
|
/**
|
|
|
|
* Custom labels for table header actions.
|
|
|
|
*/
|
|
|
|
labels: PropTypes.shape( {
|
|
|
|
compareButton: PropTypes.string,
|
|
|
|
downloadButton: PropTypes.string,
|
|
|
|
helpText: PropTypes.string,
|
|
|
|
placeholder: 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
|
|
|
*/
|
2019-02-05 12:00:37 +00:00
|
|
|
primaryData: PropTypes.object,
|
2019-10-08 22:02:26 +00:00
|
|
|
/**
|
|
|
|
* The string to use as a query parameter when searching row items.
|
|
|
|
*/
|
|
|
|
searchBy: PropTypes.string,
|
2018-11-26 14:01:20 +00:00
|
|
|
/**
|
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 = {
|
2019-02-05 12:00:37 +00:00
|
|
|
primaryData: {},
|
|
|
|
tableData: {
|
|
|
|
items: {
|
|
|
|
data: [],
|
|
|
|
totalResults: 0,
|
|
|
|
},
|
|
|
|
query: {},
|
|
|
|
},
|
2018-11-26 14:01:20 +00:00
|
|
|
tableQuery: {},
|
2019-10-08 22:02:26 +00:00
|
|
|
compareParam: 'filter',
|
|
|
|
downloadable: false,
|
|
|
|
onSearch: noop,
|
|
|
|
baseSearchQuery: {},
|
2018-11-26 14:01:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( ( select, props ) => {
|
2019-02-26 09:28:50 +00:00
|
|
|
const {
|
|
|
|
endpoint,
|
|
|
|
getSummary,
|
|
|
|
isRequesting,
|
2019-10-08 22:02:26 +00:00
|
|
|
itemIdField,
|
2019-02-26 09:28:50 +00:00
|
|
|
query,
|
|
|
|
tableData,
|
|
|
|
tableQuery,
|
|
|
|
columnPrefsKey,
|
2019-03-21 03:25:05 +00:00
|
|
|
filters,
|
|
|
|
advancedFilters,
|
2019-02-26 09:28:50 +00:00
|
|
|
} = props;
|
2019-03-21 10:35:00 +00:00
|
|
|
|
|
|
|
let userPrefColumns = [];
|
|
|
|
if ( columnPrefsKey ) {
|
|
|
|
const { getCurrentUserData } = select( 'wc-api' );
|
|
|
|
const userData = getCurrentUserData();
|
|
|
|
|
2019-09-23 21:47:08 +00:00
|
|
|
userPrefColumns =
|
|
|
|
userData && userData[ columnPrefsKey ] ? userData[ columnPrefsKey ] : userPrefColumns;
|
2019-03-21 10:35:00 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 09:28:50 +00:00
|
|
|
if ( isRequesting || ( query.search && ! ( query[ endpoint ] && query[ endpoint ].length ) ) ) {
|
2019-03-21 10:35:00 +00:00
|
|
|
return {
|
|
|
|
userPrefColumns,
|
|
|
|
};
|
2019-02-05 12:00:37 +00:00
|
|
|
}
|
2019-03-21 03:25:05 +00:00
|
|
|
// Variations and Category charts are powered by the /reports/products/stats endpoint.
|
2019-02-07 22:39:56 +00:00
|
|
|
const chartEndpoint = [ 'variations', 'categories' ].includes( endpoint )
|
|
|
|
? 'products'
|
|
|
|
: endpoint;
|
2018-12-03 03:40:57 +00:00
|
|
|
const primaryData = getSummary
|
2019-03-21 03:25:05 +00:00
|
|
|
? getReportChartData( {
|
|
|
|
endpoint: chartEndpoint,
|
|
|
|
dataType: 'primary',
|
|
|
|
query,
|
|
|
|
select,
|
|
|
|
filters,
|
|
|
|
advancedFilters,
|
|
|
|
tableQuery,
|
|
|
|
} )
|
2018-12-03 03:40:57 +00:00
|
|
|
: {};
|
2019-03-21 03:25:05 +00:00
|
|
|
const queriedTableData =
|
|
|
|
tableData ||
|
|
|
|
getReportTableData( { endpoint, query, select, tableQuery, filters, advancedFilters } );
|
2018-12-12 12:55:10 +00:00
|
|
|
const extendedTableData = extendTableData( select, props, queriedTableData );
|
2018-11-26 14:01:20 +00:00
|
|
|
|
2019-03-21 10:35:00 +00:00
|
|
|
return {
|
2018-11-26 14:01:20 +00:00
|
|
|
primaryData,
|
2019-10-08 22:02:26 +00:00
|
|
|
ids: itemIdField ? extendedTableData.items.data.map( item => item[ itemIdField ] ) : [],
|
2018-12-12 12:55:10 +00:00
|
|
|
tableData: extendedTableData,
|
2019-01-24 01:36:11 +00:00
|
|
|
query: { ...tableQuery, ...query },
|
2019-03-21 10:35:00 +00:00
|
|
|
userPrefColumns,
|
2018-11-26 14:01:20 +00:00
|
|
|
};
|
2018-12-13 19:24:54 +00:00
|
|
|
} ),
|
|
|
|
withDispatch( dispatch => {
|
2019-10-08 22:02:26 +00:00
|
|
|
const { initiateReportExport, updateCurrentUserData } = dispatch( 'wc-api' );
|
2018-12-13 19:24:54 +00:00
|
|
|
|
|
|
|
return {
|
2019-10-08 22:02:26 +00:00
|
|
|
initiateReportExport,
|
2018-12-13 19:24:54 +00:00
|
|
|
updateCurrentUserData,
|
|
|
|
};
|
2018-11-26 14:01:20 +00:00
|
|
|
} )
|
|
|
|
)( ReportTable );
|