woocommerce/plugins/woocommerce-admin/client/dashboard/store-performance/index.js

230 lines
5.1 KiB
JavaScript
Raw Normal View History

/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { Component, Fragment } from '@wordpress/element';
import { compose } from '@wordpress/compose';
2019-01-18 13:52:10 +00:00
/**
* WooCommerce dependencies
*/
import { getDateParamsFromQuery } from 'lib/date';
import { getPersistedQuery } from '@woocommerce/navigation';
import { getSetting } from '@woocommerce/wc-admin-settings';
wp.data Settings refactor add data store for settings using wp.data add use-select-with-refresh example replace fresh-data usage with new settings data store for settings page Add data package move to packages Fix isDirty after save Add isBusy to primary button when saving update Readme remove comment readme to use useSelect Revert "update Readme" This reverts commit 7402fd49b8f384fde5878e0bee0616f0a87bb4f6. Data Layer: Settings page to use Settings store (https://github.com/woocommerce/woocommerce-admin/pull/3430) * Data Layer: Settings store as source of truth for settings page This reverts commit 7402fd49b8f384fde5878e0bee0616f0a87bb4f6. * fixup * save on reset * non mutable constants * add set/getSettings * save using setSettings * separate HOC * cleanup * remove settingsToData * withHydration * remove withSettings HOC * renmove useSettins for now * withSettingsHydration updates * Revert "withSettingsHydration updates" This reverts commit f2adf108fbe19b574978fea5925a1a18e7ed3007. * rename withSettingsHydration * redo withSettingsHydration simplification * restore * useSettings * render using useSettings * handleInputChange working * get setIsDirty working * saving works * reset and cleanup * cleanup * use snake case on hook files * use clearIsDirty * Avoid mutation on setting update * remove @todo * persiting -> isPersisting * better reducer ternaries * add wcSettings as arg to withSettingsHydration reset package-lock Settings: split out mutable wcAdminSettings (https://github.com/woocommerce/woocommerce-admin/pull/3675) Settings: handle async settings groups (https://github.com/woocommerce/woocommerce-admin/pull/3707)
2020-03-25 03:20:17 +00:00
import { SETTINGS_STORE_NAME } from '@woocommerce/data';
2019-01-18 13:52:10 +00:00
/**
* Internal dependencies
*/
import {
EllipsisMenu,
MenuItem,
MenuTitle,
SectionHeader,
SummaryList,
SummaryListPlaceholder,
SummaryNumber,
} from '@woocommerce/components';
import withSelect from 'wc-api/with-select';
import './style.scss';
2019-07-01 10:16:12 +00:00
import { recordEvent } from 'lib/tracks';
import { CurrencyContext } from 'lib/currency-context';
import { getIndicatorData, getIndicatorValues } from './utils';
const { performanceIndicators: indicators } = getSetting( 'dataEndpoints', {
performanceIndicators: [],
} );
class StorePerformance extends Component {
renderMenu() {
2019-04-30 00:35:37 +00:00
const {
hiddenBlocks,
isFirst,
isLast,
onMove,
onRemove,
2019-04-30 00:35:37 +00:00
onTitleBlur,
onTitleChange,
onToggleHiddenBlock,
2019-04-30 00:35:37 +00:00
titleInput,
controls: Controls,
2019-04-30 00:35:37 +00:00
} = this.props;
return (
<EllipsisMenu
label={ __(
'Choose which analytics to display and the section name',
'woocommerce-admin'
) }
renderContent={ ( { onToggle } ) => (
2019-04-30 00:35:37 +00:00
<Fragment>
<MenuTitle>
{ __( 'Display Stats:', 'woocommerce-admin' ) }
</MenuTitle>
2019-04-30 00:35:37 +00:00
{ indicators.map( ( indicator, i ) => {
const checked = ! hiddenBlocks.includes(
indicator.stat
);
2019-04-30 00:35:37 +00:00
return (
<MenuItem
checked={ checked }
isCheckbox
isClickable
key={ i }
2019-07-01 10:16:12 +00:00
onInvoke={ () => {
onToggleHiddenBlock( indicator.stat )();
recordEvent( 'dash_indicators_toggle', {
status: checked ? 'off' : 'on',
key: indicator.stat,
} );
} }
2019-04-30 00:35:37 +00:00
>
{ indicator.label }
2019-04-30 00:35:37 +00:00
</MenuItem>
);
} ) }
{ window.wcAdminFeatures[
'analytics-dashboard/customizable'
] && (
2019-05-22 20:43:45 +00:00
<Controls
onToggle={ onToggle }
onMove={ onMove }
onRemove={ onRemove }
isFirst={ isFirst }
isLast={ isLast }
onTitleBlur={ onTitleBlur }
onTitleChange={ onTitleChange }
titleInput={ titleInput }
/>
) }
2019-04-30 00:35:37 +00:00
</Fragment>
) }
2019-04-30 00:35:37 +00:00
/>
);
}
renderList() {
const {
query,
primaryRequesting,
secondaryRequesting,
primaryError,
secondaryError,
primaryData,
secondaryData,
userIndicators,
wp.data Settings refactor add data store for settings using wp.data add use-select-with-refresh example replace fresh-data usage with new settings data store for settings page Add data package move to packages Fix isDirty after save Add isBusy to primary button when saving update Readme remove comment readme to use useSelect Revert "update Readme" This reverts commit 7402fd49b8f384fde5878e0bee0616f0a87bb4f6. Data Layer: Settings page to use Settings store (https://github.com/woocommerce/woocommerce-admin/pull/3430) * Data Layer: Settings store as source of truth for settings page This reverts commit 7402fd49b8f384fde5878e0bee0616f0a87bb4f6. * fixup * save on reset * non mutable constants * add set/getSettings * save using setSettings * separate HOC * cleanup * remove settingsToData * withHydration * remove withSettings HOC * renmove useSettins for now * withSettingsHydration updates * Revert "withSettingsHydration updates" This reverts commit f2adf108fbe19b574978fea5925a1a18e7ed3007. * rename withSettingsHydration * redo withSettingsHydration simplification * restore * useSettings * render using useSettings * handleInputChange working * get setIsDirty working * saving works * reset and cleanup * cleanup * use snake case on hook files * use clearIsDirty * Avoid mutation on setting update * remove @todo * persiting -> isPersisting * better reducer ternaries * add wcSettings as arg to withSettingsHydration reset package-lock Settings: split out mutable wcAdminSettings (https://github.com/woocommerce/woocommerce-admin/pull/3675) Settings: handle async settings groups (https://github.com/woocommerce/woocommerce-admin/pull/3707)
2020-03-25 03:20:17 +00:00
defaultDateRange,
} = this.props;
if ( primaryRequesting || secondaryRequesting ) {
return (
<SummaryListPlaceholder
numberOfItems={ userIndicators.length }
/>
);
}
if ( primaryError || secondaryError ) {
return null;
}
const persistedQuery = getPersistedQuery( query );
wp.data Settings refactor add data store for settings using wp.data add use-select-with-refresh example replace fresh-data usage with new settings data store for settings page Add data package move to packages Fix isDirty after save Add isBusy to primary button when saving update Readme remove comment readme to use useSelect Revert "update Readme" This reverts commit 7402fd49b8f384fde5878e0bee0616f0a87bb4f6. Data Layer: Settings page to use Settings store (https://github.com/woocommerce/woocommerce-admin/pull/3430) * Data Layer: Settings store as source of truth for settings page This reverts commit 7402fd49b8f384fde5878e0bee0616f0a87bb4f6. * fixup * save on reset * non mutable constants * add set/getSettings * save using setSettings * separate HOC * cleanup * remove settingsToData * withHydration * remove withSettings HOC * renmove useSettins for now * withSettingsHydration updates * Revert "withSettingsHydration updates" This reverts commit f2adf108fbe19b574978fea5925a1a18e7ed3007. * rename withSettingsHydration * redo withSettingsHydration simplification * restore * useSettings * render using useSettings * handleInputChange working * get setIsDirty working * saving works * reset and cleanup * cleanup * use snake case on hook files * use clearIsDirty * Avoid mutation on setting update * remove @todo * persiting -> isPersisting * better reducer ternaries * add wcSettings as arg to withSettingsHydration reset package-lock Settings: split out mutable wcAdminSettings (https://github.com/woocommerce/woocommerce-admin/pull/3675) Settings: handle async settings groups (https://github.com/woocommerce/woocommerce-admin/pull/3707)
2020-03-25 03:20:17 +00:00
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 (
<SummaryList>
{ () =>
userIndicators.map( ( indicator, i ) => {
const {
primaryValue,
secondaryValue,
delta,
reportUrl,
reportUrlType,
} = getIndicatorValues( {
indicator,
primaryData,
secondaryData,
currency,
formatAmount,
persistedQuery,
} );
return (
<SummaryNumber
key={ i }
href={ reportUrl }
hrefType={ reportUrlType }
label={ indicator.label }
value={ primaryValue }
prevLabel={ prevLabel }
prevValue={ secondaryValue }
delta={ delta }
2019-07-01 10:16:12 +00:00
onLinkClickCallback={ () => {
recordEvent( 'dash_indicators_click', {
key: indicator.stat,
} );
2019-07-01 10:16:12 +00:00
} }
/>
);
} )
}
</SummaryList>
);
}
render() {
const { userIndicators, title } = this.props;
return (
<Fragment>
<SectionHeader
title={
title || __( 'Store Performance', 'woocommerce-admin' )
}
menu={ this.renderMenu() }
/>
{ userIndicators.length > 0 && (
<div className="woocommerce-dashboard__store-performance">
{ this.renderList() }
</div>
) }
</Fragment>
);
}
}
StorePerformance.contextType = CurrencyContext;
export default compose(
withSelect( ( select, props ) => {
const { hiddenBlocks, query } = 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,
wp.data Settings refactor add data store for settings using wp.data add use-select-with-refresh example replace fresh-data usage with new settings data store for settings page Add data package move to packages Fix isDirty after save Add isBusy to primary button when saving update Readme remove comment readme to use useSelect Revert "update Readme" This reverts commit 7402fd49b8f384fde5878e0bee0616f0a87bb4f6. Data Layer: Settings page to use Settings store (https://github.com/woocommerce/woocommerce-admin/pull/3430) * Data Layer: Settings store as source of truth for settings page This reverts commit 7402fd49b8f384fde5878e0bee0616f0a87bb4f6. * fixup * save on reset * non mutable constants * add set/getSettings * save using setSettings * separate HOC * cleanup * remove settingsToData * withHydration * remove withSettings HOC * renmove useSettins for now * withSettingsHydration updates * Revert "withSettingsHydration updates" This reverts commit f2adf108fbe19b574978fea5925a1a18e7ed3007. * rename withSettingsHydration * redo withSettingsHydration simplification * restore * useSettings * render using useSettings * handleInputChange working * get setIsDirty working * saving works * reset and cleanup * cleanup * use snake case on hook files * use clearIsDirty * Avoid mutation on setting update * remove @todo * persiting -> isPersisting * better reducer ternaries * add wcSettings as arg to withSettingsHydration reset package-lock Settings: split out mutable wcAdminSettings (https://github.com/woocommerce/woocommerce-admin/pull/3675) Settings: handle async settings groups (https://github.com/woocommerce/woocommerce-admin/pull/3707)
2020-03-25 03:20:17 +00:00
defaultDateRange,
};
if ( userIndicators.length === 0 ) {
return data;
}
const indicatorData = getIndicatorData( select, userIndicators, query );
return {
...data,
...indicatorData,
};
} )
)( StorePerformance );