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';
|
2019-10-11 12:55:35 +00:00
|
|
|
import { partial, filter, 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';
|
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-12-31 08:50:45 +00:00
|
|
|
import { getAllTasks } from './task-list/tasks';
|
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>
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-22 13:23:37 +00:00
|
|
|
render() {
|
2019-10-11 12:55:35 +00:00
|
|
|
const { query, path, taskListHidden, taskListCompleted } = this.props;
|
2019-05-02 10:22:34 +00:00
|
|
|
const { sections } = this.state;
|
|
|
|
|
2019-10-11 12:55:35 +00:00
|
|
|
if (
|
2019-11-07 18:31:02 +00:00
|
|
|
isOnboardingEnabled() &&
|
2019-10-11 12:55:35 +00:00
|
|
|
wcSettings.onboarding &&
|
|
|
|
! taskListHidden &&
|
|
|
|
( query.task || ! taskListCompleted )
|
|
|
|
) {
|
|
|
|
return <TaskList query={ query } />;
|
|
|
|
}
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const { period, compare, before, after } = getDateParamsFromQuery(
|
|
|
|
query
|
|
|
|
);
|
|
|
|
const {
|
|
|
|
primary: primaryDate,
|
|
|
|
secondary: secondaryDate,
|
|
|
|
} = getCurrentDates( query );
|
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-07 18:31:02 +00:00
|
|
|
{ isOnboardingEnabled() &&
|
2019-10-11 12:55:35 +00:00
|
|
|
wcSettings.onboarding &&
|
|
|
|
! taskListHidden &&
|
|
|
|
taskListCompleted && <TaskList query={ query } inline /> }
|
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-05-07 07:21:34 +00:00
|
|
|
|
|
|
|
export default compose(
|
2019-10-11 12:55:35 +00:00
|
|
|
withSelect( ( select, props ) => {
|
2020-02-14 02:23:21 +00:00
|
|
|
const { getCurrentUserData, getProfileItems, getOptions } = select(
|
|
|
|
'wc-api'
|
|
|
|
);
|
2019-05-07 07:21:34 +00:00
|
|
|
const userData = getCurrentUserData();
|
|
|
|
|
2019-10-16 20:53:45 +00:00
|
|
|
const withSelectData = {
|
2019-05-07 07:21:34 +00:00
|
|
|
userPrefSections: userData.dashboard_sections,
|
|
|
|
};
|
2019-10-16 20:53:45 +00:00
|
|
|
|
2019-11-07 18:31:02 +00:00
|
|
|
if ( isOnboardingEnabled() ) {
|
2019-10-16 20:53:45 +00:00
|
|
|
const profileItems = getProfileItems();
|
2019-12-31 08:50:45 +00:00
|
|
|
const tasks = getAllTasks( {
|
2019-10-16 20:53:45 +00:00
|
|
|
profileItems,
|
2019-11-25 16:15:22 +00:00
|
|
|
options: getOptions( [ 'woocommerce_task_list_payments' ] ),
|
2019-10-16 20:53:45 +00:00
|
|
|
query: props.query,
|
|
|
|
} );
|
2020-02-14 02:23:21 +00:00
|
|
|
const visibleTasks = filter( tasks, ( task ) => task.visible );
|
|
|
|
const completedTasks = filter(
|
|
|
|
tasks,
|
|
|
|
( task ) => task.visible && task.completed
|
|
|
|
);
|
|
|
|
|
|
|
|
withSelectData.taskListCompleted =
|
|
|
|
visibleTasks.length === completedTasks.length;
|
2019-10-16 20:53:45 +00:00
|
|
|
withSelectData.taskListHidden =
|
|
|
|
get(
|
|
|
|
getOptions( [ 'woocommerce_task_list_hidden' ] ),
|
|
|
|
[ 'woocommerce_task_list_hidden' ],
|
|
|
|
'no'
|
2020-02-14 02:23:21 +00:00
|
|
|
) === 'yes';
|
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 );
|