/** * External dependencies */ import { __ } from '@wordpress/i18n'; import { Component, Fragment } from '@wordpress/element'; import { compose } from '@wordpress/compose'; import { getPersistedQuery } from '@woocommerce/navigation'; import { withSelect } from '@wordpress/data'; import { SETTINGS_STORE_NAME } from '@woocommerce/data'; import { EllipsisMenu, MenuItem, MenuTitle, SectionHeader, SummaryList, SummaryListPlaceholder, SummaryNumber, } from '@woocommerce/components'; import { getDateParamsFromQuery } from '@woocommerce/date'; import { recordEvent } from '@woocommerce/tracks'; /** * Internal dependencies */ import './style.scss'; import { CurrencyContext } from '../../lib/currency-context'; import { getIndicatorData, getIndicatorValues } from './utils'; import { getAdminSetting } from '~/utils/admin-settings'; const { performanceIndicators: indicators } = getAdminSetting( 'dataEndpoints', { performanceIndicators: [], } ); class StorePerformance extends Component { renderMenu() { const { hiddenBlocks, isFirst, isLast, onMove, onRemove, onTitleBlur, onTitleChange, onToggleHiddenBlock, titleInput, controls: Controls, } = this.props; return ( ( { __( 'Display stats:', 'woocommerce-admin' ) } { indicators.map( ( indicator, i ) => { const checked = ! hiddenBlocks.includes( indicator.stat ); return ( { onToggleHiddenBlock( indicator.stat )(); recordEvent( 'dash_indicators_toggle', { status: checked ? 'off' : 'on', key: indicator.stat, } ); } } > { indicator.label } ); } ) } ) } /> ); } renderList() { const { query, primaryRequesting, secondaryRequesting, primaryError, secondaryError, primaryData, secondaryData, userIndicators, defaultDateRange, } = this.props; if ( primaryRequesting || secondaryRequesting ) { return ( ); } if ( primaryError || secondaryError ) { return null; } const persistedQuery = getPersistedQuery( query ); const { compare } = getDateParamsFromQuery( query, defaultDateRange ); const prevLabel = compare === 'previous_period' ? __( 'Previous period:', 'woocommerce-admin' ) : __( 'Previous year:', 'woocommerce-admin' ); const { formatAmount, getCurrencyConfig } = this.context; const currency = getCurrencyConfig(); return ( { () => userIndicators.map( ( indicator, i ) => { const { primaryValue, secondaryValue, delta, reportUrl, reportUrlType, } = getIndicatorValues( { indicator, primaryData, secondaryData, currency, formatAmount, persistedQuery, } ); return ( { recordEvent( 'dash_indicators_click', { key: indicator.stat, } ); } } /> ); } ) } ); } render() { const { userIndicators, title } = this.props; return ( { userIndicators.length > 0 && (
{ this.renderList() }
) }
); } } StorePerformance.contextType = CurrencyContext; export default compose( withSelect( ( select, props ) => { const { hiddenBlocks, query, filters } = props; const userIndicators = indicators.filter( ( indicator ) => ! hiddenBlocks.includes( indicator.stat ) ); const { woocommerce_default_date_range: defaultDateRange } = select( SETTINGS_STORE_NAME ).getSetting( 'wc_admin', 'wcAdminSettings' ); const data = { hiddenBlocks, userIndicators, indicators, defaultDateRange, }; if ( userIndicators.length === 0 ) { return data; } const indicatorData = getIndicatorData( select, userIndicators, query, filters ); return { ...data, ...indicatorData, }; } ) )( StorePerformance );