2018-06-28 13:52:45 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-06-29 15:20:08 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import classnames from 'classnames';
|
2018-07-06 12:40:05 +00:00
|
|
|
import clickOutside from 'react-click-outside';
|
|
|
|
import { Component } from '@wordpress/element';
|
2018-06-29 15:20:08 +00:00
|
|
|
import Gridicon from 'gridicons';
|
2018-07-13 20:28:01 +00:00
|
|
|
import { IconButton, NavigableMenu } from '@wordpress/components';
|
|
|
|
import { partial, uniqueId, find } from 'lodash';
|
2018-06-28 13:52:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2018-06-29 15:20:08 +00:00
|
|
|
import './style.scss';
|
2018-07-09 15:46:31 +00:00
|
|
|
import ActivityPanelToggleBubble from './toggle-bubble';
|
2018-09-21 15:19:05 +00:00
|
|
|
import { H, Section } from '@woocommerce/components';
|
2018-10-12 19:20:48 +00:00
|
|
|
import InboxPanel from './panels/inbox';
|
2018-07-16 18:37:25 +00:00
|
|
|
import OrdersPanel from './panels/orders';
|
|
|
|
import StockPanel from './panels/stock';
|
|
|
|
import ReviewsPanel from './panels/reviews';
|
2018-06-29 15:20:08 +00:00
|
|
|
import WordPressNotices from './wordpress-notices';
|
2018-06-28 13:52:45 +00:00
|
|
|
|
|
|
|
class ActivityPanel extends Component {
|
|
|
|
constructor() {
|
|
|
|
super( ...arguments );
|
|
|
|
this.togglePanel = this.togglePanel.bind( this );
|
2018-06-29 15:20:08 +00:00
|
|
|
this.toggleMobile = this.toggleMobile.bind( this );
|
|
|
|
this.renderTab = this.renderTab.bind( this );
|
2018-07-18 15:20:00 +00:00
|
|
|
this.updateNoticeFlag = this.updateNoticeFlag.bind( this );
|
2018-06-28 13:52:45 +00:00
|
|
|
this.state = {
|
2018-06-29 15:20:08 +00:00
|
|
|
isPanelOpen: false,
|
|
|
|
mobileOpen: false,
|
2018-06-28 13:52:45 +00:00
|
|
|
currentTab: '',
|
2018-07-18 15:20:00 +00:00
|
|
|
hasWordPressNotices: false,
|
2018-06-28 13:52:45 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
togglePanel( tabName ) {
|
2018-06-29 15:20:08 +00:00
|
|
|
// The WordPress Notices tab is handled differently, since they are displayed inline, so the panel should be closed,
|
|
|
|
// Close behavior of the expanded notices is based on current tab.
|
|
|
|
if ( 'wpnotices' === tabName ) {
|
2018-06-28 13:52:45 +00:00
|
|
|
this.setState( state => ( {
|
2018-06-29 15:20:08 +00:00
|
|
|
currentTab: 'wpnotices' === state.currentTab ? '' : tabName,
|
|
|
|
mobileOpen: 'wpnotices' !== state.currentTab,
|
|
|
|
isPanelOpen: false,
|
2018-06-28 13:52:45 +00:00
|
|
|
} ) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-29 15:20:08 +00:00
|
|
|
this.setState( state => {
|
|
|
|
if (
|
|
|
|
tabName === state.currentTab ||
|
|
|
|
'' === state.currentTab ||
|
|
|
|
'wpnotices' === state.currentTab
|
|
|
|
) {
|
|
|
|
return {
|
|
|
|
isPanelOpen: ! state.isPanelOpen,
|
|
|
|
currentTab: state.isPanelOpen ? '' : tabName,
|
|
|
|
mobileOpen: ! state.isPanelOpen,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return { currentTab: tabName };
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
// On smaller screen, the panel buttons are hidden behind a toggle.
|
|
|
|
toggleMobile() {
|
2018-09-05 18:52:48 +00:00
|
|
|
const tabs = this.getTabs();
|
2018-06-29 15:20:08 +00:00
|
|
|
this.setState( state => ( {
|
|
|
|
mobileOpen: ! state.mobileOpen,
|
2018-09-05 18:52:48 +00:00
|
|
|
currentTab: state.mobileOpen ? '' : tabs[ 0 ].name,
|
2018-06-29 15:20:08 +00:00
|
|
|
isPanelOpen: ! state.mobileOpen,
|
|
|
|
} ) );
|
2018-06-28 13:52:45 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 12:40:05 +00:00
|
|
|
handleClickOutside() {
|
|
|
|
const { isPanelOpen, currentTab } = this.state;
|
|
|
|
|
|
|
|
if ( isPanelOpen ) {
|
|
|
|
this.togglePanel( currentTab );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 15:20:00 +00:00
|
|
|
updateNoticeFlag( noticeCount ) {
|
|
|
|
this.setState( {
|
|
|
|
hasWordPressNotices: noticeCount > 0,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2018-07-06 12:40:05 +00:00
|
|
|
// TODO Pull in dynamic unread status/count
|
2018-06-28 13:52:45 +00:00
|
|
|
getTabs() {
|
|
|
|
return [
|
2018-10-12 19:20:48 +00:00
|
|
|
{
|
|
|
|
name: 'inbox',
|
|
|
|
title: __( 'Inbox', 'wc-admin' ),
|
|
|
|
icon: <Gridicon icon="mail" />,
|
|
|
|
unread: true,
|
|
|
|
},
|
2018-06-28 13:52:45 +00:00
|
|
|
{
|
2018-06-29 15:20:08 +00:00
|
|
|
name: 'orders',
|
2018-07-10 12:48:06 +00:00
|
|
|
title: __( 'Orders', 'wc-admin' ),
|
2018-06-29 15:20:08 +00:00
|
|
|
icon: <Gridicon icon="pages" />,
|
2018-09-05 18:52:48 +00:00
|
|
|
unread: true,
|
2018-06-28 13:52:45 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'stock',
|
2018-07-10 12:48:06 +00:00
|
|
|
title: __( 'Stock', 'wc-admin' ),
|
2018-06-29 15:20:08 +00:00
|
|
|
icon: <Gridicon icon="clipboard" />,
|
2018-07-18 15:20:00 +00:00
|
|
|
unread: false,
|
2018-06-29 15:20:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'reviews',
|
2018-07-10 12:48:06 +00:00
|
|
|
title: __( 'Reviews', 'wc-admin' ),
|
2018-06-29 15:20:08 +00:00
|
|
|
icon: <Gridicon icon="star" />,
|
2018-07-18 15:20:00 +00:00
|
|
|
unread: false,
|
2018-06-28 13:52:45 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-07-06 12:40:05 +00:00
|
|
|
getPanelContent( tab ) {
|
|
|
|
switch ( tab ) {
|
2018-10-12 19:20:48 +00:00
|
|
|
case 'inbox':
|
|
|
|
return <InboxPanel />;
|
2018-06-28 13:52:45 +00:00
|
|
|
case 'orders':
|
2018-07-09 15:46:31 +00:00
|
|
|
return <OrdersPanel />;
|
|
|
|
case 'stock':
|
|
|
|
return <StockPanel />;
|
|
|
|
case 'reviews':
|
|
|
|
return <ReviewsPanel />;
|
2018-06-28 13:52:45 +00:00
|
|
|
default:
|
2018-07-09 15:46:31 +00:00
|
|
|
return null;
|
2018-06-28 13:52:45 +00:00
|
|
|
}
|
2018-07-06 12:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderPanel() {
|
|
|
|
const { isPanelOpen, currentTab } = this.state;
|
2018-06-29 15:20:08 +00:00
|
|
|
|
2018-07-13 20:28:01 +00:00
|
|
|
const tab = find( this.getTabs(), { name: currentTab } );
|
|
|
|
if ( ! tab ) {
|
2018-07-17 17:07:36 +00:00
|
|
|
return <div className="woocommerce-layout__activity-panel-wrapper" />;
|
2018-07-13 20:28:01 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 17:07:36 +00:00
|
|
|
const classNames = classnames( 'woocommerce-layout__activity-panel-wrapper', {
|
|
|
|
'is-open': isPanelOpen,
|
|
|
|
} );
|
|
|
|
|
2018-06-29 15:20:08 +00:00
|
|
|
return (
|
2018-07-13 20:28:01 +00:00
|
|
|
<div className={ classNames } tabIndex={ 0 } role="tabpanel" aria-label={ tab.title }>
|
2018-07-06 12:40:05 +00:00
|
|
|
{ ( isPanelOpen && (
|
|
|
|
<div
|
|
|
|
className="woocommerce-layout__activity-panel-content"
|
|
|
|
key={ 'activity-panel-' + currentTab }
|
|
|
|
id={ 'activity-panel-' + currentTab }
|
|
|
|
>
|
|
|
|
{ this.getPanelContent( currentTab ) }
|
|
|
|
</div>
|
|
|
|
) ) ||
|
|
|
|
null }
|
2018-07-13 19:31:58 +00:00
|
|
|
</div>
|
2018-06-29 15:20:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-07-13 20:28:01 +00:00
|
|
|
renderTab( tab, i ) {
|
|
|
|
const { currentTab, isPanelOpen } = this.state;
|
2018-06-29 15:20:08 +00:00
|
|
|
const className = classnames( 'woocommerce-layout__activity-panel-tab', {
|
|
|
|
'is-active': tab.name === currentTab,
|
2018-07-06 12:40:05 +00:00
|
|
|
'has-unread': tab.unread,
|
2018-06-29 15:20:08 +00:00
|
|
|
} );
|
|
|
|
|
2018-07-13 20:28:01 +00:00
|
|
|
const selected = tab.name === currentTab;
|
|
|
|
let tabIndex = -1;
|
|
|
|
|
2018-09-05 18:52:48 +00:00
|
|
|
// Only make this item tabbable if it is the currently selected item, or the panel is closed and the item is the first item
|
2018-07-13 20:28:01 +00:00
|
|
|
// If wpnotices is currently selected, tabindex below should be -1 and <WordPressNotices /> will become the tabbed element.
|
|
|
|
if ( selected || ( ! isPanelOpen && i === 0 && 'wpnotices' !== currentTab ) ) {
|
|
|
|
tabIndex = null;
|
|
|
|
}
|
|
|
|
|
2018-06-29 15:20:08 +00:00
|
|
|
return (
|
|
|
|
<IconButton
|
2018-07-13 20:28:01 +00:00
|
|
|
role="tab"
|
2018-06-29 15:20:08 +00:00
|
|
|
className={ className }
|
2018-07-13 20:28:01 +00:00
|
|
|
tabIndex={ tabIndex }
|
|
|
|
aria-selected={ selected }
|
|
|
|
aria-controls={ 'activity-panel-' + tab.name }
|
|
|
|
key={ 'activity-panel-tab-' + tab.name }
|
|
|
|
id={ 'activity-panel-tab-' + tab.name }
|
2018-06-29 15:20:08 +00:00
|
|
|
onClick={ partial( this.togglePanel, tab.name ) }
|
|
|
|
icon={ tab.icon }
|
|
|
|
>
|
2018-07-18 15:20:00 +00:00
|
|
|
{ tab.title }{' '}
|
|
|
|
{ tab.unread && (
|
|
|
|
<span className="screen-reader-text">{ __( 'unread activity', 'wc-admin' ) }</span>
|
|
|
|
) }
|
2018-06-29 15:20:08 +00:00
|
|
|
</IconButton>
|
|
|
|
);
|
2018-06-28 13:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const tabs = this.getTabs();
|
2018-07-18 15:20:00 +00:00
|
|
|
const { currentTab, mobileOpen, hasWordPressNotices } = this.state;
|
2018-07-13 19:31:58 +00:00
|
|
|
const headerId = uniqueId( 'activity-panel-header_' );
|
2018-06-29 15:20:08 +00:00
|
|
|
const panelClasses = classnames( 'woocommerce-layout__activity-panel', {
|
|
|
|
'is-mobile-open': this.state.mobileOpen,
|
|
|
|
} );
|
2018-06-28 13:52:45 +00:00
|
|
|
|
2018-07-18 15:20:00 +00:00
|
|
|
const hasUnread = hasWordPressNotices || tabs.some( tab => tab.unread );
|
|
|
|
const viewLabel = hasUnread
|
|
|
|
? __( 'View Activity Panel, you have unread activity', 'wc-admin' )
|
|
|
|
: __( 'View Activity Panel', 'wc-admin' );
|
|
|
|
|
2018-06-28 13:52:45 +00:00
|
|
|
return (
|
2018-07-13 19:31:58 +00:00
|
|
|
<div>
|
|
|
|
<H id={ headerId } className="screen-reader-text">
|
|
|
|
{ __( 'Store Activity', 'wc-admin' ) }
|
|
|
|
</H>
|
|
|
|
<Section component="aside" id="woocommerce-activity-panel" aria-labelledby={ headerId }>
|
|
|
|
<IconButton
|
|
|
|
onClick={ this.toggleMobile }
|
2018-07-18 15:20:00 +00:00
|
|
|
icon={
|
|
|
|
mobileOpen ? (
|
|
|
|
<Gridicon icon="cross-small" />
|
|
|
|
) : (
|
|
|
|
<ActivityPanelToggleBubble hasUnread={ hasUnread } />
|
|
|
|
)
|
2018-07-13 19:31:58 +00:00
|
|
|
}
|
2018-07-18 15:20:00 +00:00
|
|
|
label={ mobileOpen ? __( 'Close Activity Panel', 'wc-admin' ) : viewLabel }
|
2018-07-13 19:31:58 +00:00
|
|
|
aria-expanded={ mobileOpen }
|
|
|
|
tooltip={ false }
|
|
|
|
className="woocommerce-layout__activity-panel-mobile-toggle"
|
|
|
|
/>
|
|
|
|
<div className={ panelClasses }>
|
2018-07-13 20:28:01 +00:00
|
|
|
<NavigableMenu
|
|
|
|
role="tablist"
|
|
|
|
orientation="horizontal"
|
|
|
|
className="woocommerce-layout__activity-panel-tabs"
|
|
|
|
>
|
2018-07-13 19:31:58 +00:00
|
|
|
{ tabs && tabs.map( this.renderTab ) }
|
|
|
|
<WordPressNotices
|
|
|
|
showNotices={ 'wpnotices' === currentTab }
|
|
|
|
togglePanel={ this.togglePanel }
|
2018-07-18 15:20:00 +00:00
|
|
|
onCountUpdate={ this.updateNoticeFlag }
|
2018-07-13 19:31:58 +00:00
|
|
|
/>
|
2018-07-13 20:28:01 +00:00
|
|
|
</NavigableMenu>
|
2018-07-13 19:31:58 +00:00
|
|
|
{ this.renderPanel() }
|
2018-06-29 15:20:08 +00:00
|
|
|
</div>
|
2018-07-13 19:31:58 +00:00
|
|
|
</Section>
|
2018-07-06 12:40:05 +00:00
|
|
|
</div>
|
2018-06-28 13:52:45 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 12:40:05 +00:00
|
|
|
export default clickOutside( ActivityPanel );
|