2019-04-22 13:23:37 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-04-30 00:35:37 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2020-06-22 21:25:01 +00:00
|
|
|
import { Fragment, Suspense, lazy } from '@wordpress/element';
|
2019-05-07 07:21:34 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
2020-06-10 23:49:27 +00:00
|
|
|
import { partial } from 'lodash';
|
2020-05-29 02:32:37 +00:00
|
|
|
import { Dropdown, Button, Icon } from '@wordpress/components';
|
2019-12-05 22:38:26 +00:00
|
|
|
import { applyFilters } from '@wordpress/hooks';
|
2020-05-29 02:32:37 +00:00
|
|
|
import { Icon as WPIcon, plusCircleFilled } from '@wordpress/icons';
|
2019-05-07 07:21:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
2020-04-29 18:01:27 +00:00
|
|
|
import { H, Spinner } from '@woocommerce/components';
|
2020-06-10 16:46:46 +00:00
|
|
|
import {
|
|
|
|
SETTINGS_STORE_NAME,
|
2020-06-10 23:49:27 +00:00
|
|
|
OPTIONS_STORE_NAME,
|
2020-06-10 16:46:46 +00:00
|
|
|
useUserPreferences,
|
|
|
|
} 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-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
|
|
|
|
2020-04-29 18:01:27 +00:00
|
|
|
const TaskList = lazy( () =>
|
2020-05-21 17:15:08 +00:00
|
|
|
import( /* webpackChunkName: "task-list" */ '../task-list' )
|
2020-04-29 18:01:27 +00:00
|
|
|
);
|
|
|
|
|
2019-12-05 22:38:26 +00:00
|
|
|
const DASHBOARD_FILTERS_FILTER = 'woocommerce_admin_dashboard_filters';
|
|
|
|
const filters = applyFilters( DASHBOARD_FILTERS_FILTER, [] );
|
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
const mergeSectionsWithDefaults = ( prefSections ) => {
|
|
|
|
if ( ! prefSections || prefSections.length === 0 ) {
|
|
|
|
return defaultSections;
|
2019-05-07 07:21:34 +00:00
|
|
|
}
|
2020-06-10 16:46:46 +00:00
|
|
|
const defaultKeys = defaultSections.map( ( section ) => section.key );
|
|
|
|
const prefKeys = prefSections.map( ( section ) => section.key );
|
|
|
|
const keys = new Set( [ ...prefKeys, ...defaultKeys ] );
|
|
|
|
const sections = [];
|
|
|
|
|
|
|
|
keys.forEach( ( key ) => {
|
|
|
|
const defaultSection = defaultSections.find(
|
|
|
|
( section ) => section.key === key
|
|
|
|
);
|
|
|
|
if ( ! defaultSection ) {
|
|
|
|
return;
|
2019-05-07 07:21:34 +00:00
|
|
|
}
|
2020-06-10 16:46:46 +00:00
|
|
|
const prefSection = prefSections.find(
|
|
|
|
( section ) => section.key === key
|
|
|
|
);
|
2019-05-07 07:21:34 +00:00
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
sections.push( {
|
|
|
|
...defaultSection,
|
|
|
|
...prefSection,
|
2019-05-07 07:21:34 +00:00
|
|
|
} );
|
2020-06-10 16:46:46 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
return sections;
|
2020-06-22 21:25:01 +00:00
|
|
|
};
|
2019-05-07 07:21:34 +00:00
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
const CustomizableDashboard = ( {
|
|
|
|
defaultDateRange,
|
2020-06-18 14:56:30 +00:00
|
|
|
homepageEnabled,
|
2020-06-10 16:46:46 +00:00
|
|
|
path,
|
|
|
|
query,
|
|
|
|
taskListComplete,
|
|
|
|
taskListHidden,
|
|
|
|
} ) => {
|
2020-06-22 21:25:01 +00:00
|
|
|
const { updateUserPreferences, ...userPrefs } = useUserPreferences();
|
2019-05-07 07:21:34 +00:00
|
|
|
|
2020-06-22 21:25:01 +00:00
|
|
|
const sections = mergeSectionsWithDefaults( userPrefs.dashboard_sections );
|
2020-06-10 16:46:46 +00:00
|
|
|
|
2020-06-22 21:25:01 +00:00
|
|
|
const isTaskListEnabled =
|
|
|
|
! homepageEnabled && isOnboardingEnabled() && ! taskListHidden;
|
2020-06-10 16:46:46 +00:00
|
|
|
|
|
|
|
const isDashboardShown =
|
|
|
|
! isTaskListEnabled || ( ! query.task && taskListComplete );
|
|
|
|
|
|
|
|
const updateSections = ( newSections ) => {
|
|
|
|
updateUserPreferences( { dashboard_sections: newSections } );
|
2020-06-22 21:25:01 +00:00
|
|
|
};
|
2019-05-07 07:21:34 +00:00
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
const updateSection = ( updatedKey, newSettings ) => {
|
|
|
|
const newSections = sections.map( ( section ) => {
|
2019-05-07 07:21:34 +00:00
|
|
|
if ( section.key === updatedKey ) {
|
|
|
|
return {
|
|
|
|
...section,
|
|
|
|
...newSettings,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return section;
|
|
|
|
} );
|
2020-06-10 16:46:46 +00:00
|
|
|
updateSections( newSections );
|
2020-06-22 21:25:01 +00:00
|
|
|
};
|
2019-05-07 07:21:34 +00:00
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
const onChangeHiddenBlocks = ( updatedKey ) => {
|
2020-02-14 02:23:21 +00:00
|
|
|
return ( updatedHiddenBlocks ) => {
|
2020-06-10 16:46:46 +00:00
|
|
|
updateSection( updatedKey, {
|
2020-02-14 02:23:21 +00:00
|
|
|
hiddenBlocks: updatedHiddenBlocks,
|
|
|
|
} );
|
2019-05-07 07:21:34 +00:00
|
|
|
};
|
2020-06-22 21:25:01 +00:00
|
|
|
};
|
2019-05-02 10:22:34 +00:00
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
const 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 } );
|
2020-06-10 16:46:46 +00:00
|
|
|
updateSection( updatedKey, { title: updatedTitle } );
|
2019-05-02 10:22:34 +00:00
|
|
|
};
|
2020-06-22 21:25:01 +00:00
|
|
|
};
|
2019-05-02 10:22:34 +00:00
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
const toggleVisibility = ( key, onToggle ) => {
|
2019-04-30 00:35:37 +00:00
|
|
|
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.
|
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
|
|
|
}
|
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
updateSections( sections );
|
2019-04-30 00:35:37 +00:00
|
|
|
};
|
2020-06-22 21:25:01 +00:00
|
|
|
};
|
2019-04-30 00:35:37 +00:00
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
const onMove = ( index, change ) => {
|
2019-04-30 00:35:37 +00:00
|
|
|
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 );
|
2020-06-10 16:46:46 +00:00
|
|
|
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.
|
2020-06-10 16:46:46 +00:00
|
|
|
onMove( index, change + change );
|
2019-05-09 01:13:14 +00:00
|
|
|
}
|
2020-06-22 21:25:01 +00:00
|
|
|
};
|
2019-04-30 00:35:37 +00:00
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
const renderAddMore = () => {
|
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 } ) => (
|
2020-05-29 02:32:37 +00:00
|
|
|
<Button
|
2019-04-30 00:35:37 +00:00
|
|
|
onClick={ onToggle }
|
|
|
|
title={ __( 'Add more sections', 'woocommerce-admin' ) }
|
|
|
|
aria-expanded={ isOpen }
|
2020-05-29 02:32:37 +00:00
|
|
|
>
|
|
|
|
<WPIcon icon={ plusCircleFilled } />
|
|
|
|
</Button>
|
2019-04-30 00:35:37 +00:00
|
|
|
) }
|
|
|
|
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-06-10 16:46:46 +00:00
|
|
|
onClick={ toggleVisibility(
|
2020-02-14 02:23:21 +00:00
|
|
|
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-06-22 21:25:01 +00:00
|
|
|
};
|
2019-04-30 00:35:37 +00:00
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
const renderDashboardReports = () => {
|
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-06-10 16:46:46 +00:00
|
|
|
onChangeHiddenBlocks={ onChangeHiddenBlocks(
|
2020-02-14 02:23:21 +00:00
|
|
|
section.key
|
|
|
|
) }
|
2020-06-10 16:46:46 +00:00
|
|
|
onTitleUpdate={ onSectionTitleUpdate(
|
2020-02-14 02:23:21 +00:00
|
|
|
section.key
|
|
|
|
) }
|
2019-05-09 01:13:14 +00:00
|
|
|
path={ path }
|
|
|
|
query={ query }
|
|
|
|
title={ section.title }
|
2020-06-10 16:46:46 +00:00
|
|
|
onMove={ partial( onMove, index ) }
|
2020-06-22 21:25:01 +00:00
|
|
|
onRemove={ toggleVisibility( section.key ) }
|
2020-02-14 02:23:21 +00:00
|
|
|
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
|
|
|
} ) }
|
2020-06-10 16:46:46 +00:00
|
|
|
{ renderAddMore() }
|
2019-04-22 13:23:37 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
2020-06-22 21:25:01 +00:00
|
|
|
};
|
2020-03-23 20:47:12 +00:00
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
{ isTaskListEnabled && (
|
|
|
|
<Suspense fallback={ <Spinner /> }>
|
2020-06-22 21:25:01 +00:00
|
|
|
<TaskList query={ query } inline={ isDashboardShown } />
|
2020-06-10 16:46:46 +00:00
|
|
|
</Suspense>
|
|
|
|
) }
|
|
|
|
{ isDashboardShown && renderDashboardReports() }
|
|
|
|
</Fragment>
|
|
|
|
);
|
2020-06-22 21:25:01 +00:00
|
|
|
};
|
2019-05-07 07:21:34 +00:00
|
|
|
|
|
|
|
export default compose(
|
2020-03-23 20:47:12 +00:00
|
|
|
withSelect( ( select ) => {
|
2020-06-10 23:49:27 +00:00
|
|
|
const { getOption } = select( OPTIONS_STORE_NAME );
|
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 = {
|
2020-03-25 03:20:17 +00:00
|
|
|
defaultDateRange,
|
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-06-10 23:49:27 +00:00
|
|
|
withSelectData.homepageEnabled =
|
2020-06-15 02:17:12 +00:00
|
|
|
window.wcAdminFeatures.homescreen &&
|
2020-06-10 23:49:27 +00:00
|
|
|
getOption( 'woocommerce_homescreen_enabled' ) === 'yes';
|
2019-10-16 20:53:45 +00:00
|
|
|
withSelectData.taskListHidden =
|
2020-06-10 23:49:27 +00:00
|
|
|
getOption( 'woocommerce_task_list_hidden' ) === 'yes';
|
2020-06-10 13:33:30 +00:00
|
|
|
withSelectData.taskListComplete =
|
2020-06-10 23:49:27 +00:00
|
|
|
getOption( 'woocommerce_task_list_complete' ) === 'yes';
|
2019-10-16 20:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return withSelectData;
|
2019-05-07 07:21:34 +00:00
|
|
|
} )
|
|
|
|
)( CustomizableDashboard );
|