2018-12-22 00:24:26 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* 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';
|
|
|
|
import { isEqual, xor } from 'lodash';
|
2018-12-22 00:24:26 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2019-02-25 19:25:19 +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
|
|
|
|
*/
|
|
|
|
import { EllipsisMenu, MenuItem, SectionHeader } from '@woocommerce/components';
|
2019-01-09 00:12:39 +00:00
|
|
|
import { getAllowedIntervalsForQuery } from '@woocommerce/date';
|
2018-12-22 00:24:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import ChartBlock from './block';
|
2019-01-08 01:49:11 +00:00
|
|
|
import { getChartFromKey, uniqCharts } from './config';
|
|
|
|
import withSelect from 'wc-api/with-select';
|
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 );
|
|
|
|
this.state = {
|
2019-01-08 01:49:11 +00:00
|
|
|
chartType: props.userPrefChartType || 'line',
|
|
|
|
hiddenChartKeys: props.userPrefCharts || [],
|
2019-01-09 00:12:39 +00:00
|
|
|
interval: props.userPrefIntervals || 'day',
|
2018-12-22 00:24:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.toggle = this.toggle.bind( this );
|
|
|
|
}
|
|
|
|
|
2019-01-08 01:49:11 +00:00
|
|
|
componentDidUpdate( {
|
|
|
|
userPrefCharts: prevUserPrefCharts,
|
|
|
|
userPrefChartType: prevUserPrefChartType,
|
2019-01-09 00:12:39 +00:00
|
|
|
userPrefChartInterval: prevUserPrefChartInterval,
|
2019-01-08 01:49:11 +00:00
|
|
|
} ) {
|
2019-01-09 00:12:39 +00:00
|
|
|
const { userPrefCharts, userPrefChartInterval, userPrefChartType } = this.props;
|
2019-01-08 01:49:11 +00:00
|
|
|
if ( userPrefCharts && ! isEqual( userPrefCharts, prevUserPrefCharts ) ) {
|
|
|
|
/* eslint-disable react/no-did-update-set-state */
|
|
|
|
this.setState( {
|
|
|
|
hiddenChartKeys: userPrefCharts,
|
|
|
|
} );
|
|
|
|
/* eslint-enable react/no-did-update-set-state */
|
|
|
|
}
|
|
|
|
if ( userPrefChartType && userPrefChartType !== prevUserPrefChartType ) {
|
|
|
|
/* eslint-disable react/no-did-update-set-state */
|
|
|
|
this.setState( {
|
|
|
|
chartType: userPrefChartType,
|
|
|
|
} );
|
|
|
|
/* eslint-enable react/no-did-update-set-state */
|
|
|
|
}
|
2019-01-09 00:12:39 +00:00
|
|
|
if ( userPrefChartInterval !== prevUserPrefChartInterval ) {
|
|
|
|
/* eslint-disable react/no-did-update-set-state */
|
|
|
|
this.setState( {
|
|
|
|
interval: userPrefChartInterval,
|
|
|
|
} );
|
|
|
|
/* eslint-enable react/no-did-update-set-state */
|
|
|
|
}
|
2019-01-08 01:49:11 +00:00
|
|
|
}
|
|
|
|
|
2018-12-22 00:24:26 +00:00
|
|
|
toggle( key ) {
|
|
|
|
return () => {
|
2019-01-08 01:49:11 +00:00
|
|
|
const hiddenChartKeys = xor( this.state.hiddenChartKeys, [ key ] );
|
|
|
|
this.setState( { hiddenChartKeys } );
|
|
|
|
const userDataFields = {
|
|
|
|
[ 'dashboard_charts' ]: hiddenChartKeys,
|
|
|
|
};
|
|
|
|
this.props.updateCurrentUserData( userDataFields );
|
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 = {
|
2019-02-14 17:35:08 +00:00
|
|
|
[ 'dashboard_chart_type' ]: chartType,
|
2019-01-08 01:49:11 +00:00
|
|
|
};
|
|
|
|
this.props.updateCurrentUserData( userDataFields );
|
2018-12-22 00:24:26 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
renderMenu() {
|
2019-02-25 19:25:19 +00:00
|
|
|
const { hiddenChartKeys } = this.state;
|
|
|
|
|
2018-12-22 00:24:26 +00:00
|
|
|
return (
|
|
|
|
<EllipsisMenu label={ __( 'Choose which charts to display', 'wc-admin' ) }>
|
2019-01-08 01:49:11 +00:00
|
|
|
{ uniqCharts.map( chart => {
|
2018-12-22 00:24:26 +00:00
|
|
|
return (
|
2019-02-25 19:25:19 +00:00
|
|
|
<MenuItem
|
|
|
|
checked={ ! hiddenChartKeys.includes( chart.key ) }
|
|
|
|
isCheckbox
|
|
|
|
isClickable
|
|
|
|
key={ chart.key }
|
|
|
|
onInvoke={ this.toggle( chart.key ) }
|
|
|
|
>
|
|
|
|
{ __( `${ chart.label }`, 'wc-admin' ) }
|
2018-12-22 00:24:26 +00:00
|
|
|
</MenuItem>
|
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
</EllipsisMenu>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-09 00:12:39 +00:00
|
|
|
renderIntervalSelector() {
|
|
|
|
const allowedIntervals = getAllowedIntervalsForQuery( this.props.query );
|
|
|
|
if ( ! allowedIntervals || allowedIntervals.length < 1 ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const intervalLabels = {
|
|
|
|
hour: __( 'By hour', 'wc-admin' ),
|
|
|
|
day: __( 'By day', 'wc-admin' ),
|
|
|
|
week: __( 'By week', 'wc-admin' ),
|
|
|
|
month: __( 'By month', 'wc-admin' ),
|
|
|
|
quarter: __( 'By quarter', 'wc-admin' ),
|
|
|
|
year: __( 'By year', 'wc-admin' ),
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SelectControl
|
|
|
|
className="woocommerce-chart__interval-select"
|
|
|
|
value={ this.state.interval }
|
|
|
|
options={ allowedIntervals.map( allowedInterval => ( {
|
|
|
|
value: allowedInterval,
|
|
|
|
label: intervalLabels[ allowedInterval ],
|
|
|
|
} ) ) }
|
|
|
|
onChange={ this.setInterval }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
setInterval = interval => {
|
|
|
|
this.setState( { interval }, () => {
|
|
|
|
const userDataFields = {
|
|
|
|
[ 'dashboard_chart_interval' ]: this.state.interval,
|
|
|
|
};
|
|
|
|
this.props.updateCurrentUserData( userDataFields );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2018-12-22 00:24:26 +00:00
|
|
|
render() {
|
|
|
|
const { path } = this.props;
|
2019-01-09 00:12:39 +00:00
|
|
|
const { chartType, hiddenChartKeys, 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
|
|
|
|
title={ __( 'Charts', 'wc-admin' ) }
|
|
|
|
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
|
|
|
|
className={ classNames( 'woocommerce-chart__type-button', {
|
2019-02-14 17:35:08 +00:00
|
|
|
'woocommerce-chart__type-button-selected':
|
|
|
|
! query.chartType || query.chartType === 'line',
|
2018-12-22 00:24:26 +00:00
|
|
|
} ) }
|
|
|
|
icon={ <Gridicon icon="line-graph" /> }
|
|
|
|
title={ __( 'Line chart', 'wc-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
|
|
|
|
className={ classNames( 'woocommerce-chart__type-button', {
|
2019-02-14 17:35:08 +00:00
|
|
|
'woocommerce-chart__type-button-selected': query.chartType === 'bar',
|
2018-12-22 00:24:26 +00:00
|
|
|
} ) }
|
|
|
|
icon={ <Gridicon icon="stats-alt" /> }
|
|
|
|
title={ __( 'Bar chart', 'wc-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">
|
2019-01-08 01:49:11 +00:00
|
|
|
{ uniqCharts.map( chart => {
|
|
|
|
return hiddenChartKeys.includes( chart.key ) ? null : (
|
2019-01-10 09:55:15 +00:00
|
|
|
<ChartBlock
|
|
|
|
charts={ getChartFromKey( chart.key ) }
|
|
|
|
endpoint={ chart.endpoint }
|
|
|
|
key={ chart.key }
|
|
|
|
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(
|
|
|
|
withSelect( select => {
|
|
|
|
const { getCurrentUserData } = select( 'wc-api' );
|
|
|
|
const userData = getCurrentUserData();
|
|
|
|
|
|
|
|
return {
|
|
|
|
userPrefCharts: userData.dashboard_charts,
|
|
|
|
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
|
|
|
};
|
|
|
|
} ),
|
|
|
|
withDispatch( dispatch => {
|
|
|
|
const { updateCurrentUserData } = dispatch( 'wc-api' );
|
|
|
|
|
|
|
|
return {
|
|
|
|
updateCurrentUserData,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( DashboardCharts );
|