2018-10-16 16:07:43 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
2018-10-30 18:57:48 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { getDateParamsFromQuery } from '@woocommerce/date';
|
2018-11-05 21:02:04 +00:00
|
|
|
import { getNewPath } from '@woocommerce/navigation';
|
2018-10-30 18:57:48 +00:00
|
|
|
import { SummaryList, SummaryListPlaceholder, SummaryNumber } from '@woocommerce/components';
|
2019-01-29 16:48:46 +00:00
|
|
|
import { calculateDelta, formatValue } from '@woocommerce/number';
|
|
|
|
import { formatCurrency } from '@woocommerce/currency';
|
2018-10-30 18:57:48 +00:00
|
|
|
|
2018-10-16 16:07:43 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-01-30 07:22:10 +00:00
|
|
|
import { getSummaryNumbers } from 'wc-api/reports/utils';
|
2018-10-30 18:57:48 +00:00
|
|
|
import ReportError from 'analytics/components/report-error';
|
2018-12-04 17:22:46 +00:00
|
|
|
import withSelect from 'wc-api/with-select';
|
2018-10-18 20:45:36 +00:00
|
|
|
|
2018-12-22 11:46:10 +00:00
|
|
|
/**
|
|
|
|
* Component to render summary numbers in reports.
|
|
|
|
*/
|
2018-12-07 21:13:02 +00:00
|
|
|
export class ReportSummary extends Component {
|
2019-02-07 09:57:47 +00:00
|
|
|
formatVal( val, type ) {
|
|
|
|
return 'currency' === type ? formatCurrency( val ) : formatValue( type, val );
|
|
|
|
}
|
|
|
|
|
|
|
|
getValues( key, type ) {
|
|
|
|
const { emptySearchResults, summaryData } = this.props;
|
|
|
|
const { totals } = summaryData;
|
|
|
|
|
|
|
|
const primaryValue = emptySearchResults ? 0 : totals.primary[ key ];
|
|
|
|
const secondaryValue = emptySearchResults ? 0 : totals.secondary[ key ];
|
|
|
|
|
|
|
|
return {
|
|
|
|
delta: calculateDelta( primaryValue, secondaryValue ),
|
|
|
|
prevValue: this.formatVal( secondaryValue, type ),
|
|
|
|
value: this.formatVal( primaryValue, type ),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-16 16:07:43 +00:00
|
|
|
render() {
|
2019-02-27 14:43:34 +00:00
|
|
|
const { charts, isRequesting, query, selectedChart, summaryData } = this.props;
|
|
|
|
const { isError, isRequesting: isSummaryDataRequesting } = summaryData;
|
2018-10-17 18:56:50 +00:00
|
|
|
|
2018-12-07 21:13:02 +00:00
|
|
|
if ( isError ) {
|
2018-10-17 18:56:50 +00:00
|
|
|
return <ReportError isError />;
|
|
|
|
}
|
|
|
|
|
2019-02-27 14:43:34 +00:00
|
|
|
if ( isRequesting || isSummaryDataRequesting ) {
|
2018-10-16 16:07:43 +00:00
|
|
|
return <SummaryListPlaceholder numberOfItems={ charts.length } />;
|
|
|
|
}
|
|
|
|
|
2018-12-07 21:13:02 +00:00
|
|
|
const { compare } = getDateParamsFromQuery( query );
|
2018-10-16 16:07:43 +00:00
|
|
|
|
2019-01-17 03:28:41 +00:00
|
|
|
const renderSummaryNumbers = ( { onToggle } ) =>
|
|
|
|
charts.map( chart => {
|
2019-02-15 10:54:41 +00:00
|
|
|
const { key, order, orderby, label, type } = chart;
|
|
|
|
const newPath = { chart: key };
|
|
|
|
if ( orderby ) {
|
|
|
|
newPath.orderby = orderby;
|
|
|
|
}
|
|
|
|
if ( order ) {
|
|
|
|
newPath.order = order;
|
|
|
|
}
|
|
|
|
const href = getNewPath( newPath );
|
2019-01-17 03:28:41 +00:00
|
|
|
const isSelected = selectedChart.key === key;
|
2019-02-07 09:57:47 +00:00
|
|
|
const { delta, prevValue, value } = this.getValues( key, type );
|
2018-10-16 16:07:43 +00:00
|
|
|
|
2019-01-17 03:28:41 +00:00
|
|
|
return (
|
|
|
|
<SummaryNumber
|
|
|
|
key={ key }
|
|
|
|
delta={ delta }
|
|
|
|
href={ href }
|
|
|
|
label={ label }
|
|
|
|
prevLabel={
|
|
|
|
'previous_period' === compare
|
2019-03-13 17:14:02 +00:00
|
|
|
? __( 'Previous Period:', 'woocommerce-admin' )
|
|
|
|
: __( 'Previous Year:', 'woocommerce-admin' )
|
2019-01-17 03:28:41 +00:00
|
|
|
}
|
|
|
|
prevValue={ prevValue }
|
|
|
|
selected={ isSelected }
|
|
|
|
value={ value }
|
|
|
|
onLinkClickCallback={ onToggle }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} );
|
2018-10-16 16:07:43 +00:00
|
|
|
|
2019-01-17 03:28:41 +00:00
|
|
|
return <SummaryList>{ renderSummaryNumbers }</SummaryList>;
|
2018-10-16 16:07:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReportSummary.propTypes = {
|
2018-12-22 11:46:10 +00:00
|
|
|
/**
|
|
|
|
* Properties of all the charts available for that report.
|
|
|
|
*/
|
2018-10-16 16:07:43 +00:00
|
|
|
charts: PropTypes.array.isRequired,
|
2018-12-22 11:46:10 +00:00
|
|
|
/**
|
|
|
|
* The endpoint to use in API calls to populate the Summary Numbers.
|
|
|
|
* 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/stats`). If the provided endpoint
|
2018-12-22 11:46:10 +00:00
|
|
|
* doesn't exist, an error will be shown to the user with `ReportError`.
|
|
|
|
*/
|
2018-10-16 16:07:43 +00:00
|
|
|
endpoint: PropTypes.string.isRequired,
|
2019-02-27 16:28:18 +00:00
|
|
|
/**
|
2019-03-07 02:57:22 +00:00
|
|
|
* Allows specifying properties different from the `endpoint` that will be used
|
2019-02-27 16:28:18 +00:00
|
|
|
* to limit the items when there is an active search.
|
|
|
|
*/
|
2019-03-07 02:57:22 +00:00
|
|
|
limitProperties: PropTypes.array,
|
2018-12-22 11:46:10 +00:00
|
|
|
/**
|
|
|
|
* The query string represented in object form.
|
|
|
|
*/
|
2018-10-16 16:07:43 +00:00
|
|
|
query: PropTypes.object.isRequired,
|
2019-02-27 14:43:34 +00:00
|
|
|
/**
|
|
|
|
* Whether there is an API call running.
|
|
|
|
*/
|
|
|
|
isRequesting: PropTypes.bool,
|
2018-12-22 11:46:10 +00:00
|
|
|
/**
|
|
|
|
* Properties of the selected chart.
|
|
|
|
*/
|
|
|
|
selectedChart: PropTypes.shape( {
|
|
|
|
/**
|
|
|
|
* Key of the selected chart.
|
|
|
|
*/
|
|
|
|
key: PropTypes.string.isRequired,
|
|
|
|
} ).isRequired,
|
2019-02-07 09:57:47 +00:00
|
|
|
/**
|
|
|
|
* Data to display in the SummaryNumbers.
|
|
|
|
*/
|
|
|
|
summaryData: PropTypes.object,
|
|
|
|
};
|
|
|
|
|
|
|
|
ReportSummary.defaultProps = {
|
|
|
|
summaryData: {
|
|
|
|
totals: {
|
|
|
|
primary: {},
|
|
|
|
secondary: {},
|
|
|
|
},
|
|
|
|
isError: false,
|
|
|
|
isRequesting: false,
|
|
|
|
},
|
2018-10-16 16:07:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( ( select, props ) => {
|
2019-03-07 02:57:22 +00:00
|
|
|
const { endpoint, isRequesting, limitProperties, query } = props;
|
|
|
|
const limitBy = limitProperties || [ endpoint ];
|
2019-02-27 14:43:34 +00:00
|
|
|
|
|
|
|
if ( isRequesting ) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:57:22 +00:00
|
|
|
const hasLimitByParam = limitBy.some( item => query[ item ] && query[ item ].length );
|
|
|
|
|
|
|
|
if ( query.search && ! hasLimitByParam ) {
|
2019-02-07 09:57:47 +00:00
|
|
|
return {
|
|
|
|
emptySearchResults: true,
|
|
|
|
};
|
|
|
|
}
|
2019-02-27 14:43:34 +00:00
|
|
|
|
2019-02-27 16:28:18 +00:00
|
|
|
const summaryData = getSummaryNumbers( endpoint, query, select, limitBy );
|
2018-10-16 16:07:43 +00:00
|
|
|
|
|
|
|
return {
|
2018-12-07 21:13:02 +00:00
|
|
|
summaryData,
|
2018-10-16 16:07:43 +00:00
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( ReportSummary );
|