2019-04-22 13:23:37 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* 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-04-30 00:35:37 +00:00
|
|
|
import { partial } from 'lodash';
|
|
|
|
import { IconButton, Icon, Dropdown, Button } from '@wordpress/components';
|
2019-05-07 07:21:34 +00:00
|
|
|
import { withDispatch } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { H, ReportFilters } 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-04-22 13:23:37 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
const prefSection = prefSections.find( section => section.key === key );
|
|
|
|
|
|
|
|
sections.push( {
|
|
|
|
...defaultSection,
|
|
|
|
...prefSection,
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
return sections;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSections( newSections ) {
|
|
|
|
this.setState( { sections: newSections } );
|
|
|
|
this.props.updateCurrentUserData( { dashboard_sections: newSections } );
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSection( updatedKey, newSettings ) {
|
|
|
|
const newSections = this.state.sections.map( section => {
|
|
|
|
if ( section.key === updatedKey ) {
|
|
|
|
return {
|
|
|
|
...section,
|
|
|
|
...newSettings,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return section;
|
|
|
|
} );
|
|
|
|
this.updateSections( newSections );
|
|
|
|
}
|
|
|
|
|
|
|
|
onChangeHiddenBlocks( updatedKey ) {
|
|
|
|
return updatedHiddenBlocks => {
|
|
|
|
this.updateSection( updatedKey, { hiddenBlocks: updatedHiddenBlocks } );
|
|
|
|
};
|
2019-05-02 10:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onSectionTitleUpdate( updatedKey ) {
|
|
|
|
return updatedTitle => {
|
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 ];
|
|
|
|
const index = sections.findIndex( s => key === s.key );
|
|
|
|
const toggledSection = sections.splice( index, 1 ).shift();
|
|
|
|
toggledSection.isVisible = ! toggledSection.isVisible;
|
|
|
|
sections.push( toggledSection );
|
|
|
|
|
2019-07-01 10:16:12 +00:00
|
|
|
if ( ! toggledSection.isVisible ) {
|
|
|
|
recordEvent( 'dash_section_remove', { key: toggledSection.key } );
|
|
|
|
}
|
|
|
|
|
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 );
|
|
|
|
} 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;
|
|
|
|
const hiddenSections = sections.filter( section => false === section.isVisible );
|
|
|
|
|
|
|
|
if ( 0 === hiddenSections.length ) {
|
|
|
|
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>
|
|
|
|
<H>{ __( 'Dashboard Sections', 'woocommerce-admin' ) }</H>
|
|
|
|
<div className="woocommerce-dashboard-section__add-more-choices">
|
|
|
|
{ hiddenSections.map( section => {
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
key={ section.key }
|
|
|
|
onClick={ this.toggleVisibility( section.key, onToggle ) }
|
|
|
|
className="woocommerce-dashboard-section__add-more-btn"
|
|
|
|
title={ sprintf( __( 'Add %s section', 'woocommerce-admin' ), section.title ) }
|
|
|
|
>
|
|
|
|
<Icon icon={ section.icon } size={ 30 } />
|
|
|
|
<span className="woocommerce-dashboard-section__add-more-btn-title">
|
|
|
|
{ section.title }
|
|
|
|
</span>
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-22 13:23:37 +00:00
|
|
|
render() {
|
|
|
|
const { query, path } = this.props;
|
2019-05-02 10:22:34 +00:00
|
|
|
const { sections } = this.state;
|
2019-05-09 01:13:14 +00:00
|
|
|
const visibleSectionKeys = sections
|
|
|
|
.filter( section => section.isVisible )
|
|
|
|
.map( section => section.key );
|
2019-05-02 10:22:34 +00:00
|
|
|
|
2019-04-22 13:23:37 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<ReportFilters query={ query } path={ path } />
|
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 }
|
|
|
|
onChangeHiddenBlocks={ this.onChangeHiddenBlocks( section.key ) }
|
|
|
|
onTitleUpdate={ this.onSectionTitleUpdate( section.key ) }
|
|
|
|
path={ path }
|
|
|
|
query={ query }
|
|
|
|
title={ section.title }
|
|
|
|
onMove={ partial( this.onMove, index ) }
|
|
|
|
onRemove={ this.toggleVisibility( section.key ) }
|
|
|
|
isFirst={ section.key === visibleSectionKeys[ 0 ] }
|
|
|
|
isLast={ section.key === visibleSectionKeys[ visibleSectionKeys.length - 1 ] }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
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(
|
|
|
|
withSelect( select => {
|
|
|
|
const { getCurrentUserData } = select( 'wc-api' );
|
|
|
|
const userData = getCurrentUserData();
|
|
|
|
|
|
|
|
return {
|
|
|
|
userPrefSections: userData.dashboard_sections,
|
|
|
|
};
|
|
|
|
} ),
|
|
|
|
withDispatch( dispatch => {
|
|
|
|
const { updateCurrentUserData } = dispatch( 'wc-api' );
|
|
|
|
|
|
|
|
return {
|
|
|
|
updateCurrentUserData,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( CustomizableDashboard );
|