/** @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 { isEqual, xor } from 'lodash'; import PropTypes from 'prop-types'; import { ToggleControl, IconButton, NavigableMenu } from '@wordpress/components'; import { withDispatch } from '@wordpress/data'; /** * WooCommerce dependencies */ import { EllipsisMenu, MenuItem, SectionHeader } from '@woocommerce/components'; /** * 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 || [], query: props.query, }; this.toggle = this.toggle.bind( this ); } componentDidUpdate( { userPrefCharts: prevUserPrefCharts, userPrefChartType: prevUserPrefChartType, } ) { const { userPrefCharts, userPrefChartType } = this.props; 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 */ } } toggle( key ) { return () => { const hiddenChartKeys = xor( this.state.hiddenChartKeys, [ key ] ); this.setState( { hiddenChartKeys } ); const userDataFields = { [ 'dashboard_charts' ]: hiddenChartKeys, }; this.props.updateCurrentUserData( userDataFields ); }; } handleTypeToggle( type ) { return () => { this.setState( { chartType: type } ); const userDataFields = { [ 'dashboard_chart_type' ]: type, }; this.props.updateCurrentUserData( userDataFields ); }; } renderMenu() { return ( { uniqCharts.map( chart => { return ( ); } ) } ); } render() { const { path } = this.props; const { chartType, hiddenChartKeys } = this.state; const query = { ...this.props.query, type: chartType }; return (
} title={ __( 'Line chart', 'wc-admin' ) } aria-checked={ query.type === 'line' } role="menuitemradio" tabIndex={ query.type === 'line' ? 0 : -1 } onClick={ this.handleTypeToggle( 'line' ) } /> } title={ __( 'Bar chart', 'wc-admin' ) } aria-checked={ query.type === 'bar' } role="menuitemradio" tabIndex={ query.type === '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, }; } ), withDispatch( dispatch => { const { updateCurrentUserData } = dispatch( 'wc-api' ); return { updateCurrentUserData, }; } ) )( DashboardCharts );