2018-12-22 00:24:26 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2019-01-08 01:49:11 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import Gridicon from 'gridicons';
|
2018-12-22 00:24:26 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2020-02-14 02:23:21 +00:00
|
|
|
import {
|
|
|
|
IconButton,
|
|
|
|
NavigableMenu,
|
|
|
|
SelectControl,
|
|
|
|
} from '@wordpress/components';
|
2019-01-08 01:49:11 +00:00
|
|
|
import { withDispatch } from '@wordpress/data';
|
2018-12-22 00:24:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
2020-02-14 02:23:21 +00:00
|
|
|
import {
|
|
|
|
EllipsisMenu,
|
|
|
|
MenuItem,
|
|
|
|
MenuTitle,
|
|
|
|
SectionHeader,
|
|
|
|
} from '@woocommerce/components';
|
2019-11-26 19:39:40 +00:00
|
|
|
import { getAllowedIntervalsForQuery } from 'lib/date';
|
2018-12-22 00:24:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import ChartBlock from './block';
|
2019-06-15 12:12:19 +00:00
|
|
|
import { uniqCharts } from './config';
|
2019-01-08 01:49:11 +00:00
|
|
|
import withSelect from 'wc-api/with-select';
|
2019-07-01 10:16:12 +00:00
|
|
|
import { recordEvent } from 'lib/tracks';
|
2018-12-22 00:24:26 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
class DashboardCharts extends Component {
|
2019-01-08 01:49:11 +00:00
|
|
|
constructor( props ) {
|
2018-12-22 00:24:26 +00:00
|
|
|
super( ...arguments );
|
2019-05-02 10:22:34 +00:00
|
|
|
|
2018-12-22 00:24:26 +00:00
|
|
|
this.state = {
|
2019-01-08 01:49:11 +00:00
|
|
|
chartType: props.userPrefChartType || 'line',
|
2019-05-03 17:53:07 +00:00
|
|
|
interval: props.userPrefChartInterval || 'day',
|
2018-12-22 00:24:26 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-14 17:35:08 +00:00
|
|
|
handleTypeToggle( chartType ) {
|
2018-12-22 00:24:26 +00:00
|
|
|
return () => {
|
2019-02-15 14:37:56 +00:00
|
|
|
this.setState( { chartType } );
|
2019-01-08 01:49:11 +00:00
|
|
|
const userDataFields = {
|
2020-02-14 02:23:21 +00:00
|
|
|
dashboard_chart_type: chartType,
|
2019-01-08 01:49:11 +00:00
|
|
|
};
|
|
|
|
this.props.updateCurrentUserData( userDataFields );
|
2019-07-11 14:55:52 +00:00
|
|
|
recordEvent( 'dash_charts_type_toggle', { chart_type: chartType } );
|
2018-12-22 00:24:26 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
renderMenu() {
|
2019-04-30 00:35:37 +00:00
|
|
|
const {
|
2019-05-07 07:21:34 +00:00
|
|
|
hiddenBlocks,
|
|
|
|
isFirst,
|
|
|
|
isLast,
|
|
|
|
onMove,
|
|
|
|
onRemove,
|
2019-04-30 00:35:37 +00:00
|
|
|
onTitleBlur,
|
|
|
|
onTitleChange,
|
2019-05-07 07:21:34 +00:00
|
|
|
onToggleHiddenBlock,
|
2019-04-30 00:35:37 +00:00
|
|
|
titleInput,
|
2019-05-09 01:13:14 +00:00
|
|
|
controls: Controls,
|
2019-04-30 00:35:37 +00:00
|
|
|
} = this.props;
|
2019-02-25 19:25:19 +00:00
|
|
|
|
2018-12-22 00:24:26 +00:00
|
|
|
return (
|
2019-05-02 10:22:34 +00:00
|
|
|
<EllipsisMenu
|
2020-02-14 02:23:21 +00:00
|
|
|
label={ __(
|
|
|
|
'Choose which charts to display',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-05-03 01:37:16 +00:00
|
|
|
renderContent={ ( { onToggle } ) => (
|
2019-04-30 00:35:37 +00:00
|
|
|
<Fragment>
|
2020-02-14 02:23:21 +00:00
|
|
|
<MenuTitle>
|
|
|
|
{ __( 'Charts', 'woocommerce-admin' ) }
|
|
|
|
</MenuTitle>
|
|
|
|
{ uniqCharts.map( ( chart ) => {
|
2019-07-01 10:16:12 +00:00
|
|
|
const key = chart.endpoint + '_' + chart.key;
|
|
|
|
const checked = ! hiddenBlocks.includes( key );
|
2019-04-30 00:35:37 +00:00
|
|
|
return (
|
|
|
|
<MenuItem
|
2019-07-01 10:16:12 +00:00
|
|
|
checked={ checked }
|
2019-04-30 00:35:37 +00:00
|
|
|
isCheckbox
|
|
|
|
isClickable
|
2019-06-15 12:12:19 +00:00
|
|
|
key={ chart.endpoint + '_' + chart.key }
|
2019-07-01 10:16:12 +00:00
|
|
|
onInvoke={ () => {
|
|
|
|
onToggleHiddenBlock( key )();
|
2020-02-14 02:23:21 +00:00
|
|
|
recordEvent(
|
|
|
|
'dash_charts_chart_toggle',
|
|
|
|
{
|
|
|
|
status: checked ? 'off' : 'on',
|
|
|
|
key,
|
|
|
|
}
|
|
|
|
);
|
2019-07-01 10:16:12 +00:00
|
|
|
} }
|
2019-04-30 00:35:37 +00:00
|
|
|
>
|
2019-09-04 15:39:22 +00:00
|
|
|
{ chart.label }
|
2019-04-30 00:35:37 +00:00
|
|
|
</MenuItem>
|
|
|
|
);
|
|
|
|
} ) }
|
2020-02-14 02:23:21 +00:00
|
|
|
{ window.wcAdminFeatures[
|
|
|
|
'analytics-dashboard/customizable'
|
|
|
|
] && (
|
2019-05-22 20:43:45 +00:00
|
|
|
<Controls
|
|
|
|
onToggle={ onToggle }
|
|
|
|
onMove={ onMove }
|
|
|
|
onRemove={ onRemove }
|
|
|
|
isFirst={ isFirst }
|
|
|
|
isLast={ isLast }
|
|
|
|
onTitleBlur={ onTitleBlur }
|
|
|
|
onTitleChange={ onTitleChange }
|
|
|
|
titleInput={ titleInput }
|
|
|
|
/>
|
2019-05-09 01:13:14 +00:00
|
|
|
) }
|
2019-04-30 00:35:37 +00:00
|
|
|
</Fragment>
|
2019-05-02 10:22:34 +00:00
|
|
|
) }
|
2019-04-30 00:35:37 +00:00
|
|
|
/>
|
2018-12-22 00:24:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-09 00:12:39 +00:00
|
|
|
renderIntervalSelector() {
|
2020-02-14 02:23:21 +00:00
|
|
|
const allowedIntervals = getAllowedIntervalsForQuery(
|
|
|
|
this.props.query
|
|
|
|
);
|
2019-01-09 00:12:39 +00:00
|
|
|
if ( ! allowedIntervals || allowedIntervals.length < 1 ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const intervalLabels = {
|
2019-03-13 17:14:02 +00:00
|
|
|
hour: __( 'By hour', 'woocommerce-admin' ),
|
|
|
|
day: __( 'By day', 'woocommerce-admin' ),
|
|
|
|
week: __( 'By week', 'woocommerce-admin' ),
|
|
|
|
month: __( 'By month', 'woocommerce-admin' ),
|
|
|
|
quarter: __( 'By quarter', 'woocommerce-admin' ),
|
|
|
|
year: __( 'By year', 'woocommerce-admin' ),
|
2019-01-09 00:12:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SelectControl
|
|
|
|
className="woocommerce-chart__interval-select"
|
|
|
|
value={ this.state.interval }
|
2020-02-14 02:23:21 +00:00
|
|
|
options={ allowedIntervals.map( ( allowedInterval ) => ( {
|
2019-01-09 00:12:39 +00:00
|
|
|
value: allowedInterval,
|
|
|
|
label: intervalLabels[ allowedInterval ],
|
|
|
|
} ) ) }
|
|
|
|
onChange={ this.setInterval }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
setInterval = ( interval ) => {
|
2019-01-09 00:12:39 +00:00
|
|
|
this.setState( { interval }, () => {
|
|
|
|
const userDataFields = {
|
2020-02-14 02:23:21 +00:00
|
|
|
dashboard_chart_interval: this.state.interval,
|
2019-01-09 00:12:39 +00:00
|
|
|
};
|
|
|
|
this.props.updateCurrentUserData( userDataFields );
|
2019-07-11 14:49:53 +00:00
|
|
|
recordEvent( 'dash_charts_interval', { interval } );
|
2019-01-09 00:12:39 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2018-12-22 00:24:26 +00:00
|
|
|
render() {
|
2019-05-07 07:21:34 +00:00
|
|
|
const { hiddenBlocks, path, title } = this.props;
|
|
|
|
const { chartType, interval } = this.state;
|
2019-02-15 14:37:56 +00:00
|
|
|
const query = { ...this.props.query, chartType, interval };
|
2018-12-22 00:24:26 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<div className="woocommerce-dashboard__dashboard-charts">
|
2019-01-09 00:12:39 +00:00
|
|
|
<SectionHeader
|
2019-05-02 10:22:34 +00:00
|
|
|
title={ title || __( 'Charts', 'woocommerce-admin' ) }
|
2019-01-09 00:12:39 +00:00
|
|
|
menu={ this.renderMenu() }
|
|
|
|
className={ 'has-interval-select' }
|
|
|
|
>
|
|
|
|
{ this.renderIntervalSelector() }
|
2018-12-22 00:24:26 +00:00
|
|
|
<NavigableMenu
|
|
|
|
className="woocommerce-chart__types"
|
|
|
|
orientation="horizontal"
|
|
|
|
role="menubar"
|
|
|
|
>
|
|
|
|
<IconButton
|
2020-02-14 02:23:21 +00:00
|
|
|
className={ classNames(
|
|
|
|
'woocommerce-chart__type-button',
|
|
|
|
{
|
|
|
|
'woocommerce-chart__type-button-selected':
|
|
|
|
! query.chartType ||
|
|
|
|
query.chartType === 'line',
|
|
|
|
}
|
|
|
|
) }
|
2018-12-22 00:24:26 +00:00
|
|
|
icon={ <Gridicon icon="line-graph" /> }
|
2020-02-14 02:23:21 +00:00
|
|
|
title={ __(
|
|
|
|
'Line chart',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-02-14 17:35:08 +00:00
|
|
|
aria-checked={ query.chartType === 'line' }
|
2018-12-22 00:24:26 +00:00
|
|
|
role="menuitemradio"
|
2019-02-14 17:35:08 +00:00
|
|
|
tabIndex={ query.chartType === 'line' ? 0 : -1 }
|
2018-12-22 00:24:26 +00:00
|
|
|
onClick={ this.handleTypeToggle( 'line' ) }
|
|
|
|
/>
|
|
|
|
<IconButton
|
2020-02-14 02:23:21 +00:00
|
|
|
className={ classNames(
|
|
|
|
'woocommerce-chart__type-button',
|
|
|
|
{
|
|
|
|
'woocommerce-chart__type-button-selected':
|
|
|
|
query.chartType === 'bar',
|
|
|
|
}
|
|
|
|
) }
|
2018-12-22 00:24:26 +00:00
|
|
|
icon={ <Gridicon icon="stats-alt" /> }
|
2019-03-13 17:14:02 +00:00
|
|
|
title={ __( 'Bar chart', 'woocommerce-admin' ) }
|
2019-02-14 17:35:08 +00:00
|
|
|
aria-checked={ query.chartType === 'bar' }
|
2018-12-22 00:24:26 +00:00
|
|
|
role="menuitemradio"
|
2019-02-14 17:35:08 +00:00
|
|
|
tabIndex={ query.chartType === 'bar' ? 0 : -1 }
|
2018-12-22 00:24:26 +00:00
|
|
|
onClick={ this.handleTypeToggle( 'bar' ) }
|
|
|
|
/>
|
|
|
|
</NavigableMenu>
|
|
|
|
</SectionHeader>
|
|
|
|
<div className="woocommerce-dashboard__columns">
|
2020-02-14 02:23:21 +00:00
|
|
|
{ uniqCharts.map( ( chart ) => {
|
|
|
|
return hiddenBlocks.includes(
|
|
|
|
chart.endpoint + '_' + chart.key
|
|
|
|
) ? null : (
|
2019-01-10 09:55:15 +00:00
|
|
|
<ChartBlock
|
2019-06-15 12:12:19 +00:00
|
|
|
charts={ [ chart ] }
|
2019-01-10 09:55:15 +00:00
|
|
|
endpoint={ chart.endpoint }
|
2019-06-15 12:12:19 +00:00
|
|
|
key={ chart.endpoint + '_' + chart.key }
|
2019-01-10 09:55:15 +00:00
|
|
|
path={ path }
|
|
|
|
query={ query }
|
|
|
|
/>
|
2018-12-22 00:24:26 +00:00
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DashboardCharts.propTypes = {
|
|
|
|
path: PropTypes.string.isRequired,
|
|
|
|
query: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
2019-01-08 01:49:11 +00:00
|
|
|
export default compose(
|
2020-02-14 02:23:21 +00:00
|
|
|
withSelect( ( select ) => {
|
2019-01-08 01:49:11 +00:00
|
|
|
const { getCurrentUserData } = select( 'wc-api' );
|
|
|
|
const userData = getCurrentUserData();
|
|
|
|
|
|
|
|
return {
|
|
|
|
userPrefChartType: userData.dashboard_chart_type,
|
2019-01-09 00:12:39 +00:00
|
|
|
userPrefChartInterval: userData.dashboard_chart_interval,
|
2019-01-08 01:49:11 +00:00
|
|
|
};
|
|
|
|
} ),
|
2020-02-14 02:23:21 +00:00
|
|
|
withDispatch( ( dispatch ) => {
|
2019-01-08 01:49:11 +00:00
|
|
|
const { updateCurrentUserData } = dispatch( 'wc-api' );
|
|
|
|
|
|
|
|
return {
|
|
|
|
updateCurrentUserData,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( DashboardCharts );
|