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-09-03 21:45:40 +00:00
|
|
|
import { withDispatch, withSelect } from '@wordpress/data';
|
2020-05-12 23:14:08 +00:00
|
|
|
import classnames from 'classnames';
|
2020-05-21 17:15:08 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2020-10-22 22:13:14 +00:00
|
|
|
import {
|
|
|
|
useUserPreferences,
|
|
|
|
NOTES_STORE_NAME,
|
|
|
|
OPTIONS_STORE_NAME,
|
|
|
|
} from '@woocommerce/data';
|
2020-06-10 23:49:27 +00:00
|
|
|
|
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-06-25 01:08:30 +00:00
|
|
|
import '../dashboard/style.scss';
|
2020-05-21 17:15:08 +00:00
|
|
|
import TaskListPlaceholder from '../task-list/placeholder';
|
2020-10-06 13:13:32 +00:00
|
|
|
import InboxPanel from '../inbox-panel';
|
2020-08-05 00:14:56 +00:00
|
|
|
import { WelcomeModal } from './welcome-modal';
|
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' )
|
|
|
|
);
|
|
|
|
|
2020-08-05 00:14:56 +00:00
|
|
|
export const Layout = ( {
|
2020-08-13 13:23:38 +00:00
|
|
|
isBatchUpdating,
|
2020-08-05 00:14:56 +00:00
|
|
|
query,
|
|
|
|
requestingTaskList,
|
|
|
|
taskListComplete,
|
|
|
|
taskListHidden,
|
|
|
|
shouldShowWelcomeModal,
|
|
|
|
updateOptions,
|
|
|
|
} ) => {
|
2020-10-22 22:13:14 +00:00
|
|
|
const userPrefs = useUserPreferences();
|
|
|
|
const twoColumns =
|
|
|
|
( userPrefs.homepage_layout || 'two_columns' ) === 'two_columns';
|
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();
|
2020-10-22 22:13:14 +00:00
|
|
|
const shouldBeSticky = twoColumns && bottom < window.innerHeight;
|
2020-05-12 23:14:08 +00:00
|
|
|
|
|
|
|
setIsContentSticky( shouldBeSticky );
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
maybeStickContent();
|
|
|
|
window.addEventListener( 'resize', maybeStickContent );
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener( 'resize', maybeStickContent );
|
|
|
|
};
|
|
|
|
}, [] );
|
|
|
|
|
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-08-13 13:23:38 +00:00
|
|
|
if ( isBatchUpdating && ! showInbox ) {
|
2020-06-05 16:28:25 +00:00
|
|
|
setShowInbox( true );
|
|
|
|
}
|
|
|
|
|
2020-05-21 17:15:08 +00:00
|
|
|
const renderColumns = () => {
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
{ showInbox && (
|
2020-06-15 02:17:12 +00:00
|
|
|
<div className="woocommerce-homescreen-column is-inbox">
|
2020-10-16 17:35:38 +00:00
|
|
|
<InboxPanel />
|
2020-05-21 17:15:08 +00:00
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
<div
|
2020-06-15 02:17:12 +00:00
|
|
|
className="woocommerce-homescreen-column"
|
2020-05-21 17:15:08 +00:00
|
|
|
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
|
2020-06-15 02:17:12 +00:00
|
|
|
className={ classnames( 'woocommerce-homescreen', {
|
2020-10-22 22:13:14 +00:00
|
|
|
'two-columns': twoColumns,
|
2020-05-12 23:14:08 +00:00
|
|
|
} ) }
|
|
|
|
>
|
2020-05-21 17:15:08 +00:00
|
|
|
{ isDashboardShown
|
|
|
|
? renderColumns()
|
2020-06-05 16:28:25 +00:00
|
|
|
: isTaskListEnabled && renderTaskList() }
|
2020-08-05 00:14:56 +00:00
|
|
|
{ shouldShowWelcomeModal && (
|
|
|
|
<WelcomeModal
|
|
|
|
onClose={ () => {
|
|
|
|
updateOptions( {
|
2020-09-18 20:19:55 +00:00
|
|
|
woocommerce_task_list_welcome_modal_dismissed:
|
|
|
|
'yes',
|
2020-08-05 00:14:56 +00:00
|
|
|
} );
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) }
|
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,
|
2020-08-05 00:14:56 +00:00
|
|
|
/**
|
|
|
|
* If the welcome modal should display
|
|
|
|
*/
|
|
|
|
shouldShowWelcomeModal: PropTypes.bool,
|
|
|
|
/**
|
|
|
|
* Dispatch an action to update an option
|
|
|
|
*/
|
|
|
|
updateOptions: PropTypes.func.isRequired,
|
2020-05-21 17:15:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default compose(
|
2020-09-03 21:45:40 +00:00
|
|
|
withSelect( ( select ) => {
|
2020-08-13 13:23:38 +00:00
|
|
|
const { isNotesRequesting } = select( NOTES_STORE_NAME );
|
2020-06-10 23:49:27 +00:00
|
|
|
const { getOption, isResolving } = select( OPTIONS_STORE_NAME );
|
2020-05-21 17:15:08 +00:00
|
|
|
|
2020-08-05 00:14:56 +00:00
|
|
|
const welcomeModalDismissed =
|
|
|
|
getOption( 'woocommerce_task_list_welcome_modal_dismissed' ) ===
|
2020-09-18 20:19:55 +00:00
|
|
|
'yes';
|
2020-08-05 00:14:56 +00:00
|
|
|
|
|
|
|
const welcomeModalDismissedIsResolving = isResolving( 'getOption', [
|
|
|
|
'woocommerce_task_list_welcome_modal_dismissed',
|
|
|
|
] );
|
|
|
|
|
|
|
|
const shouldShowWelcomeModal =
|
|
|
|
! welcomeModalDismissedIsResolving && ! welcomeModalDismissed;
|
|
|
|
|
2020-05-21 17:15:08 +00:00
|
|
|
return {
|
2020-08-24 21:51:41 +00:00
|
|
|
isBatchUpdating: isNotesRequesting( 'batchUpdateNotes' ),
|
2020-08-05 00:14:56 +00:00
|
|
|
shouldShowWelcomeModal,
|
2020-08-24 21:51:41 +00:00
|
|
|
taskListComplete:
|
|
|
|
getOption( 'woocommerce_task_list_complete' ) === 'yes',
|
|
|
|
taskListHidden:
|
|
|
|
getOption( 'woocommerce_task_list_hidden' ) === 'yes',
|
|
|
|
requestingTaskList:
|
|
|
|
isResolving( 'getOption', [
|
|
|
|
'woocommerce_task_list_complete',
|
|
|
|
] ) ||
|
|
|
|
isResolving( 'getOption', [ 'woocommerce_task_list_hidden' ] ),
|
2020-05-21 17:15:08 +00:00
|
|
|
};
|
2020-08-05 00:14:56 +00:00
|
|
|
} ),
|
|
|
|
withDispatch( ( dispatch ) => ( {
|
|
|
|
updateOptions: dispatch( OPTIONS_STORE_NAME ).updateOptions,
|
|
|
|
} ) )
|
2020-05-21 17:15:08 +00:00
|
|
|
)( Layout );
|