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() {
|
2018-12-07 21:13:02 +00:00
|
|
|
const { charts, query, selectedChart, summaryData } = this.props;
|
2019-02-07 09:57:47 +00:00
|
|
|
const { isError, isRequesting } = 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 />;
|
|
|
|
}
|
|
|
|
|
2018-12-07 21:13:02 +00:00
|
|
|
if ( isRequesting ) {
|
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 => {
|
|
|
|
const { key, label, type } = chart;
|
|
|
|
const href = getNewPath( { chart: key } );
|
|
|
|
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
|
|
|
|
? __( 'Previous Period:', 'wc-admin' )
|
|
|
|
: __( 'Previous Year:', 'wc-admin' )
|
|
|
|
}
|
|
|
|
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,
|
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,
|
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 ) => {
|
|
|
|
const { query, endpoint } = props;
|
2019-02-07 09:57:47 +00:00
|
|
|
if ( query.search && ! ( query[ endpoint ] && query[ endpoint ].length ) ) {
|
|
|
|
return {
|
|
|
|
emptySearchResults: true,
|
|
|
|
};
|
|
|
|
}
|
2018-12-07 21:13:02 +00:00
|
|
|
const summaryData = getSummaryNumbers( endpoint, query, select );
|
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 );
|