2018-05-03 18:23:17 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-01-17 14:08:59 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2018-10-31 02:07:16 +00:00
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2019-01-17 14:08:59 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import { withDispatch } from '@wordpress/data';
|
|
|
|
import moment from 'moment';
|
|
|
|
import { find } from 'lodash';
|
2019-05-02 10:22:34 +00:00
|
|
|
import { TextControl } from '@wordpress/components';
|
2018-05-03 18:23:17 +00:00
|
|
|
|
2019-01-18 13:52:10 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { getCurrentDates, appendTimestamp, getDateParamsFromQuery } from '@woocommerce/date';
|
|
|
|
import { getNewPath, getPersistedQuery } from '@woocommerce/navigation';
|
2019-01-29 16:48:46 +00:00
|
|
|
import { calculateDelta, formatValue } from '@woocommerce/number';
|
|
|
|
import { formatCurrency } from '@woocommerce/currency';
|
2019-01-18 13:52:10 +00:00
|
|
|
|
2018-05-10 19:23:41 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2018-08-20 21:18:13 +00:00
|
|
|
import {
|
|
|
|
EllipsisMenu,
|
|
|
|
MenuItem,
|
|
|
|
MenuTitle,
|
2018-10-31 02:07:16 +00:00
|
|
|
SectionHeader,
|
2018-08-20 21:18:13 +00:00
|
|
|
SummaryList,
|
2019-01-17 14:08:59 +00:00
|
|
|
SummaryListPlaceholder,
|
2018-08-20 21:18:13 +00:00
|
|
|
SummaryNumber,
|
|
|
|
} from '@woocommerce/components';
|
2019-01-17 14:08:59 +00:00
|
|
|
import withSelect from 'wc-api/with-select';
|
2018-06-29 18:27:18 +00:00
|
|
|
import './style.scss';
|
2018-05-10 19:23:41 +00:00
|
|
|
|
2018-07-02 18:08:50 +00:00
|
|
|
class StorePerformance extends Component {
|
2018-05-14 15:47:42 +00:00
|
|
|
renderMenu() {
|
2019-04-30 00:35:37 +00:00
|
|
|
const {
|
2019-05-07 07:21:34 +00:00
|
|
|
hiddenBlocks,
|
2019-04-30 00:35:37 +00:00
|
|
|
indicators,
|
2019-05-07 07:21:34 +00:00
|
|
|
isFirst,
|
|
|
|
isLast,
|
|
|
|
onMove,
|
|
|
|
onRemove,
|
2019-04-30 00:35:37 +00:00
|
|
|
onTitleBlur,
|
|
|
|
onTitleChange,
|
2019-05-07 07:21:34 +00:00
|
|
|
onToggleHiddenBlock,
|
2019-04-30 00:35:37 +00:00
|
|
|
titleInput,
|
2019-05-09 01:13:14 +00:00
|
|
|
controls: Controls,
|
2019-04-30 00:35:37 +00:00
|
|
|
} = this.props;
|
2019-05-02 10:22:34 +00:00
|
|
|
|
2018-05-14 15:47:42 +00:00
|
|
|
return (
|
2019-05-02 10:22:34 +00:00
|
|
|
<EllipsisMenu
|
|
|
|
label={ __(
|
|
|
|
'Choose which analytics to display and the section name',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-05-03 01:37:16 +00:00
|
|
|
renderContent={ ( { onToggle } ) => (
|
2019-04-30 00:35:37 +00:00
|
|
|
<Fragment>
|
2019-05-08 16:10:05 +00:00
|
|
|
{ window.wcAdminFeatures[ 'analytics-dashboard/customizable' ] && (
|
2019-04-30 00:35:37 +00:00
|
|
|
<div className="woocommerce-ellipsis-menu__item">
|
|
|
|
<TextControl
|
|
|
|
label={ __( 'Section Title', 'woocommerce-admin' ) }
|
|
|
|
onBlur={ onTitleBlur }
|
|
|
|
onChange={ onTitleChange }
|
|
|
|
required
|
|
|
|
value={ titleInput }
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
<MenuTitle>{ __( 'Display Stats:', 'woocommerce-admin' ) }</MenuTitle>
|
|
|
|
{ indicators.map( ( indicator, i ) => {
|
2019-05-07 07:21:34 +00:00
|
|
|
const checked = ! hiddenBlocks.includes( indicator.stat );
|
2019-04-30 00:35:37 +00:00
|
|
|
return (
|
|
|
|
<MenuItem
|
|
|
|
checked={ checked }
|
|
|
|
isCheckbox
|
|
|
|
isClickable
|
|
|
|
key={ i }
|
2019-05-07 07:21:34 +00:00
|
|
|
onInvoke={ () => onToggleHiddenBlock( indicator.stat )() }
|
2019-04-30 00:35:37 +00:00
|
|
|
>
|
|
|
|
{ sprintf( __( 'Show %s', 'woocommerce-admin' ), indicator.label ) }
|
|
|
|
</MenuItem>
|
|
|
|
);
|
|
|
|
} ) }
|
2019-05-09 01:13:14 +00:00
|
|
|
{ window.wcAdminFeatures[ 'analytics-dashboard/customizable' ] && (
|
|
|
|
<Controls
|
|
|
|
onToggle={ onToggle }
|
|
|
|
onMove={ onMove }
|
|
|
|
onRemove={ onRemove }
|
|
|
|
isFirst={ isFirst }
|
|
|
|
isLast={ isLast }
|
|
|
|
/>
|
|
|
|
) }
|
2019-04-30 00:35:37 +00:00
|
|
|
</Fragment>
|
2019-05-02 10:22:34 +00:00
|
|
|
) }
|
2019-04-30 00:35:37 +00:00
|
|
|
/>
|
2018-05-14 15:47:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-17 14:08:59 +00:00
|
|
|
renderList() {
|
|
|
|
const {
|
|
|
|
query,
|
|
|
|
primaryRequesting,
|
|
|
|
secondaryRequesting,
|
|
|
|
primaryError,
|
|
|
|
secondaryError,
|
|
|
|
primaryData,
|
|
|
|
secondaryData,
|
|
|
|
userIndicators,
|
|
|
|
} = this.props;
|
|
|
|
if ( primaryRequesting || secondaryRequesting ) {
|
|
|
|
return <SummaryListPlaceholder numberOfItems={ userIndicators.length } />;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( primaryError || secondaryError ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const persistedQuery = getPersistedQuery( query );
|
|
|
|
|
|
|
|
const { compare } = getDateParamsFromQuery( query );
|
|
|
|
const prevLabel =
|
|
|
|
'previous_period' === compare
|
2019-03-13 17:14:02 +00:00
|
|
|
? __( 'Previous Period:', 'woocommerce-admin' )
|
|
|
|
: __( 'Previous Year:', 'woocommerce-admin' );
|
2019-01-17 14:08:59 +00:00
|
|
|
return (
|
|
|
|
<SummaryList>
|
2019-01-17 03:28:41 +00:00
|
|
|
{ () =>
|
|
|
|
userIndicators.map( ( indicator, i ) => {
|
|
|
|
const primaryItem = find( primaryData.data, data => data.stat === indicator.stat );
|
|
|
|
const secondaryItem = find( secondaryData.data, data => data.stat === indicator.stat );
|
2018-05-14 15:47:42 +00:00
|
|
|
|
2019-01-17 03:28:41 +00:00
|
|
|
if ( ! primaryItem || ! secondaryItem ) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-01-17 14:08:59 +00:00
|
|
|
|
2019-01-17 03:28:41 +00:00
|
|
|
const href =
|
|
|
|
( primaryItem._links &&
|
|
|
|
primaryItem._links.report[ 0 ] &&
|
|
|
|
primaryItem._links.report[ 0 ].href ) ||
|
|
|
|
'';
|
|
|
|
const reportUrl =
|
|
|
|
( href && getNewPath( persistedQuery, href, { chart: primaryItem.chart } ) ) || '';
|
2019-01-29 16:48:46 +00:00
|
|
|
const isCurrency = 'currency' === primaryItem.format;
|
|
|
|
|
2019-01-17 03:28:41 +00:00
|
|
|
const delta = calculateDelta( primaryItem.value, secondaryItem.value );
|
2019-01-29 16:48:46 +00:00
|
|
|
const primaryValue = isCurrency
|
|
|
|
? formatCurrency( primaryItem.value )
|
|
|
|
: formatValue( primaryItem.format, primaryItem.value );
|
|
|
|
const secondaryValue = isCurrency
|
|
|
|
? formatCurrency( secondaryItem.value )
|
|
|
|
: formatValue( secondaryItem.format, secondaryItem.value );
|
2019-01-17 14:08:59 +00:00
|
|
|
|
2019-01-17 03:28:41 +00:00
|
|
|
return (
|
|
|
|
<SummaryNumber
|
|
|
|
key={ i }
|
|
|
|
href={ reportUrl }
|
|
|
|
label={ indicator.label }
|
|
|
|
value={ primaryValue }
|
|
|
|
prevLabel={ prevLabel }
|
|
|
|
prevValue={ secondaryValue }
|
|
|
|
delta={ delta }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} )
|
|
|
|
}
|
2019-01-17 14:08:59 +00:00
|
|
|
</SummaryList>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-05-07 07:20:48 +00:00
|
|
|
const { userIndicators, title } = this.props;
|
2018-05-03 18:23:17 +00:00
|
|
|
return (
|
2018-10-31 02:07:16 +00:00
|
|
|
<Fragment>
|
2019-03-13 17:14:02 +00:00
|
|
|
<SectionHeader
|
2019-05-02 10:22:34 +00:00
|
|
|
title={ title || __( 'Store Performance', 'woocommerce-admin' ) }
|
2019-03-13 17:14:02 +00:00
|
|
|
menu={ this.renderMenu() }
|
|
|
|
/>
|
2019-05-07 07:20:48 +00:00
|
|
|
{ userIndicators.length > 0 && (
|
|
|
|
<div className="woocommerce-dashboard__store-performance">{ this.renderList() }</div>
|
|
|
|
) }
|
2018-10-31 02:07:16 +00:00
|
|
|
</Fragment>
|
2018-05-03 18:23:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-01-17 14:08:59 +00:00
|
|
|
export default compose(
|
|
|
|
withSelect( ( select, props ) => {
|
2019-05-07 07:21:34 +00:00
|
|
|
const { hiddenBlocks, query } = props;
|
|
|
|
const { getReportItems, getReportItemsError, isReportItemsRequesting } = select( 'wc-api' );
|
2019-01-17 14:08:59 +00:00
|
|
|
|
|
|
|
const datesFromQuery = getCurrentDates( query );
|
|
|
|
const endPrimary = datesFromQuery.primary.before;
|
|
|
|
const endSecondary = datesFromQuery.secondary.before;
|
|
|
|
|
|
|
|
const indicators = wcSettings.dataEndpoints.performanceIndicators;
|
2019-04-03 01:36:20 +00:00
|
|
|
const userIndicators = indicators.filter(
|
2019-05-07 07:21:34 +00:00
|
|
|
indicator => ! hiddenBlocks.includes( indicator.stat )
|
2019-04-03 01:36:20 +00:00
|
|
|
);
|
2019-01-17 14:08:59 +00:00
|
|
|
const statKeys = userIndicators.map( indicator => indicator.stat ).join( ',' );
|
2018-05-03 18:23:17 +00:00
|
|
|
|
2019-05-07 07:20:48 +00:00
|
|
|
if ( statKeys.length === 0 ) {
|
|
|
|
return {
|
2019-05-08 08:41:38 +00:00
|
|
|
hiddenBlocks,
|
2019-05-07 07:20:48 +00:00
|
|
|
userIndicators,
|
|
|
|
indicators,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-17 14:08:59 +00:00
|
|
|
const primaryQuery = {
|
|
|
|
after: appendTimestamp( datesFromQuery.primary.after, 'start' ),
|
|
|
|
before: appendTimestamp( endPrimary, endPrimary.isSame( moment(), 'day' ) ? 'now' : 'end' ),
|
|
|
|
stats: statKeys,
|
|
|
|
};
|
|
|
|
|
|
|
|
const secondaryQuery = {
|
|
|
|
after: appendTimestamp( datesFromQuery.secondary.after, 'start' ),
|
|
|
|
before: appendTimestamp(
|
|
|
|
endSecondary,
|
|
|
|
endSecondary.isSame( moment(), 'day' ) ? 'now' : 'end'
|
|
|
|
),
|
|
|
|
stats: statKeys,
|
|
|
|
};
|
|
|
|
|
|
|
|
const primaryData = getReportItems( 'performance-indicators', primaryQuery );
|
|
|
|
const primaryError = getReportItemsError( 'performance-indicators', primaryQuery ) || null;
|
|
|
|
const primaryRequesting = isReportItemsRequesting( 'performance-indicators', primaryQuery );
|
|
|
|
|
|
|
|
const secondaryData = getReportItems( 'performance-indicators', secondaryQuery );
|
|
|
|
const secondaryError = getReportItemsError( 'performance-indicators', secondaryQuery ) || null;
|
|
|
|
const secondaryRequesting = isReportItemsRequesting( 'performance-indicators', secondaryQuery );
|
|
|
|
|
|
|
|
return {
|
2019-05-07 07:21:34 +00:00
|
|
|
hiddenBlocks,
|
2019-01-17 14:08:59 +00:00
|
|
|
userIndicators,
|
|
|
|
indicators,
|
|
|
|
primaryData,
|
|
|
|
primaryError,
|
|
|
|
primaryRequesting,
|
|
|
|
secondaryData,
|
|
|
|
secondaryError,
|
|
|
|
secondaryRequesting,
|
|
|
|
};
|
|
|
|
} ),
|
|
|
|
withDispatch( dispatch => {
|
|
|
|
const { updateCurrentUserData } = dispatch( 'wc-api' );
|
|
|
|
|
|
|
|
return {
|
|
|
|
updateCurrentUserData,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( StorePerformance );
|