2021-09-01 21:28:24 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { MenuGroup, MenuItem } from '@wordpress/components';
|
|
|
|
import { check } from '@wordpress/icons';
|
2022-06-16 05:06:08 +00:00
|
|
|
import { Fragment, useEffect } from '@wordpress/element';
|
2021-09-01 21:28:24 +00:00
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
2022-04-20 08:48:53 +00:00
|
|
|
import {
|
|
|
|
ONBOARDING_STORE_NAME,
|
|
|
|
OPTIONS_STORE_NAME,
|
|
|
|
TaskListType,
|
|
|
|
TaskType,
|
2022-04-07 19:56:20 +00:00
|
|
|
WCDataSelector,
|
2022-04-20 08:48:53 +00:00
|
|
|
} from '@woocommerce/data';
|
2021-09-01 21:28:24 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-12-14 16:56:42 +00:00
|
|
|
import { DisplayOption } from '~/activity-panel/display-options';
|
2021-09-01 21:28:24 +00:00
|
|
|
import { Task } from './task';
|
2022-11-03 01:22:36 +00:00
|
|
|
import { TasksPlaceholder } from './placeholder';
|
2021-10-05 18:20:28 +00:00
|
|
|
import './tasks.scss';
|
2022-04-20 08:48:53 +00:00
|
|
|
import { TaskList } from './task-list';
|
2022-04-04 14:54:34 +00:00
|
|
|
import { TaskList as TwoColumnTaskList } from '../two-column-tasks/task-list';
|
2022-04-04 18:38:27 +00:00
|
|
|
import TwoColumnTaskListPlaceholder from '../two-column-tasks/placeholder';
|
2022-03-15 17:40:22 +00:00
|
|
|
import '../two-column-tasks/style.scss';
|
2022-04-04 18:38:27 +00:00
|
|
|
import { getAdminSetting } from '~/utils/admin-settings';
|
2021-09-01 21:28:24 +00:00
|
|
|
|
|
|
|
export type TasksProps = {
|
2021-10-15 19:56:17 +00:00
|
|
|
query: { task?: string };
|
2022-06-14 18:31:50 +00:00
|
|
|
context?: string;
|
2021-09-01 21:28:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const Tasks: React.FC< TasksProps > = ( { query } ) => {
|
|
|
|
const { task } = query;
|
|
|
|
const { hideTaskList } = useDispatch( ONBOARDING_STORE_NAME );
|
|
|
|
const { updateOptions } = useDispatch( OPTIONS_STORE_NAME );
|
|
|
|
|
2022-04-07 19:56:20 +00:00
|
|
|
const { isResolving, taskLists } = useSelect(
|
|
|
|
( select: WCDataSelector ) => {
|
|
|
|
return {
|
|
|
|
isResolving: ! select(
|
|
|
|
ONBOARDING_STORE_NAME
|
|
|
|
).hasFinishedResolution( 'getTaskLists' ),
|
|
|
|
taskLists: select( ONBOARDING_STORE_NAME ).getTaskLists(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
2021-09-01 21:28:24 +00:00
|
|
|
|
|
|
|
const getCurrentTask = () => {
|
|
|
|
if ( ! task ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tasks = taskLists.reduce(
|
2022-04-20 08:48:53 +00:00
|
|
|
( acc: TaskType[], taskList: TaskListType ) => [
|
|
|
|
...acc,
|
|
|
|
...taskList.tasks,
|
|
|
|
],
|
2021-09-01 21:28:24 +00:00
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2022-04-20 08:48:53 +00:00
|
|
|
const currentTask = tasks.find( ( t: TaskType ) => t.id === task );
|
2021-09-01 21:28:24 +00:00
|
|
|
|
|
|
|
if ( ! currentTask ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return currentTask;
|
|
|
|
};
|
|
|
|
|
2022-04-20 08:48:53 +00:00
|
|
|
const toggleTaskList = ( taskList: TaskListType ) => {
|
2022-03-01 12:34:18 +00:00
|
|
|
const { id, eventPrefix, isHidden } = taskList;
|
2021-09-01 21:28:24 +00:00
|
|
|
const newValue = ! isHidden;
|
|
|
|
|
|
|
|
recordEvent(
|
2022-03-01 12:34:18 +00:00
|
|
|
newValue ? `${ eventPrefix }hide` : `${ eventPrefix }show`,
|
2021-09-01 21:28:24 +00:00
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
2021-10-15 19:56:17 +00:00
|
|
|
hideTaskList( id );
|
2021-09-01 21:28:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
// @todo Update this when all task lists have been hidden or completed.
|
|
|
|
const taskListsFinished = false;
|
|
|
|
updateOptions( {
|
|
|
|
woocommerce_task_list_prompt_shown: true,
|
|
|
|
} );
|
|
|
|
}, [ taskLists, isResolving ] );
|
|
|
|
|
|
|
|
const currentTask = getCurrentTask();
|
|
|
|
|
|
|
|
if ( task && ! currentTask ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-08-23 04:07:30 +00:00
|
|
|
if ( currentTask ) {
|
|
|
|
return (
|
|
|
|
<div className="woocommerce-task-dashboard__container">
|
|
|
|
<Task query={ query } task={ currentTask } />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-04 18:38:27 +00:00
|
|
|
const taskListIds = getAdminSetting( 'visibleTaskListIds', [] );
|
2022-07-28 16:16:10 +00:00
|
|
|
const TaskListPlaceholderComponent =
|
|
|
|
taskListIds[ 0 ] === 'setup'
|
|
|
|
? TwoColumnTaskListPlaceholder
|
|
|
|
: TasksPlaceholder;
|
2022-04-04 18:38:27 +00:00
|
|
|
|
2021-09-01 21:28:24 +00:00
|
|
|
if ( isResolving ) {
|
2022-04-04 18:38:27 +00:00
|
|
|
return <TaskListPlaceholderComponent query={ query } />;
|
2021-09-01 21:28:24 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 06:16:39 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{ taskLists
|
2022-06-16 05:06:08 +00:00
|
|
|
.filter(
|
|
|
|
( { id }: TaskListType ) => ! id.endsWith( 'two_column' )
|
2022-04-25 06:16:39 +00:00
|
|
|
)
|
2022-06-14 18:31:50 +00:00
|
|
|
.filter( ( { isVisible }: TaskListType ) => isVisible )
|
2022-04-25 06:16:39 +00:00
|
|
|
.map( ( taskList: TaskListType ) => {
|
2022-06-14 18:31:50 +00:00
|
|
|
const { id, isHidden, isToggleable } = taskList;
|
2022-07-28 16:16:10 +00:00
|
|
|
const TaskListComponent =
|
|
|
|
id === 'setup' ? TwoColumnTaskList : TaskList;
|
2022-04-25 06:16:39 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment key={ id }>
|
|
|
|
<TaskListComponent
|
2022-06-16 05:06:08 +00:00
|
|
|
isExpandable={ false }
|
2022-04-25 06:16:39 +00:00
|
|
|
query={ query }
|
|
|
|
twoColumns={ false }
|
|
|
|
{ ...taskList }
|
|
|
|
/>
|
|
|
|
{ isToggleable && (
|
|
|
|
<DisplayOption>
|
|
|
|
<MenuGroup
|
|
|
|
className="woocommerce-layout__homescreen-display-options"
|
|
|
|
label={ __( 'Display', 'woocommerce' ) }
|
|
|
|
>
|
|
|
|
<MenuItem
|
|
|
|
className="woocommerce-layout__homescreen-extension-tasklist-toggle"
|
|
|
|
icon={
|
|
|
|
isHidden ? undefined : check
|
|
|
|
}
|
|
|
|
isSelected={ ! isHidden }
|
|
|
|
role="menuitemcheckbox"
|
|
|
|
onClick={ () =>
|
|
|
|
toggleTaskList( taskList )
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{ __(
|
|
|
|
'Show things to do next',
|
|
|
|
'woocommerce'
|
|
|
|
) }
|
|
|
|
</MenuItem>
|
|
|
|
</MenuGroup>
|
|
|
|
</DisplayOption>
|
|
|
|
) }
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
</>
|
|
|
|
);
|
2021-09-01 21:28:24 +00:00
|
|
|
};
|