2020-05-12 23:14:08 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-05-21 17:15:08 +00:00
|
|
|
import {
|
|
|
|
Fragment,
|
|
|
|
Suspense,
|
|
|
|
lazy,
|
|
|
|
useState,
|
|
|
|
useRef,
|
|
|
|
useEffect,
|
|
|
|
} from '@wordpress/element';
|
|
|
|
import { compose } from '@wordpress/compose';
|
2020-05-12 23:14:08 +00:00
|
|
|
import classnames from 'classnames';
|
2020-05-21 17:15:08 +00:00
|
|
|
import { get } from 'lodash';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
2020-05-12 23:14:08 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-05-19 13:16:19 +00:00
|
|
|
import QuickLinks from '../quick-links';
|
2020-05-12 23:14:08 +00:00
|
|
|
import StatsOverview from './stats-overview';
|
|
|
|
import './style.scss';
|
2020-05-21 17:15:08 +00:00
|
|
|
import { isOnboardingEnabled } from 'dashboard/utils';
|
|
|
|
import withSelect from 'wc-api/with-select';
|
|
|
|
import TaskListPlaceholder from '../task-list/placeholder';
|
2020-06-05 16:28:25 +00:00
|
|
|
import InboxPanel from '../header/activity-panel/panels/inbox';
|
2020-05-12 23:14:08 +00:00
|
|
|
|
2020-05-21 17:15:08 +00:00
|
|
|
const TaskList = lazy( () =>
|
|
|
|
import( /* webpackChunkName: "task-list" */ '../task-list' )
|
|
|
|
);
|
|
|
|
|
|
|
|
export const Layout = ( props ) => {
|
2020-05-12 23:14:08 +00:00
|
|
|
const [ showInbox, setShowInbox ] = useState( true );
|
|
|
|
const [ isContentSticky, setIsContentSticky ] = useState( false );
|
|
|
|
const content = useRef( null );
|
|
|
|
const maybeStickContent = () => {
|
|
|
|
if ( ! content.current ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const { bottom } = content.current.getBoundingClientRect();
|
|
|
|
const shouldBeSticky = showInbox && bottom < window.innerHeight;
|
|
|
|
|
|
|
|
setIsContentSticky( shouldBeSticky );
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
maybeStickContent();
|
|
|
|
window.addEventListener( 'resize', maybeStickContent );
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener( 'resize', maybeStickContent );
|
|
|
|
};
|
|
|
|
}, [] );
|
|
|
|
|
2020-06-05 16:28:25 +00:00
|
|
|
const {
|
|
|
|
isUndoRequesting,
|
|
|
|
query,
|
|
|
|
requestingTaskList,
|
|
|
|
taskListComplete,
|
|
|
|
taskListHidden,
|
|
|
|
} = props;
|
2020-05-27 16:08:39 +00:00
|
|
|
const isTaskListEnabled = taskListHidden === false && ! taskListComplete;
|
2020-05-21 17:15:08 +00:00
|
|
|
const isDashboardShown = ! isTaskListEnabled || ! query.task;
|
|
|
|
|
2020-06-05 16:28:25 +00:00
|
|
|
const isInboxPanelEmpty = ( isEmpty ) => {
|
|
|
|
setShowInbox( ! isEmpty );
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( isUndoRequesting && ! showInbox ) {
|
|
|
|
setShowInbox( true );
|
|
|
|
}
|
|
|
|
|
2020-05-21 17:15:08 +00:00
|
|
|
const renderColumns = () => {
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
{ showInbox && (
|
|
|
|
<div className="woocommerce-homepage-column is-inbox">
|
2020-06-05 16:28:25 +00:00
|
|
|
<InboxPanel isPanelEmpty={ isInboxPanelEmpty } />
|
2020-05-21 17:15:08 +00:00
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
<div
|
|
|
|
className="woocommerce-homepage-column"
|
|
|
|
ref={ content }
|
|
|
|
style={ {
|
|
|
|
position: isContentSticky ? 'sticky' : 'static',
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
{ isTaskListEnabled && renderTaskList() }
|
|
|
|
<StatsOverview />
|
2020-06-08 18:35:37 +00:00
|
|
|
{ ! isTaskListEnabled && <QuickLinks /> }
|
2020-05-21 17:15:08 +00:00
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderTaskList = () => {
|
|
|
|
if ( requestingTaskList ) {
|
|
|
|
return <TaskListPlaceholder />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-05-27 16:08:39 +00:00
|
|
|
<Suspense fallback={ <TaskListPlaceholder /> }>
|
|
|
|
<TaskList query={ query } />
|
2020-05-21 17:15:08 +00:00
|
|
|
</Suspense>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-05-12 23:14:08 +00:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={ classnames( 'woocommerce-homepage', {
|
|
|
|
hasInbox: showInbox,
|
|
|
|
} ) }
|
|
|
|
>
|
2020-05-21 17:15:08 +00:00
|
|
|
{ isDashboardShown
|
|
|
|
? renderColumns()
|
2020-06-05 16:28:25 +00:00
|
|
|
: isTaskListEnabled && renderTaskList() }
|
2020-05-12 23:14:08 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-05-21 17:15:08 +00:00
|
|
|
Layout.propTypes = {
|
|
|
|
/**
|
|
|
|
* If the task list option is being fetched.
|
|
|
|
*/
|
|
|
|
requestingTaskList: PropTypes.bool.isRequired,
|
2020-05-27 16:08:39 +00:00
|
|
|
/**
|
|
|
|
* If the task list has been completed.
|
|
|
|
*/
|
|
|
|
taskListComplete: PropTypes.bool,
|
2020-05-21 17:15:08 +00:00
|
|
|
/**
|
|
|
|
* If the task list is hidden.
|
|
|
|
*/
|
|
|
|
taskListHidden: PropTypes.bool,
|
|
|
|
/**
|
|
|
|
* Page query, used to determine the current task if any.
|
|
|
|
*/
|
|
|
|
query: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( ( select ) => {
|
|
|
|
const {
|
|
|
|
getOptions,
|
2020-06-05 16:28:25 +00:00
|
|
|
getUndoDismissRequesting,
|
2020-05-21 17:15:08 +00:00
|
|
|
isGetOptionsRequesting,
|
|
|
|
} = select( 'wc-api' );
|
|
|
|
|
|
|
|
if ( isOnboardingEnabled() ) {
|
|
|
|
const options = getOptions( [
|
2020-05-27 16:08:39 +00:00
|
|
|
'woocommerce_task_list_complete',
|
2020-05-21 17:15:08 +00:00
|
|
|
'woocommerce_task_list_hidden',
|
|
|
|
] );
|
2020-06-05 16:28:25 +00:00
|
|
|
const { isUndoRequesting } = getUndoDismissRequesting();
|
2020-06-08 18:35:37 +00:00
|
|
|
|
2020-05-21 17:15:08 +00:00
|
|
|
return {
|
2020-06-05 16:28:25 +00:00
|
|
|
isUndoRequesting,
|
2020-05-21 17:15:08 +00:00
|
|
|
requestingTaskList: isGetOptionsRequesting( [
|
2020-05-27 16:08:39 +00:00
|
|
|
'woocommerce_task_list_complete',
|
2020-05-21 17:15:08 +00:00
|
|
|
'woocommerce_task_list_hidden',
|
|
|
|
] ),
|
2020-06-10 13:33:30 +00:00
|
|
|
taskListComplete:
|
|
|
|
get( options, [ 'woocommerce_task_list_complete' ] ) ===
|
|
|
|
'yes',
|
2020-06-05 16:28:25 +00:00
|
|
|
taskListHidden:
|
|
|
|
get( options, [ 'woocommerce_task_list_hidden' ] ) ===
|
|
|
|
'yes',
|
2020-05-21 17:15:08 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
requestingTaskList: false,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( Layout );
|