2018-10-16 16:07:43 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-11-21 01:19:33 +00:00
|
|
|
import { Component } from '@wordpress/element';
|
2018-10-16 16:07:43 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import { format as formatDate } from '@wordpress/date';
|
|
|
|
import { withSelect } from '@wordpress/data';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-11-20 22:21:47 +00:00
|
|
|
import { find, get } from 'lodash';
|
2018-10-16 16:07:43 +00:00
|
|
|
|
|
|
|
/**
|
2018-10-30 18:57:48 +00:00
|
|
|
* WooCommerce dependencies
|
2018-10-16 16:07:43 +00:00
|
|
|
*/
|
2018-11-20 22:21:47 +00:00
|
|
|
import { flattenFilters } from '@woocommerce/navigation';
|
2018-10-16 16:07:43 +00:00
|
|
|
import {
|
|
|
|
getAllowedIntervalsForQuery,
|
|
|
|
getCurrentDates,
|
|
|
|
getDateFormatsForInterval,
|
|
|
|
getIntervalForQuery,
|
2018-11-14 01:45:05 +00:00
|
|
|
getChartTypeForQuery,
|
2018-10-16 16:07:43 +00:00
|
|
|
getPreviousDate,
|
2018-10-30 18:57:48 +00:00
|
|
|
} from '@woocommerce/date';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2018-11-21 01:19:33 +00:00
|
|
|
import { Chart } from 'components';
|
2018-11-12 14:34:51 +00:00
|
|
|
import { getReportChartData, getTooltipValueFormat } 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
|
|
|
|
2018-11-20 22:21:47 +00:00
|
|
|
export const DEFAULT_FILTER = 'all';
|
|
|
|
|
|
|
|
export class ReportChart extends Component {
|
|
|
|
getSelectedFilter( filters, query ) {
|
|
|
|
if ( filters.length === 0 ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const filterConfig = filters.pop();
|
|
|
|
|
|
|
|
if ( filterConfig.showFilters( query ) ) {
|
|
|
|
const allFilters = flattenFilters( filterConfig.filters );
|
|
|
|
const value = query[ filterConfig.param ] || DEFAULT_FILTER;
|
|
|
|
const selectedFilter = find( allFilters, { value } );
|
|
|
|
const selectedFilterParam = get( selectedFilter, [ 'settings', 'param' ] );
|
|
|
|
|
|
|
|
if ( ! selectedFilterParam || Object.keys( query ).includes( selectedFilterParam ) ) {
|
|
|
|
return selectedFilter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.getSelectedFilter( filters, query );
|
|
|
|
}
|
|
|
|
|
|
|
|
getChartMode() {
|
|
|
|
const { filters, query } = this.props;
|
|
|
|
if ( ! filters ) {
|
2018-11-22 13:03:35 +00:00
|
|
|
return;
|
2018-11-20 22:21:47 +00:00
|
|
|
}
|
|
|
|
const clonedFilters = filters.slice( 0 );
|
|
|
|
const selectedFilter = this.getSelectedFilter( clonedFilters, query );
|
|
|
|
|
|
|
|
return get( selectedFilter, [ 'chartMode' ] );
|
|
|
|
}
|
|
|
|
|
2018-10-16 16:07:43 +00:00
|
|
|
render() {
|
2018-11-20 22:21:47 +00:00
|
|
|
const { query, itemsLabel, path, primaryData, secondaryData, selectedChart } = this.props;
|
2018-10-16 16:07:43 +00:00
|
|
|
|
2018-10-17 18:56:50 +00:00
|
|
|
if ( primaryData.isError || secondaryData.isError ) {
|
|
|
|
return <ReportError isError />;
|
|
|
|
}
|
|
|
|
|
2018-10-16 16:07:43 +00:00
|
|
|
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 ]: {
|
2018-11-12 14:34:51 +00:00
|
|
|
labelDate: secondaryDate.format( 'YYYY-MM-DD HH:mm:ss' ),
|
2018-10-16 16:07:43 +00:00
|
|
|
value: ( secondaryInterval && secondaryInterval.subtotals[ selectedChart.key ] ) || 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} );
|
2018-11-01 09:13:45 +00:00
|
|
|
|
2018-10-16 16:07:43 +00:00
|
|
|
return (
|
|
|
|
<Chart
|
2018-11-01 09:13:45 +00:00
|
|
|
path={ path }
|
|
|
|
query={ query }
|
2018-10-16 16:07:43 +00:00
|
|
|
data={ chartData }
|
|
|
|
title={ selectedChart.label }
|
|
|
|
interval={ currentInterval }
|
2018-11-14 01:45:05 +00:00
|
|
|
type={ getChartTypeForQuery( query ) }
|
2018-10-16 16:07:43 +00:00
|
|
|
allowedIntervals={ allowedIntervals }
|
2018-11-12 21:41:33 +00:00
|
|
|
itemsLabel={ itemsLabel }
|
2018-11-12 14:34:51 +00:00
|
|
|
mode={ this.getChartMode() }
|
|
|
|
tooltipLabelFormat={ formats.tooltipLabelFormat }
|
|
|
|
tooltipValueFormat={ getTooltipValueFormat( selectedChart.type ) }
|
2018-10-16 16:07:43 +00:00
|
|
|
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-11-21 01:19:33 +00:00
|
|
|
isRequesting={ primaryData.isRequesting || secondaryData.isRequesting }
|
2018-10-16 16:07:43 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReportChart.propTypes = {
|
2018-11-20 22:21:47 +00:00
|
|
|
filters: PropTypes.array,
|
2018-11-12 21:41:33 +00:00
|
|
|
itemsLabel: PropTypes.string,
|
2018-11-01 09:13:45 +00:00
|
|
|
path: PropTypes.string.isRequired,
|
2018-10-16 16:07:43 +00:00
|
|
|
primaryData: PropTypes.object.isRequired,
|
2018-11-01 09:13:45 +00:00
|
|
|
query: PropTypes.object.isRequired,
|
2018-10-16 16:07:43 +00:00
|
|
|
secondaryData: 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 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 );
|