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-02 10:22:34 +00:00
|
|
|
import { applyFilters } from '@wordpress/hooks';
|
2019-04-30 00:35:37 +00:00
|
|
|
import { partial } from 'lodash';
|
|
|
|
import { IconButton, Icon, Dropdown, Button } from '@wordpress/components';
|
2019-04-22 13:23:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
import DashboardCharts from './dashboard-charts';
|
|
|
|
import Leaderboards from './leaderboards';
|
2019-05-02 10:22:34 +00:00
|
|
|
import Section from './section';
|
2019-04-22 13:23:37 +00:00
|
|
|
import { ReportFilters, H } from '@woocommerce/components';
|
|
|
|
import StorePerformance from './store-performance';
|
|
|
|
|
|
|
|
export default class CustomizableDashboard extends Component {
|
2019-05-02 10:22:34 +00:00
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
|
|
|
this.state = {
|
|
|
|
sections: applyFilters( 'woocommerce_dashboard_sections', [
|
|
|
|
{
|
|
|
|
key: 'store-performance',
|
|
|
|
component: StorePerformance,
|
2019-04-30 00:35:37 +00:00
|
|
|
title: __( 'Performance', 'woocommerce-admin' ),
|
|
|
|
isVisible: true,
|
|
|
|
icon: 'arrow-right-alt',
|
2019-05-02 10:22:34 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'charts',
|
|
|
|
component: DashboardCharts,
|
|
|
|
title: __( 'Charts', 'woocommerce-admin' ),
|
2019-04-30 00:35:37 +00:00
|
|
|
isVisible: true,
|
|
|
|
icon: 'chart-bar',
|
2019-05-02 10:22:34 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'leaderboards',
|
|
|
|
component: Leaderboards,
|
|
|
|
title: __( 'Leaderboards', 'woocommerce-admin' ),
|
2019-04-30 00:35:37 +00:00
|
|
|
isVisible: true,
|
|
|
|
icon: 'editor-ol',
|
2019-05-02 10:22:34 +00:00
|
|
|
},
|
|
|
|
] ),
|
|
|
|
};
|
2019-04-30 00:35:37 +00:00
|
|
|
|
|
|
|
this.onMove = this.onMove.bind( this );
|
2019-05-02 10:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onSectionTitleUpdate( updatedKey ) {
|
|
|
|
return updatedTitle => {
|
|
|
|
this.setState( {
|
|
|
|
sections: this.state.sections.map( section => {
|
|
|
|
if ( section.key === updatedKey ) {
|
|
|
|
return {
|
|
|
|
...section,
|
|
|
|
title: updatedTitle,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return section;
|
|
|
|
} ),
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
this.setState( state => {
|
|
|
|
// When toggling visibility, place section at the end of the array.
|
|
|
|
const sections = [ ...state.sections ];
|
|
|
|
const index = sections.findIndex( s => key === s.key );
|
|
|
|
const toggledSection = sections.splice( index, 1 ).shift();
|
|
|
|
toggledSection.isVisible = ! toggledSection.isVisible;
|
|
|
|
sections.push( toggledSection );
|
|
|
|
|
|
|
|
return { sections };
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
onMove( index, change ) {
|
|
|
|
const sections = [ ...this.state.sections ];
|
|
|
|
const movedSection = sections.splice( index, 1 ).shift();
|
|
|
|
sections.splice( index + change, 0, movedSection );
|
|
|
|
|
|
|
|
this.setState( { sections } );
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-30 00:35:37 +00:00
|
|
|
const visibleSections = sections.filter( section => section.isVisible );
|
2019-05-02 10:22:34 +00:00
|
|
|
|
2019-04-22 13:23:37 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<H>{ __( 'Customizable Dashboard', 'woocommerce-admin' ) }</H>
|
|
|
|
<ReportFilters query={ query } path={ path } />
|
2019-04-30 00:35:37 +00:00
|
|
|
{ visibleSections.map( ( section, index ) => {
|
2019-05-02 10:22:34 +00:00
|
|
|
return (
|
|
|
|
<Section
|
|
|
|
component={ section.component }
|
|
|
|
key={ section.key }
|
|
|
|
onTitleUpdate={ this.onSectionTitleUpdate( section.key ) }
|
|
|
|
path={ path }
|
|
|
|
query={ query }
|
|
|
|
title={ section.title }
|
2019-04-30 00:35:37 +00:00
|
|
|
onMove={ partial( this.onMove, index ) }
|
|
|
|
onRemove={ this.toggleVisibility( section.key ) }
|
|
|
|
isFirst={ 0 === index }
|
|
|
|
isLast={ visibleSections.length === index + 1 }
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|