/** @format */ /** * External dependencies */ import { __ } from '@wordpress/i18n'; import classNames from 'classnames'; import { Component, Fragment } from '@wordpress/element'; import { compose } from '@wordpress/compose'; import Gridicon from 'gridicons'; import { xor } from 'lodash'; import PropTypes from 'prop-types'; import { IconButton, NavigableMenu, SelectControl } from '@wordpress/components'; import { withDispatch } from '@wordpress/data'; /** * WooCommerce dependencies */ import { EllipsisMenu, MenuItem, SectionHeader } from '@woocommerce/components'; import { getAllowedIntervalsForQuery } from '@woocommerce/date'; /** * Internal dependencies */ import ChartBlock from './block'; import { getChartFromKey, uniqCharts } from './config'; import withSelect from 'wc-api/with-select'; import './style.scss'; class DashboardCharts extends Component { constructor( props ) { super( ...arguments ); this.state = { chartType: props.userPrefChartType || 'line', hiddenChartKeys: props.userPrefCharts || [ 'avg_order_value', 'avg_items_per_order', 'items_sold', 'gross_revenue', 'refunds', 'coupons', 'taxes', 'shipping', 'amount', 'total_tax', 'order_tax', 'shipping_tax', ], interval: props.userPrefIntervals || 'day', }; this.toggle = this.toggle.bind( this ); } toggle( key ) { return () => { const hiddenChartKeys = xor( this.state.hiddenChartKeys, [ key ] ); this.setState( { hiddenChartKeys } ); const userDataFields = { [ 'dashboard_charts' ]: hiddenChartKeys, }; this.props.updateCurrentUserData( userDataFields ); }; } handleTypeToggle( chartType ) { return () => { this.setState( { chartType } ); const userDataFields = { [ 'dashboard_chart_type' ]: chartType, }; this.props.updateCurrentUserData( userDataFields ); }; } renderMenu() { const { hiddenChartKeys } = this.state; return ( { uniqCharts.map( chart => { return ( { __( `${ chart.label }`, 'woocommerce-admin' ) } ); } ) } ); } renderIntervalSelector() { const allowedIntervals = getAllowedIntervalsForQuery( this.props.query ); if ( ! allowedIntervals || allowedIntervals.length < 1 ) { return null; } const intervalLabels = { 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' ), }; return ( ( { 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 ); } ); }; render() { const { path } = this.props; const { chartType, hiddenChartKeys, interval } = this.state; const query = { ...this.props.query, chartType, interval }; return (
{ this.renderIntervalSelector() } } title={ __( 'Line chart', 'woocommerce-admin' ) } aria-checked={ query.chartType === 'line' } role="menuitemradio" tabIndex={ query.chartType === 'line' ? 0 : -1 } onClick={ this.handleTypeToggle( 'line' ) } /> } title={ __( 'Bar chart', 'woocommerce-admin' ) } aria-checked={ query.chartType === 'bar' } role="menuitemradio" tabIndex={ query.chartType === 'bar' ? 0 : -1 } onClick={ this.handleTypeToggle( 'bar' ) } />
{ uniqCharts.map( chart => { return hiddenChartKeys.includes( chart.key ) ? null : ( ); } ) }
); } } DashboardCharts.propTypes = { path: PropTypes.string.isRequired, query: PropTypes.object.isRequired, }; export default compose( withSelect( select => { const { getCurrentUserData } = select( 'wc-api' ); const userData = getCurrentUserData(); return { userPrefCharts: userData.dashboard_charts, userPrefChartType: userData.dashboard_chart_type, userPrefChartInterval: userData.dashboard_chart_interval, }; } ), withDispatch( dispatch => { const { updateCurrentUserData } = dispatch( 'wc-api' ); return { updateCurrentUserData, }; } ) )( DashboardCharts );