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 { map } from 'lodash';
|
|
|
|
import { withSelect } from '@wordpress/data';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
2018-10-30 18:57:48 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { formatCurrency } from '@woocommerce/currency';
|
|
|
|
import { getDateParamsFromQuery } from '@woocommerce/date';
|
|
|
|
import { SummaryList, SummaryListPlaceholder, SummaryNumber } from '@woocommerce/components';
|
|
|
|
|
2018-10-16 16:07:43 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { getNewPath } from 'lib/nav-utils';
|
|
|
|
import { getSummaryNumbers } from 'store/reports/utils';
|
2018-10-18 20:45:36 +00:00
|
|
|
import { numberFormat } from 'lib/number';
|
2018-10-30 18:57:48 +00:00
|
|
|
import ReportError from 'analytics/components/report-error';
|
2018-10-18 20:45:36 +00:00
|
|
|
|
2018-10-16 16:07:43 +00:00
|
|
|
class ReportSummary extends Component {
|
|
|
|
render() {
|
|
|
|
const { selectedChart, charts } = this.props;
|
2018-10-17 18:56:50 +00:00
|
|
|
|
|
|
|
if ( this.props.summaryNumbers.isError ) {
|
|
|
|
return <ReportError isError />;
|
|
|
|
}
|
|
|
|
|
2018-10-16 16:07:43 +00:00
|
|
|
if ( this.props.summaryNumbers.isRequesting ) {
|
|
|
|
return <SummaryListPlaceholder numberOfItems={ charts.length } />;
|
|
|
|
}
|
|
|
|
|
|
|
|
const totals = this.props.summaryNumbers.totals.primary || {};
|
|
|
|
const secondaryTotals = this.props.summaryNumbers.totals.secondary || {};
|
|
|
|
const { compare } = getDateParamsFromQuery( this.props.query );
|
|
|
|
|
|
|
|
const summaryNumbers = map( charts, chart => {
|
|
|
|
const { key, label, type } = chart;
|
|
|
|
const isSelected = selectedChart.key === key;
|
|
|
|
let value = parseFloat( totals[ key ] );
|
|
|
|
let secondaryValue =
|
|
|
|
( secondaryTotals[ key ] && parseFloat( secondaryTotals[ key ] ) ) || undefined;
|
|
|
|
|
|
|
|
let delta = 0;
|
|
|
|
if ( secondaryValue && secondaryValue !== 0 ) {
|
|
|
|
delta = Math.round( ( value - secondaryValue ) / secondaryValue * 100 );
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( type ) {
|
|
|
|
case 'average':
|
|
|
|
value = Math.round( value );
|
|
|
|
secondaryValue = secondaryValue && Math.round( secondaryValue );
|
|
|
|
break;
|
|
|
|
case 'currency':
|
|
|
|
value = formatCurrency( value );
|
|
|
|
secondaryValue = secondaryValue && formatCurrency( secondaryValue );
|
|
|
|
break;
|
|
|
|
case 'number':
|
2018-10-18 20:45:36 +00:00
|
|
|
value = numberFormat( value );
|
|
|
|
secondaryValue = secondaryValue && numberFormat( secondaryValue );
|
2018-10-16 16:07:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const href = getNewPath( { chart: key } );
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SummaryNumber
|
|
|
|
key={ key }
|
|
|
|
value={ value }
|
|
|
|
label={ label }
|
|
|
|
selected={ isSelected }
|
|
|
|
prevValue={ secondaryValue }
|
|
|
|
prevLabel={
|
|
|
|
'previous_period' === compare
|
|
|
|
? __( 'Previous Period:', 'wc-admin' )
|
|
|
|
: __( 'Previous Year:', 'wc-admin' )
|
|
|
|
}
|
|
|
|
delta={ delta }
|
|
|
|
href={ href }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
return <SummaryList>{ summaryNumbers }</SummaryList>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReportSummary.propTypes = {
|
|
|
|
charts: PropTypes.array.isRequired,
|
|
|
|
endpoint: PropTypes.string.isRequired,
|
|
|
|
query: PropTypes.object.isRequired,
|
|
|
|
selectedChart: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( ( select, props ) => {
|
|
|
|
const { query, endpoint } = props;
|
2018-10-17 23:02:31 +00:00
|
|
|
const summaryNumbers = getSummaryNumbers( endpoint, query, select );
|
2018-10-16 16:07:43 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
summaryNumbers,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( ReportSummary );
|