2019-04-22 13:23:37 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-04-30 00:35:37 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2019-04-22 13:23:37 +00:00
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2019-05-07 07:21:34 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
2020-03-23 20:47:12 +00:00
|
|
|
import { partial, get } from 'lodash';
|
2019-04-30 00:35:37 +00:00
|
|
|
import { IconButton, Icon, Dropdown, Button } from '@wordpress/components';
|
2019-05-07 07:21:34 +00:00
|
|
|
import { withDispatch } from '@wordpress/data';
|
2019-12-05 22:38:26 +00:00
|
|
|
import { applyFilters } from '@wordpress/hooks';
|
2019-05-07 07:21:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
2019-11-27 23:12:33 +00:00
|
|
|
import { H } from '@woocommerce/components';
|
2020-04-27 14:41:26 +00:00
|
|
|
import { SETTINGS_STORE_NAME } from '@woocommerce/data';
|
2019-04-22 13:23:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2019-05-07 07:21:34 +00:00
|
|
|
import defaultSections from './default-sections';
|
2019-05-09 01:13:14 +00:00
|
|
|
import Section from './section';
|
2019-05-07 07:21:34 +00:00
|
|
|
import withSelect from 'wc-api/with-select';
|
2019-07-01 10:16:12 +00:00
|
|
|
import { recordEvent } from 'lib/tracks';
|
2019-10-11 12:55:35 +00:00
|
|
|
import TaskList from './task-list';
|
2019-11-07 18:31:02 +00:00
|
|
|
import { isOnboardingEnabled } from 'dashboard/utils';
|
2020-02-14 02:23:21 +00:00
|
|
|
import {
|
|
|
|
getCurrentDates,
|
|
|
|
getDateParamsFromQuery,
|
|
|
|
isoDateFormat,
|
|
|
|
} from 'lib/date';
|
2019-11-27 23:12:33 +00:00
|
|
|
import ReportFilters from 'analytics/components/report-filters';
|
2019-04-22 13:23:37 +00:00
|
|
|
|
2019-12-05 22:38:26 +00:00
|
|
|
const DASHBOARD_FILTERS_FILTER = 'woocommerce_admin_dashboard_filters';
|
|
|
|
const filters = applyFilters( DASHBOARD_FILTERS_FILTER, [] );
|
|
|
|
|
2019-05-07 07:21:34 +00:00
|
|
|
class CustomizableDashboard extends Component {
|
2019-05-02 10:22:34 +00:00
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
2019-05-07 07:21:34 +00:00
|
|
|
|
2019-05-02 10:22:34 +00:00
|
|
|
this.state = {
|
2019-05-07 07:21:34 +00:00
|
|
|
sections: this.mergeSectionsWithDefaults( props.userPrefSections ),
|
2019-05-02 10:22:34 +00:00
|
|
|
};
|
2019-04-30 00:35:37 +00:00
|
|
|
|
|
|
|
this.onMove = this.onMove.bind( this );
|
2019-05-07 07:21:34 +00:00
|
|
|
this.updateSection = this.updateSection.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
mergeSectionsWithDefaults( prefSections ) {
|
|
|
|
if ( ! prefSections || prefSections.length === 0 ) {
|
|
|
|
return defaultSections;
|
|
|
|
}
|
2020-02-14 02:23:21 +00:00
|
|
|
const defaultKeys = defaultSections.map( ( section ) => section.key );
|
|
|
|
const prefKeys = prefSections.map( ( section ) => section.key );
|
2019-05-07 07:21:34 +00:00
|
|
|
const keys = new Set( [ ...prefKeys, ...defaultKeys ] );
|
|
|
|
const sections = [];
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
keys.forEach( ( key ) => {
|
|
|
|
const defaultSection = defaultSections.find(
|
|
|
|
( section ) => section.key === key
|
|
|
|
);
|
2019-05-07 07:21:34 +00:00
|
|
|
if ( ! defaultSection ) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-14 02:23:21 +00:00
|
|
|
const prefSection = prefSections.find(
|
|
|
|
( section ) => section.key === key
|
|
|
|
);
|
2019-05-07 07:21:34 +00:00
|
|
|
|
|
|
|
sections.push( {
|
|
|
|
...defaultSection,
|
|
|
|
...prefSection,
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
return sections;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSections( newSections ) {
|
|
|
|
this.setState( { sections: newSections } );
|
|
|
|
this.props.updateCurrentUserData( { dashboard_sections: newSections } );
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSection( updatedKey, newSettings ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
const newSections = this.state.sections.map( ( section ) => {
|
2019-05-07 07:21:34 +00:00
|
|
|
if ( section.key === updatedKey ) {
|
|
|
|
return {
|
|
|
|
...section,
|
|
|
|
...newSettings,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return section;
|
|
|
|
} );
|
|
|
|
this.updateSections( newSections );
|
|
|
|
}
|
|
|
|
|
|
|
|
onChangeHiddenBlocks( updatedKey ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
return ( updatedHiddenBlocks ) => {
|
|
|
|
this.updateSection( updatedKey, {
|
|
|
|
hiddenBlocks: updatedHiddenBlocks,
|
|
|
|
} );
|
2019-05-07 07:21:34 +00:00
|
|
|
};
|
2019-05-02 10:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onSectionTitleUpdate( updatedKey ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
return ( updatedTitle ) => {
|
2019-07-10 23:02:37 +00:00
|
|
|
recordEvent( 'dash_section_rename', { key: updatedKey } );
|
2019-05-07 07:21:34 +00:00
|
|
|
this.updateSection( updatedKey, { title: updatedTitle } );
|
2019-05-02 10:22:34 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-30 00:35:37 +00:00
|
|
|
toggleVisibility( key, onToggle ) {
|
|
|
|
return () => {
|
|
|
|
if ( onToggle ) {
|
|
|
|
// Close the dropdown before setting state so an action is not performed on an unmounted component.
|
|
|
|
onToggle();
|
|
|
|
}
|
2019-05-07 07:21:34 +00:00
|
|
|
// When toggling visibility, place section at the end of the array.
|
|
|
|
const sections = [ ...this.state.sections ];
|
2020-02-14 02:23:21 +00:00
|
|
|
const index = sections.findIndex( ( s ) => key === s.key );
|
2019-05-07 07:21:34 +00:00
|
|
|
const toggledSection = sections.splice( index, 1 ).shift();
|
|
|
|
toggledSection.isVisible = ! toggledSection.isVisible;
|
|
|
|
sections.push( toggledSection );
|
|
|
|
|
2019-07-10 22:52:15 +00:00
|
|
|
if ( toggledSection.isVisible ) {
|
|
|
|
recordEvent( 'dash_section_add', { key: toggledSection.key } );
|
|
|
|
} else {
|
2020-02-14 02:23:21 +00:00
|
|
|
recordEvent( 'dash_section_remove', {
|
|
|
|
key: toggledSection.key,
|
|
|
|
} );
|
2019-07-01 10:16:12 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 07:21:34 +00:00
|
|
|
this.updateSections( sections );
|
2019-04-30 00:35:37 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
onMove( index, change ) {
|
|
|
|
const sections = [ ...this.state.sections ];
|
|
|
|
const movedSection = sections.splice( index, 1 ).shift();
|
2019-05-09 01:13:14 +00:00
|
|
|
const newIndex = index + change;
|
|
|
|
|
|
|
|
// Figure out the index of the skipped section.
|
|
|
|
const nextJumpedSectionIndex = change < 0 ? newIndex : newIndex - 1;
|
|
|
|
|
|
|
|
if (
|
|
|
|
sections[ nextJumpedSectionIndex ].isVisible || // Is the skipped section visible?
|
|
|
|
index === 0 || // Will this be the first element?
|
|
|
|
index === sections.length - 1 // Will this be the last element?
|
|
|
|
) {
|
|
|
|
// Yes, lets insert.
|
|
|
|
sections.splice( newIndex, 0, movedSection );
|
|
|
|
this.updateSections( sections );
|
2019-07-10 22:42:02 +00:00
|
|
|
|
|
|
|
const eventProps = {
|
|
|
|
key: movedSection.key,
|
2020-02-14 02:23:21 +00:00
|
|
|
direction: change > 0 ? 'down' : 'up',
|
2019-07-10 22:42:02 +00:00
|
|
|
};
|
|
|
|
recordEvent( 'dash_section_order_change', eventProps );
|
2019-05-09 01:13:14 +00:00
|
|
|
} else {
|
|
|
|
// No, lets try the next one.
|
|
|
|
this.onMove( index, change + change );
|
|
|
|
}
|
2019-04-30 00:35:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderAddMore() {
|
|
|
|
const { sections } = this.state;
|
2020-02-14 02:23:21 +00:00
|
|
|
const hiddenSections = sections.filter(
|
|
|
|
( section ) => section.isVisible === false
|
|
|
|
);
|
2019-04-30 00:35:37 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
if ( hiddenSections.length === 0 ) {
|
2019-04-30 00:35:37 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dropdown
|
|
|
|
position="top center"
|
|
|
|
className="woocommerce-dashboard-section__add-more"
|
|
|
|
renderToggle={ ( { onToggle, isOpen } ) => (
|
|
|
|
<IconButton
|
|
|
|
onClick={ onToggle }
|
|
|
|
icon="plus-alt"
|
|
|
|
title={ __( 'Add more sections', 'woocommerce-admin' ) }
|
|
|
|
aria-expanded={ isOpen }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
renderContent={ ( { onToggle } ) => (
|
|
|
|
<Fragment>
|
2020-02-14 02:23:21 +00:00
|
|
|
<H>
|
|
|
|
{ __( 'Dashboard Sections', 'woocommerce-admin' ) }
|
|
|
|
</H>
|
2019-04-30 00:35:37 +00:00
|
|
|
<div className="woocommerce-dashboard-section__add-more-choices">
|
2020-02-14 02:23:21 +00:00
|
|
|
{ hiddenSections.map( ( section ) => {
|
2019-04-30 00:35:37 +00:00
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
key={ section.key }
|
2020-02-14 02:23:21 +00:00
|
|
|
onClick={ this.toggleVisibility(
|
|
|
|
section.key,
|
|
|
|
onToggle
|
|
|
|
) }
|
2019-04-30 00:35:37 +00:00
|
|
|
className="woocommerce-dashboard-section__add-more-btn"
|
2020-02-14 02:23:21 +00:00
|
|
|
title={ sprintf(
|
|
|
|
__(
|
|
|
|
'Add %s section',
|
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
|
|
|
section.title
|
|
|
|
) }
|
2019-04-30 00:35:37 +00:00
|
|
|
>
|
2020-02-14 02:23:21 +00:00
|
|
|
<Icon
|
|
|
|
icon={ section.icon }
|
|
|
|
size={ 30 }
|
|
|
|
/>
|
2019-04-30 00:35:37 +00:00
|
|
|
<span className="woocommerce-dashboard-section__add-more-btn-title">
|
|
|
|
{ section.title }
|
|
|
|
</span>
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-23 20:47:12 +00:00
|
|
|
renderDashboardReports() {
|
2020-03-25 03:20:17 +00:00
|
|
|
const { query, path, defaultDateRange } = this.props;
|
2019-05-02 10:22:34 +00:00
|
|
|
const { sections } = this.state;
|
2020-02-14 02:23:21 +00:00
|
|
|
const { period, compare, before, after } = getDateParamsFromQuery(
|
2020-03-25 03:20:17 +00:00
|
|
|
query,
|
|
|
|
defaultDateRange
|
2020-02-14 02:23:21 +00:00
|
|
|
);
|
|
|
|
const {
|
|
|
|
primary: primaryDate,
|
|
|
|
secondary: secondaryDate,
|
2020-03-25 03:20:17 +00:00
|
|
|
} = getCurrentDates( query, defaultDateRange );
|
2019-11-26 19:39:40 +00:00
|
|
|
const dateQuery = {
|
|
|
|
period,
|
|
|
|
compare,
|
|
|
|
before,
|
|
|
|
after,
|
|
|
|
primaryDate,
|
|
|
|
secondaryDate,
|
|
|
|
};
|
2020-02-14 02:23:21 +00:00
|
|
|
const visibleSectionKeys = sections
|
|
|
|
.filter( ( section ) => section.isVisible )
|
|
|
|
.map( ( section ) => section.key );
|
|
|
|
|
2019-04-22 13:23:37 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
2019-11-27 23:12:33 +00:00
|
|
|
<ReportFilters
|
|
|
|
report="dashboard"
|
|
|
|
query={ query }
|
|
|
|
path={ path }
|
|
|
|
dateQuery={ dateQuery }
|
|
|
|
isoDateFormat={ isoDateFormat }
|
2019-12-05 22:38:26 +00:00
|
|
|
filters={ filters }
|
2019-11-27 23:12:33 +00:00
|
|
|
/>
|
2019-05-09 01:13:14 +00:00
|
|
|
{ sections.map( ( section, index ) => {
|
|
|
|
if ( section.isVisible ) {
|
|
|
|
return (
|
|
|
|
<Section
|
|
|
|
component={ section.component }
|
|
|
|
hiddenBlocks={ section.hiddenBlocks }
|
|
|
|
key={ section.key }
|
2020-02-14 02:23:21 +00:00
|
|
|
onChangeHiddenBlocks={ this.onChangeHiddenBlocks(
|
|
|
|
section.key
|
|
|
|
) }
|
|
|
|
onTitleUpdate={ this.onSectionTitleUpdate(
|
|
|
|
section.key
|
|
|
|
) }
|
2019-05-09 01:13:14 +00:00
|
|
|
path={ path }
|
|
|
|
query={ query }
|
|
|
|
title={ section.title }
|
|
|
|
onMove={ partial( this.onMove, index ) }
|
2020-02-14 02:23:21 +00:00
|
|
|
onRemove={ this.toggleVisibility(
|
|
|
|
section.key
|
|
|
|
) }
|
|
|
|
isFirst={
|
|
|
|
section.key === visibleSectionKeys[ 0 ]
|
|
|
|
}
|
|
|
|
isLast={
|
|
|
|
section.key ===
|
|
|
|
visibleSectionKeys[
|
|
|
|
visibleSectionKeys.length - 1
|
|
|
|
]
|
|
|
|
}
|
2019-05-09 01:13:14 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
2019-05-02 10:22:34 +00:00
|
|
|
} ) }
|
2019-04-30 00:35:37 +00:00
|
|
|
{ this.renderAddMore() }
|
2019-04-22 13:23:37 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
2020-03-23 20:47:12 +00:00
|
|
|
|
|
|
|
render() {
|
2020-04-08 21:27:24 +00:00
|
|
|
const { query, taskListHidden, taskListComplete } = this.props;
|
2020-03-23 20:47:12 +00:00
|
|
|
|
|
|
|
const isTaskListEnabled = isOnboardingEnabled() && ! taskListHidden;
|
|
|
|
|
|
|
|
const isDashboardShown =
|
2020-04-08 21:27:24 +00:00
|
|
|
! isTaskListEnabled || ( ! query.task && taskListComplete );
|
2020-03-23 20:47:12 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
{ isTaskListEnabled && (
|
2020-04-27 14:41:26 +00:00
|
|
|
<TaskList
|
2020-04-16 23:58:36 +00:00
|
|
|
query={ query }
|
|
|
|
inline={ isDashboardShown }
|
|
|
|
/>
|
2020-03-23 20:47:12 +00:00
|
|
|
) }
|
|
|
|
{ isDashboardShown && this.renderDashboardReports() }
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
2019-04-22 13:23:37 +00:00
|
|
|
}
|
2019-05-07 07:21:34 +00:00
|
|
|
|
|
|
|
export default compose(
|
2020-03-23 20:47:12 +00:00
|
|
|
withSelect( ( select ) => {
|
2020-03-13 04:34:53 +00:00
|
|
|
const {
|
|
|
|
getCurrentUserData,
|
|
|
|
isGetProfileItemsRequesting,
|
|
|
|
getOptions,
|
|
|
|
isGetOptionsRequesting,
|
|
|
|
} = select( 'wc-api' );
|
2019-05-07 07:21:34 +00:00
|
|
|
const userData = getCurrentUserData();
|
2020-03-25 03:20:17 +00:00
|
|
|
const { woocommerce_default_date_range: defaultDateRange } = select(
|
|
|
|
SETTINGS_STORE_NAME
|
|
|
|
).getSetting( 'wc_admin', 'wcAdminSettings' );
|
|
|
|
|
2019-10-16 20:53:45 +00:00
|
|
|
const withSelectData = {
|
2019-05-07 07:21:34 +00:00
|
|
|
userPrefSections: userData.dashboard_sections,
|
2020-03-25 03:20:17 +00:00
|
|
|
defaultDateRange,
|
2020-03-13 04:34:53 +00:00
|
|
|
requesting: false,
|
2019-05-07 07:21:34 +00:00
|
|
|
};
|
2019-10-16 20:53:45 +00:00
|
|
|
|
2019-11-07 18:31:02 +00:00
|
|
|
if ( isOnboardingEnabled() ) {
|
2020-03-13 04:34:53 +00:00
|
|
|
const options = getOptions( [
|
2020-03-23 20:47:12 +00:00
|
|
|
'woocommerce_task_list_complete',
|
2020-03-13 04:34:53 +00:00
|
|
|
'woocommerce_task_list_hidden',
|
|
|
|
] );
|
2019-10-16 20:53:45 +00:00
|
|
|
withSelectData.taskListHidden =
|
2020-03-13 04:34:53 +00:00
|
|
|
get( options, [ 'woocommerce_task_list_hidden' ], 'no' ) ===
|
|
|
|
'yes';
|
2020-03-23 20:47:12 +00:00
|
|
|
withSelectData.taskListComplete = get(
|
|
|
|
options,
|
|
|
|
[ 'woocommerce_task_list_complete' ],
|
|
|
|
false
|
|
|
|
);
|
2020-03-13 04:34:53 +00:00
|
|
|
withSelectData.requesting =
|
|
|
|
withSelectData.requesting || isGetProfileItemsRequesting();
|
|
|
|
withSelectData.requesting =
|
|
|
|
withSelectData.requesting ||
|
|
|
|
isGetOptionsRequesting( [
|
|
|
|
'woocommerce_task_list_payments',
|
|
|
|
'woocommerce_task_list_hidden',
|
|
|
|
] );
|
2019-10-16 20:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return withSelectData;
|
2019-05-07 07:21:34 +00:00
|
|
|
} ),
|
2020-02-14 02:23:21 +00:00
|
|
|
withDispatch( ( dispatch ) => {
|
2019-05-07 07:21:34 +00:00
|
|
|
const { updateCurrentUserData } = dispatch( 'wc-api' );
|
|
|
|
|
|
|
|
return {
|
|
|
|
updateCurrentUserData,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( CustomizableDashboard );
|