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-04-04 19:43:15 +00:00
|
|
|
import { Fragment, useEffect } from '@wordpress/element';
|
2021-09-01 21:28:24 +00:00
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
|
|
|
import { ONBOARDING_STORE_NAME, OPTIONS_STORE_NAME } from '@woocommerce/data';
|
|
|
|
import { useExperiment } from '@woocommerce/explat';
|
|
|
|
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-04-04 18:38:27 +00:00
|
|
|
import { TasksPlaceholder, TasksPlaceholderProps } from './placeholder';
|
2021-10-05 18:20:28 +00:00
|
|
|
import './tasks.scss';
|
2022-04-04 18:58:50 +00:00
|
|
|
import { TaskListProps, 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 };
|
2021-09-01 21:28:24 +00:00
|
|
|
};
|
|
|
|
|
2022-04-04 14:54:34 +00:00
|
|
|
function getTaskListComponent( taskListId: string ): React.FC< TaskListProps > {
|
2022-03-15 17:40:22 +00:00
|
|
|
switch ( taskListId ) {
|
|
|
|
case 'setup_experiment_1':
|
|
|
|
return TwoColumnTaskList;
|
|
|
|
default:
|
|
|
|
return TaskList;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 18:38:27 +00:00
|
|
|
function getTaskListPlaceholderComponent(
|
|
|
|
taskListId: string
|
|
|
|
): React.FC< TasksPlaceholderProps > {
|
|
|
|
switch ( taskListId ) {
|
|
|
|
case 'setup_experiment_1':
|
|
|
|
return TwoColumnTaskListPlaceholder;
|
|
|
|
default:
|
|
|
|
return TasksPlaceholder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 );
|
|
|
|
const [ isLoadingExperiment, experimentAssignment ] = useExperiment(
|
|
|
|
'woocommerce_tasklist_progression'
|
|
|
|
);
|
|
|
|
|
|
|
|
const { isResolving, taskLists } = useSelect( ( select ) => {
|
|
|
|
return {
|
2022-04-04 14:54:34 +00:00
|
|
|
isResolving: ! select(
|
|
|
|
ONBOARDING_STORE_NAME
|
|
|
|
).hasFinishedResolution( 'getTaskLists' ),
|
2021-09-01 21:28:24 +00:00
|
|
|
taskLists: select( ONBOARDING_STORE_NAME ).getTaskLists(),
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
|
|
|
const getCurrentTask = () => {
|
|
|
|
if ( ! task ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tasks = taskLists.reduce(
|
|
|
|
( acc, taskList ) => [ ...acc, ...taskList.tasks ],
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
const currentTask = tasks.find( ( t ) => t.id === task );
|
|
|
|
|
|
|
|
if ( ! currentTask ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return currentTask;
|
|
|
|
};
|
|
|
|
|
|
|
|
const toggleTaskList = ( taskList ) => {
|
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-04-04 18:38:27 +00:00
|
|
|
const taskListIds = getAdminSetting( 'visibleTaskListIds', [] );
|
|
|
|
const TaskListPlaceholderComponent = getTaskListPlaceholderComponent(
|
|
|
|
taskListIds[ 0 ]
|
|
|
|
);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if ( currentTask ) {
|
2021-09-21 19:33:44 +00:00
|
|
|
return (
|
|
|
|
<div className="woocommerce-task-dashboard__container">
|
2021-10-06 18:47:41 +00:00
|
|
|
<Task query={ query } task={ currentTask } />
|
2021-09-21 19:33:44 +00:00
|
|
|
</div>
|
|
|
|
);
|
2021-09-01 21:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( isLoadingExperiment ) {
|
2022-04-04 18:38:27 +00:00
|
|
|
return <TaskListPlaceholderComponent query={ query } />;
|
2021-09-01 21:28:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 12:34:18 +00:00
|
|
|
return taskLists
|
|
|
|
.filter( ( { id } ) =>
|
|
|
|
experimentAssignment?.variationName === 'treatment'
|
|
|
|
? id.endsWith( 'two_column' )
|
|
|
|
: ! id.endsWith( 'two_column' )
|
|
|
|
)
|
|
|
|
.map( ( taskList ) => {
|
2022-04-04 14:54:34 +00:00
|
|
|
const { id, isHidden, isVisible, isToggleable } = taskList;
|
2022-03-01 12:34:18 +00:00
|
|
|
|
|
|
|
if ( ! isVisible ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-03-15 17:40:22 +00:00
|
|
|
const TaskListComponent = getTaskListComponent( id );
|
|
|
|
|
2022-03-01 12:34:18 +00:00
|
|
|
return (
|
|
|
|
<Fragment key={ id }>
|
2022-04-04 19:43:15 +00:00
|
|
|
<TaskListComponent
|
|
|
|
isExpandable={
|
|
|
|
experimentAssignment?.variationName === 'treatment'
|
|
|
|
}
|
|
|
|
query={ query }
|
|
|
|
twoColumns={ false }
|
|
|
|
{ ...taskList }
|
|
|
|
/>
|
2022-03-01 12:34:18 +00:00
|
|
|
{ isToggleable && (
|
|
|
|
<DisplayOption>
|
|
|
|
<MenuGroup
|
|
|
|
className="woocommerce-layout__homescreen-display-options"
|
2022-03-30 09:00:04 +00:00
|
|
|
label={ __( 'Display', 'woocommerce' ) }
|
2021-09-01 21:28:24 +00:00
|
|
|
>
|
2022-03-01 12:34:18 +00:00
|
|
|
<MenuItem
|
|
|
|
className="woocommerce-layout__homescreen-extension-tasklist-toggle"
|
|
|
|
icon={ ! isHidden && check }
|
|
|
|
isSelected={ ! isHidden }
|
|
|
|
role="menuitemcheckbox"
|
|
|
|
onClick={ () => toggleTaskList( taskList ) }
|
|
|
|
>
|
|
|
|
{ __(
|
|
|
|
'Show things to do next',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2022-03-01 12:34:18 +00:00
|
|
|
) }
|
|
|
|
</MenuItem>
|
|
|
|
</MenuGroup>
|
|
|
|
</DisplayOption>
|
|
|
|
) }
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
} );
|
2021-09-01 21:28:24 +00:00
|
|
|
};
|