/** * External dependencies */ import { __ } from '@wordpress/i18n'; import classNames from 'classnames'; import { Fragment, useState } from '@wordpress/element'; import Gridicon from 'gridicons'; import PropTypes from 'prop-types'; import { Button, NavigableMenu, SelectControl } from '@wordpress/components'; import { EllipsisMenu, MenuItem, MenuTitle, SectionHeader, } from '@woocommerce/components'; import { useUserPreferences } from '@woocommerce/data'; /** * Internal dependencies */ import { getAllowedIntervalsForQuery } from '../../lib/date'; import ChartBlock from './block'; import { uniqCharts } from './config'; import { recordEvent } from '../../lib/tracks'; import './style.scss'; const renderChartToggles = ( { hiddenBlocks, onToggleHiddenBlock } ) => { return uniqCharts.map( ( chart ) => { const key = chart.endpoint + '_' + chart.key; const checked = ! hiddenBlocks.includes( key ); return ( { onToggleHiddenBlock( key )(); recordEvent( 'dash_charts_chart_toggle', { status: checked ? 'off' : 'on', key, } ); } } > { chart.label } ); } ); }; const renderIntervalSelector = ( { chartInterval, setInterval, query } ) => { const allowedIntervals = getAllowedIntervalsForQuery( 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={ setInterval } /> ); }; const renderChartBlocks = ( { hiddenBlocks, path, query, filters } ) => { // Reduce the API response to only the necessary stat fields // by supplying all charts common to each endpoint. const chartsByEndpoint = uniqCharts.reduce( ( byEndpoint, chart ) => { if ( typeof byEndpoint[ chart.endpoint ] === 'undefined' ) { byEndpoint[ chart.endpoint ] = []; } byEndpoint[ chart.endpoint ].push( chart ); return byEndpoint; }, {} ); return (
{ uniqCharts.map( ( chart ) => { return hiddenBlocks.includes( chart.endpoint + '_' + chart.key ) ? null : ( ); } ) }
); }; const DashboardCharts = ( props ) => { const { controls: Controls, hiddenBlocks, isFirst, isLast, onMove, onRemove, onTitleBlur, onTitleChange, onToggleHiddenBlock, path, title, titleInput, filters, } = props; const { updateUserPreferences, ...userPrefs } = useUserPreferences(); const [ chartType, setChartType ] = useState( userPrefs.dashboard_chart_type || 'line' ); const [ chartInterval, setChartInterval ] = useState( userPrefs.dashboard_chart_interval || 'day' ); const query = { ...props.query, chartType, interval: chartInterval }; const handleTypeToggle = ( type ) => { return () => { setChartType( type ); const userDataFields = { dashboard_chart_type: type, }; updateUserPreferences( userDataFields ); recordEvent( 'dash_charts_type_toggle', { chart_type: type } ); }; }; const renderMenu = () => ( ( { __( 'Charts', 'woocommerce-admin' ) } { renderChartToggles( { hiddenBlocks, onToggleHiddenBlock, } ) } { window.wcAdminFeatures[ 'analytics-dashboard/customizable' ] && ( ) } ) } /> ); const setInterval = ( interval ) => { setChartInterval( interval ); const userDataFields = { dashboard_chart_interval: interval, }; updateUserPreferences( userDataFields ); recordEvent( 'dash_charts_interval', { interval } ); }; return (
{ renderIntervalSelector( { chartInterval, setInterval, query, } ) } { renderChartBlocks( { hiddenBlocks, path, query, filters } ) }
); }; DashboardCharts.propTypes = { path: PropTypes.string.isRequired, query: PropTypes.object.isRequired, }; export default DashboardCharts;