2018-10-16 16:07:43 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import { format as formatDate } from '@wordpress/date';
|
|
|
|
import { withSelect } from '@wordpress/data';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { Chart, ChartPlaceholder } from '@woocommerce/components';
|
|
|
|
import {
|
|
|
|
getAllowedIntervalsForQuery,
|
|
|
|
getCurrentDates,
|
|
|
|
getDateFormatsForInterval,
|
|
|
|
getIntervalForQuery,
|
|
|
|
getPreviousDate,
|
|
|
|
} from 'lib/date';
|
|
|
|
import { getReportChartData } from 'store/reports/utils';
|
2018-10-17 18:56:50 +00:00
|
|
|
import ReportError from 'analytics/components/report-error';
|
2018-10-16 16:07:43 +00:00
|
|
|
|
|
|
|
class ReportChart extends Component {
|
|
|
|
render() {
|
|
|
|
const { primaryData, secondaryData, selectedChart, query } = this.props;
|
|
|
|
|
2018-10-17 18:56:50 +00:00
|
|
|
if ( primaryData.isError || secondaryData.isError ) {
|
|
|
|
return <ReportError isError />;
|
|
|
|
}
|
|
|
|
|
2018-10-16 16:07:43 +00:00
|
|
|
if ( primaryData.isRequesting || secondaryData.isRequesting ) {
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<span className="screen-reader-text">
|
|
|
|
{ __( 'Your requested data is loading', 'wc-admin' ) }
|
|
|
|
</span>
|
|
|
|
<ChartPlaceholder />
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentInterval = getIntervalForQuery( query );
|
|
|
|
const allowedIntervals = getAllowedIntervalsForQuery( query );
|
2018-10-17 13:44:43 +00:00
|
|
|
const formats = getDateFormatsForInterval( currentInterval, primaryData.data.intervals.length );
|
2018-10-16 16:07:43 +00:00
|
|
|
const { primary, secondary } = getCurrentDates( query );
|
|
|
|
const primaryKey = `${ primary.label } (${ primary.range })`;
|
|
|
|
const secondaryKey = `${ secondary.label } (${ secondary.range })`;
|
|
|
|
|
|
|
|
const chartData = primaryData.data.intervals.map( function( interval, index ) {
|
|
|
|
const secondaryDate = getPreviousDate(
|
|
|
|
formatDate( 'Y-m-d', interval.date_start ),
|
|
|
|
primary.after,
|
|
|
|
secondary.after,
|
|
|
|
query.compare,
|
|
|
|
currentInterval
|
|
|
|
);
|
|
|
|
|
|
|
|
const secondaryInterval = secondaryData.data.intervals[ index ];
|
|
|
|
return {
|
|
|
|
date: formatDate( 'Y-m-d\\TH:i:s', interval.date_start ),
|
|
|
|
[ primaryKey ]: {
|
|
|
|
labelDate: interval.date_start,
|
|
|
|
value: interval.subtotals[ selectedChart.key ] || 0,
|
|
|
|
},
|
|
|
|
[ secondaryKey ]: {
|
|
|
|
labelDate: secondaryDate,
|
|
|
|
value: ( secondaryInterval && secondaryInterval.subtotals[ selectedChart.key ] ) || 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
return (
|
|
|
|
<Chart
|
|
|
|
data={ chartData }
|
|
|
|
title={ selectedChart.label }
|
|
|
|
interval={ currentInterval }
|
|
|
|
allowedIntervals={ allowedIntervals }
|
|
|
|
mode="time-comparison"
|
|
|
|
pointLabelFormat={ formats.pointLabelFormat }
|
|
|
|
tooltipTitle={ selectedChart.label }
|
|
|
|
xFormat={ formats.xFormat }
|
|
|
|
x2Format={ formats.x2Format }
|
|
|
|
dateParser={ '%Y-%m-%dT%H:%M:%S' }
|
2018-10-19 13:23:47 +00:00
|
|
|
valueType={ selectedChart.type }
|
2018-10-16 16:07:43 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReportChart.propTypes = {
|
|
|
|
primaryData: PropTypes.object.isRequired,
|
|
|
|
secondaryData: PropTypes.object.isRequired,
|
|
|
|
selectedChart: PropTypes.object.isRequired,
|
|
|
|
query: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( ( select, props ) => {
|
|
|
|
const { query, endpoint } = props;
|
2018-10-17 23:02:31 +00:00
|
|
|
const primaryData = getReportChartData( endpoint, 'primary', query, select );
|
2018-10-18 21:15:03 +00:00
|
|
|
const secondaryData = getReportChartData( endpoint, 'secondary', query, select );
|
2018-10-16 16:07:43 +00:00
|
|
|
return {
|
|
|
|
primaryData,
|
|
|
|
secondaryData,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( ReportChart );
|