2021-10-20 16:16:22 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2023-04-25 22:17:33 +00:00
|
|
|
import { useEffect, useRef, useState, createElement } from '@wordpress/element';
|
2021-10-26 08:40:35 +00:00
|
|
|
import { Button, Card } from '@wordpress/components';
|
2021-10-20 16:16:22 +00:00
|
|
|
import { useSelect, useDispatch } from '@wordpress/data';
|
|
|
|
import { EllipsisMenu } from '@woocommerce/components';
|
2022-06-01 17:10:20 +00:00
|
|
|
import { navigateTo, getNewPath } from '@woocommerce/navigation';
|
2023-01-21 07:19:20 +00:00
|
|
|
import { WooOnboardingTaskListHeader } from '@woocommerce/onboarding';
|
2022-03-25 08:06:40 +00:00
|
|
|
import {
|
2022-03-15 17:40:22 +00:00
|
|
|
ONBOARDING_STORE_NAME,
|
|
|
|
TaskType,
|
2022-03-25 08:06:40 +00:00
|
|
|
useUserPreferences,
|
2022-04-13 14:20:26 +00:00
|
|
|
getVisibleTasks,
|
2022-04-20 04:49:27 +00:00
|
|
|
TaskListType,
|
2022-04-07 19:56:20 +00:00
|
|
|
WCDataSelector,
|
2022-03-15 17:40:22 +00:00
|
|
|
} from '@woocommerce/data';
|
2021-10-20 16:16:22 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2023-01-21 03:05:45 +00:00
|
|
|
import { List, useSlot } from '@woocommerce/experimental';
|
2024-05-31 03:49:36 +00:00
|
|
|
import clsx from 'clsx';
|
2023-04-25 22:17:33 +00:00
|
|
|
import { useLayoutContext } from '@woocommerce/admin-layout';
|
2021-10-20 16:16:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-05-17 01:54:31 +00:00
|
|
|
import { taskHeaders } from './components/task-headers';
|
|
|
|
import DismissModal from './components/dismiss-modal';
|
|
|
|
import TaskListCompleted from './components/task-list-completed';
|
|
|
|
import { ProgressHeader } from '~/task-lists/components/progress-header';
|
|
|
|
import { TaskListItem } from './components/task-list-item';
|
|
|
|
import { TaskListCompletedHeader } from './components/task-list-completed-header';
|
2023-04-25 22:17:33 +00:00
|
|
|
|
2023-01-23 05:48:42 +00:00
|
|
|
import { ExperimentalWooTaskListFooter } from './footer-slot';
|
2023-01-21 03:05:45 +00:00
|
|
|
import {
|
|
|
|
TaskListCompletionSlot,
|
|
|
|
EXPERIMENTAL_WC_TASK_LIST_COMPLETION_SLOT_NAME,
|
|
|
|
} from './task-list-completion-slot';
|
2022-04-07 19:56:20 +00:00
|
|
|
|
2022-04-20 04:49:27 +00:00
|
|
|
export type TaskListProps = TaskListType & {
|
|
|
|
eventName?: string;
|
|
|
|
query: {
|
|
|
|
task?: string;
|
|
|
|
};
|
2022-04-22 20:21:34 +00:00
|
|
|
cesHeader?: boolean;
|
2022-04-20 04:49:27 +00:00
|
|
|
};
|
|
|
|
|
2023-05-17 01:54:31 +00:00
|
|
|
export const SetupTaskList: React.FC< TaskListProps > = ( {
|
2021-10-20 16:16:22 +00:00
|
|
|
query,
|
2022-03-15 17:40:22 +00:00
|
|
|
id,
|
2021-10-20 16:16:22 +00:00
|
|
|
eventName,
|
2022-04-25 13:44:29 +00:00
|
|
|
eventPrefix,
|
2021-10-20 16:16:22 +00:00
|
|
|
tasks,
|
|
|
|
keepCompletedTaskList,
|
|
|
|
isComplete,
|
2022-03-21 13:52:04 +00:00
|
|
|
displayProgressHeader,
|
2022-04-07 19:56:20 +00:00
|
|
|
cesHeader = true,
|
2021-10-20 16:16:22 +00:00
|
|
|
} ) => {
|
2022-04-25 13:44:29 +00:00
|
|
|
const listEventPrefix = eventName ? eventName + '_' : eventPrefix;
|
2022-04-08 18:37:22 +00:00
|
|
|
const { profileItems } = useSelect( ( select: WCDataSelector ) => {
|
|
|
|
const { getProfileItems } = select( ONBOARDING_STORE_NAME );
|
|
|
|
return {
|
|
|
|
profileItems: getProfileItems(),
|
|
|
|
};
|
|
|
|
} );
|
2022-04-21 19:40:48 +00:00
|
|
|
const {
|
|
|
|
hideTaskList,
|
|
|
|
visitedTask,
|
|
|
|
keepCompletedTaskList: keepCompletedTasks,
|
|
|
|
} = useDispatch( ONBOARDING_STORE_NAME );
|
2022-03-23 20:01:58 +00:00
|
|
|
const userPreferences = useUserPreferences();
|
2022-03-15 17:40:22 +00:00
|
|
|
const [ headerData, setHeaderData ] = useState< {
|
|
|
|
task?: TaskType;
|
|
|
|
goToTask?: () => void;
|
|
|
|
trackClick?: () => void;
|
|
|
|
} >( {} );
|
2021-10-20 16:16:22 +00:00
|
|
|
const [ activeTaskId, setActiveTaskId ] = useState( '' );
|
|
|
|
const [ showDismissModal, setShowDismissModal ] = useState( false );
|
2023-04-25 22:17:33 +00:00
|
|
|
const { layoutString } = useLayoutContext();
|
2021-10-20 16:16:22 +00:00
|
|
|
|
|
|
|
const prevQueryRef = useRef( query );
|
2022-03-15 17:40:22 +00:00
|
|
|
|
2022-04-13 14:20:26 +00:00
|
|
|
const visibleTasks = getVisibleTasks( tasks );
|
2022-03-15 17:40:22 +00:00
|
|
|
const recordTaskListView = () => {
|
|
|
|
if ( query.task ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:44:29 +00:00
|
|
|
recordEvent( `${ listEventPrefix }view`, {
|
2022-03-15 17:40:22 +00:00
|
|
|
number_tasks: visibleTasks.length,
|
|
|
|
store_connected: profileItems.wccom_connected,
|
2023-04-25 22:17:33 +00:00
|
|
|
context: layoutString,
|
2022-03-15 17:40:22 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2021-10-20 16:16:22 +00:00
|
|
|
useEffect( () => {
|
|
|
|
recordTaskListView();
|
|
|
|
}, [] );
|
|
|
|
|
2023-01-21 03:05:45 +00:00
|
|
|
const taskListCompletionSlot = useSlot(
|
|
|
|
EXPERIMENTAL_WC_TASK_LIST_COMPLETION_SLOT_NAME
|
|
|
|
);
|
|
|
|
|
2021-10-20 16:16:22 +00:00
|
|
|
useEffect( () => {
|
|
|
|
const { task: prevTask } = prevQueryRef.current;
|
|
|
|
const { task } = query;
|
|
|
|
|
|
|
|
if ( prevTask !== task ) {
|
|
|
|
window.document.documentElement.scrollTop = 0;
|
|
|
|
prevQueryRef.current = query;
|
|
|
|
}
|
|
|
|
}, [ query ] );
|
|
|
|
|
|
|
|
const incompleteTasks = tasks.filter(
|
|
|
|
( task ) => ! task.isComplete && ! task.isDismissed
|
|
|
|
);
|
|
|
|
|
2022-04-28 17:47:06 +00:00
|
|
|
const hideTasks = () => {
|
2022-03-15 17:40:22 +00:00
|
|
|
hideTaskList( id );
|
2021-10-20 16:16:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const keepTasks = () => {
|
2022-04-21 19:40:48 +00:00
|
|
|
keepCompletedTasks( id );
|
2021-10-20 16:16:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const renderMenu = () => {
|
|
|
|
return (
|
|
|
|
<div className="woocommerce-card__menu woocommerce-card__header-item">
|
|
|
|
<EllipsisMenu
|
2022-03-15 17:40:22 +00:00
|
|
|
className={ id }
|
2022-03-30 09:00:04 +00:00
|
|
|
label={ __( 'Task List Options', 'woocommerce' ) }
|
2022-03-15 17:40:22 +00:00
|
|
|
renderContent={ ( {
|
|
|
|
onToggle,
|
|
|
|
}: {
|
|
|
|
onToggle: () => void;
|
|
|
|
} ) => (
|
2021-10-20 16:16:22 +00:00
|
|
|
<div className="woocommerce-task-card__section-controls">
|
|
|
|
<Button
|
|
|
|
onClick={ () => {
|
|
|
|
if ( incompleteTasks.length > 0 ) {
|
|
|
|
setShowDismissModal( true );
|
|
|
|
onToggle();
|
|
|
|
} else {
|
2022-04-28 17:47:06 +00:00
|
|
|
hideTasks();
|
2021-10-20 16:16:22 +00:00
|
|
|
}
|
|
|
|
} }
|
|
|
|
>
|
2022-03-30 09:00:04 +00:00
|
|
|
{ __( 'Hide this', 'woocommerce' ) }
|
2021-10-20 16:16:22 +00:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
let selectedHeaderCard = visibleTasks.find(
|
|
|
|
( listTask ) => listTask.isComplete === false
|
|
|
|
);
|
|
|
|
|
2021-10-26 08:40:35 +00:00
|
|
|
// If nothing is selected, default to the last task since everything is completed.
|
2021-10-20 16:16:22 +00:00
|
|
|
if ( ! selectedHeaderCard ) {
|
2021-10-26 08:40:35 +00:00
|
|
|
selectedHeaderCard = visibleTasks[ visibleTasks.length - 1 ];
|
2021-10-20 16:16:22 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 07:19:20 +00:00
|
|
|
const taskListHeaderSlot = useSlot(
|
|
|
|
`woocommerce_onboarding_task_list_header_${
|
|
|
|
headerData?.task?.id ?? selectedHeaderCard?.id
|
|
|
|
}`
|
|
|
|
);
|
|
|
|
const hasTaskListHeaderSlotFills = Boolean(
|
|
|
|
taskListHeaderSlot?.fills?.length
|
|
|
|
);
|
|
|
|
|
2022-03-23 20:01:58 +00:00
|
|
|
const getTaskStartedCount = ( taskId: string ) => {
|
|
|
|
const trackedStartedTasks =
|
|
|
|
userPreferences.task_list_tracked_started_tasks;
|
|
|
|
if ( ! trackedStartedTasks || ! trackedStartedTasks[ taskId ] ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return trackedStartedTasks[ taskId ];
|
|
|
|
};
|
|
|
|
|
|
|
|
// @todo This would be better as a task endpoint that handles updating the count.
|
|
|
|
const updateTrackStartedCount = ( taskId: string ) => {
|
|
|
|
const newCount = getTaskStartedCount( taskId ) + 1;
|
|
|
|
const trackedStartedTasks =
|
|
|
|
userPreferences.task_list_tracked_started_tasks || {};
|
|
|
|
|
|
|
|
visitedTask( taskId );
|
|
|
|
userPreferences.updateUserPreferences( {
|
|
|
|
task_list_tracked_started_tasks: {
|
|
|
|
...( trackedStartedTasks || {} ),
|
|
|
|
[ taskId ]: newCount,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2022-03-15 17:40:22 +00:00
|
|
|
const trackClick = ( task: TaskType ) => {
|
2022-04-25 13:44:29 +00:00
|
|
|
recordEvent( `${ listEventPrefix }click`, {
|
2022-03-15 17:40:22 +00:00
|
|
|
task_name: task.id,
|
2023-04-25 22:17:33 +00:00
|
|
|
context: layoutString,
|
2022-03-15 17:40:22 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
const goToTask = ( task: TaskType ) => {
|
2021-10-26 08:40:35 +00:00
|
|
|
trackClick( task );
|
2022-03-23 20:01:58 +00:00
|
|
|
if ( ! task.isComplete ) {
|
|
|
|
updateTrackStartedCount( task.id );
|
|
|
|
}
|
2022-06-01 17:10:20 +00:00
|
|
|
|
2022-03-23 20:11:02 +00:00
|
|
|
if ( task.actionUrl ) {
|
2022-06-01 17:10:20 +00:00
|
|
|
navigateTo( {
|
|
|
|
url: task.actionUrl,
|
|
|
|
} );
|
2022-03-23 20:11:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-06-01 17:10:20 +00:00
|
|
|
|
|
|
|
navigateTo( { url: getNewPath( { task: task.id }, '/', {} ) } );
|
2021-10-26 08:40:35 +00:00
|
|
|
};
|
2021-10-20 16:16:22 +00:00
|
|
|
|
2022-03-15 17:40:22 +00:00
|
|
|
const showTaskHeader = ( task: TaskType ) => {
|
2023-01-21 07:19:20 +00:00
|
|
|
if ( taskHeaders[ task.id ] || hasTaskListHeaderSlotFills ) {
|
2021-10-26 08:40:35 +00:00
|
|
|
setHeaderData( {
|
|
|
|
task,
|
|
|
|
goToTask: () => goToTask( task ),
|
|
|
|
trackClick: () => trackClick( task ),
|
|
|
|
} );
|
2021-10-20 16:16:22 +00:00
|
|
|
setActiveTaskId( task.id );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if ( selectedHeaderCard ) {
|
2021-10-26 08:40:35 +00:00
|
|
|
showTaskHeader( selectedHeaderCard );
|
2021-10-20 16:16:22 +00:00
|
|
|
}
|
|
|
|
}, [ selectedHeaderCard ] );
|
|
|
|
|
|
|
|
if ( ! visibleTasks.length ) {
|
|
|
|
return <div className="woocommerce-task-dashboard__container"></div>;
|
|
|
|
}
|
|
|
|
|
2023-01-21 03:05:45 +00:00
|
|
|
const hasTaskListCompletionSlotFills = Boolean(
|
|
|
|
taskListCompletionSlot?.fills?.length
|
|
|
|
);
|
|
|
|
|
2022-04-08 18:37:22 +00:00
|
|
|
if ( isComplete && keepCompletedTaskList !== 'yes' ) {
|
2023-01-21 03:05:45 +00:00
|
|
|
if ( hasTaskListCompletionSlotFills ) {
|
|
|
|
return (
|
|
|
|
<TaskListCompletionSlot
|
|
|
|
fillProps={ {
|
|
|
|
hideTasks,
|
|
|
|
keepTasks,
|
|
|
|
customerEffortScore: cesHeader,
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2021-10-20 16:16:22 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-04-07 19:56:20 +00:00
|
|
|
{ cesHeader ? (
|
2022-05-16 13:03:46 +00:00
|
|
|
<TaskListCompletedHeader
|
2022-04-07 19:56:20 +00:00
|
|
|
hideTasks={ hideTasks }
|
|
|
|
keepTasks={ keepTasks }
|
2022-05-17 17:25:06 +00:00
|
|
|
customerEffortScore={ true }
|
2022-04-07 19:56:20 +00:00
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<TaskListCompleted
|
|
|
|
hideTasks={ hideTasks }
|
|
|
|
keepTasks={ keepTasks }
|
|
|
|
/>
|
|
|
|
) }
|
2021-10-20 16:16:22 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{ showDismissModal && (
|
|
|
|
<DismissModal
|
|
|
|
showDismissModal={ showDismissModal }
|
|
|
|
setShowDismissModal={ setShowDismissModal }
|
|
|
|
hideTasks={ hideTasks }
|
|
|
|
/>
|
|
|
|
) }
|
2022-03-21 13:52:04 +00:00
|
|
|
{ displayProgressHeader ? (
|
|
|
|
<ProgressHeader taskListId={ id } />
|
|
|
|
) : null }
|
2021-10-20 16:16:22 +00:00
|
|
|
<div
|
2024-05-31 03:49:36 +00:00
|
|
|
className={ clsx(
|
2023-05-17 01:54:31 +00:00
|
|
|
`woocommerce-task-dashboard__container woocommerce-task-list__${ id } setup-task-list`
|
2021-10-20 16:16:22 +00:00
|
|
|
) }
|
|
|
|
>
|
|
|
|
<Card
|
|
|
|
size="large"
|
|
|
|
className="woocommerce-task-card woocommerce-homescreen-card"
|
|
|
|
>
|
2021-10-26 08:40:35 +00:00
|
|
|
<div className="wooocommerce-task-card__header-container">
|
2021-10-20 16:16:22 +00:00
|
|
|
<div className="wooocommerce-task-card__header">
|
2023-01-21 07:19:20 +00:00
|
|
|
{ hasTaskListHeaderSlotFills ? (
|
|
|
|
<WooOnboardingTaskListHeader.Slot
|
|
|
|
id={ selectedHeaderCard?.id }
|
|
|
|
fillProps={ headerData }
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
headerData?.task &&
|
2021-10-26 08:40:35 +00:00
|
|
|
createElement(
|
|
|
|
taskHeaders[ headerData.task.id ],
|
|
|
|
headerData
|
2023-01-21 07:19:20 +00:00
|
|
|
)
|
|
|
|
) }
|
2021-10-20 16:16:22 +00:00
|
|
|
</div>
|
2022-03-21 13:52:04 +00:00
|
|
|
{ ! displayProgressHeader && renderMenu() }
|
2021-10-26 08:40:35 +00:00
|
|
|
</div>
|
2021-10-20 16:16:22 +00:00
|
|
|
<List animation="custom">
|
|
|
|
{ visibleTasks.map( ( task, index ) => {
|
|
|
|
return (
|
2023-05-17 01:54:31 +00:00
|
|
|
<TaskListItem
|
2021-10-20 16:16:22 +00:00
|
|
|
key={ task.id }
|
2022-04-28 17:47:06 +00:00
|
|
|
taskIndex={ ++index }
|
|
|
|
activeTaskId={ activeTaskId }
|
|
|
|
task={ task }
|
|
|
|
goToTask={ () => goToTask( task ) }
|
|
|
|
trackClick={ () => trackClick( task ) }
|
2021-10-20 16:16:22 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
</List>
|
2023-01-23 05:48:42 +00:00
|
|
|
<ExperimentalWooTaskListFooter />
|
2021-10-20 16:16:22 +00:00
|
|
|
</Card>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-05-17 01:54:31 +00:00
|
|
|
export default SetupTaskList;
|