2021-10-20 16:16:22 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2021-10-26 08:40:35 +00:00
|
|
|
import { useEffect, useRef, useState, createElement } from '@wordpress/element';
|
|
|
|
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';
|
|
|
|
import { updateQueryString } from '@woocommerce/navigation';
|
2022-03-15 17:40:22 +00:00
|
|
|
import {
|
|
|
|
OPTIONS_STORE_NAME,
|
|
|
|
ONBOARDING_STORE_NAME,
|
|
|
|
TaskType,
|
|
|
|
} from '@woocommerce/data';
|
2021-10-20 16:16:22 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
import { List, TaskItem } from '@woocommerce/experimental';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import '../tasks/task-list.scss';
|
|
|
|
import taskHeaders from './task-headers';
|
2022-02-04 15:26:56 +00:00
|
|
|
import DismissModal from './dismiss-modal';
|
2021-10-20 16:16:22 +00:00
|
|
|
import TaskListCompleted from './completed';
|
2022-03-15 17:40:22 +00:00
|
|
|
import { TaskListProps } from '~/tasks/task-list';
|
2022-03-21 13:52:04 +00:00
|
|
|
import { ProgressHeader } from '~/task-lists/progress-header';
|
2021-10-20 16:16:22 +00:00
|
|
|
|
2022-03-15 17:40:22 +00:00
|
|
|
export const TaskList: 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,
|
|
|
|
tasks,
|
|
|
|
twoColumns,
|
|
|
|
keepCompletedTaskList,
|
|
|
|
isComplete,
|
2022-03-21 13:52:04 +00:00
|
|
|
displayProgressHeader,
|
2021-10-20 16:16:22 +00:00
|
|
|
} ) => {
|
|
|
|
const { createNotice } = useDispatch( 'core/notices' );
|
|
|
|
const { updateOptions, dismissTask, undoDismissTask } = useDispatch(
|
|
|
|
OPTIONS_STORE_NAME
|
|
|
|
);
|
|
|
|
const { profileItems } = useSelect( ( select ) => {
|
|
|
|
const { getProfileItems } = select( ONBOARDING_STORE_NAME );
|
|
|
|
return {
|
|
|
|
profileItems: getProfileItems(),
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
const { hideTaskList } = useDispatch( ONBOARDING_STORE_NAME );
|
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 );
|
|
|
|
|
|
|
|
const prevQueryRef = useRef( query );
|
2022-03-15 17:40:22 +00:00
|
|
|
|
|
|
|
const nowTimestamp = Date.now();
|
|
|
|
const visibleTasks = tasks.filter(
|
|
|
|
( task ) =>
|
|
|
|
! task.isDismissed &&
|
|
|
|
( ! task.isSnoozed || task.snoozedUntil < nowTimestamp )
|
|
|
|
);
|
|
|
|
|
|
|
|
const recordTaskListView = () => {
|
|
|
|
if ( query.task ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
recordEvent( `${ eventName }_view`, {
|
|
|
|
number_tasks: visibleTasks.length,
|
|
|
|
store_connected: profileItems.wccom_connected,
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2021-10-20 16:16:22 +00:00
|
|
|
useEffect( () => {
|
|
|
|
recordTaskListView();
|
|
|
|
}, [] );
|
|
|
|
|
|
|
|
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-03-15 17:40:22 +00:00
|
|
|
const onDismissTask = ( taskId: string, onDismiss?: () => void ) => {
|
|
|
|
dismissTask( taskId );
|
2021-10-20 16:16:22 +00:00
|
|
|
createNotice( 'success', __( 'Task dismissed' ), {
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
label: __( 'Undo', 'woocommerce-admin' ),
|
2022-03-15 17:40:22 +00:00
|
|
|
onClick: () => undoDismissTask( taskId ),
|
2021-10-20 16:16:22 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( onDismiss ) {
|
|
|
|
onDismiss();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-15 17:40:22 +00:00
|
|
|
const hideTasks = ( event: string ) => {
|
|
|
|
hideTaskList( id );
|
2021-10-20 16:16:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const keepTasks = () => {
|
|
|
|
const updateOptionsParams = {
|
|
|
|
woocommerce_task_list_keep_completed: 'yes',
|
|
|
|
};
|
|
|
|
|
|
|
|
updateOptions( {
|
|
|
|
...updateOptionsParams,
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderMenu = () => {
|
|
|
|
return (
|
|
|
|
<div className="woocommerce-card__menu woocommerce-card__header-item">
|
|
|
|
<EllipsisMenu
|
2022-03-15 17:40:22 +00:00
|
|
|
className={ id }
|
2021-10-20 16:16:22 +00:00
|
|
|
label={ __( 'Task List Options', 'woocommerce-admin' ) }
|
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 {
|
|
|
|
hideTasks( 'remove_card' );
|
|
|
|
}
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
{ __( 'Hide this', 'woocommerce-admin' ) }
|
|
|
|
</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
|
|
|
}
|
|
|
|
|
2022-03-15 17:40:22 +00:00
|
|
|
const trackClick = ( task: TaskType ) => {
|
|
|
|
recordEvent( `${ eventName }_click`, {
|
|
|
|
task_name: task.id,
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
const goToTask = ( task: TaskType ) => {
|
2021-10-26 08:40:35 +00:00
|
|
|
trackClick( task );
|
|
|
|
updateQueryString( { task: task.id } );
|
|
|
|
};
|
2021-10-20 16:16:22 +00:00
|
|
|
|
2022-03-15 17:40:22 +00:00
|
|
|
const showTaskHeader = ( task: TaskType ) => {
|
2021-10-20 16:16:22 +00:00
|
|
|
if ( taskHeaders[ task.id ] ) {
|
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 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-15 17:40:22 +00:00
|
|
|
const onTaskSelected = ( task: TaskType ) => {
|
2021-10-26 08:40:35 +00:00
|
|
|
if ( task.id === 'woocommerce-payments' ) {
|
|
|
|
// With WCPay, we have to show the header content for user to read t&c first.
|
|
|
|
showTaskHeader( task );
|
|
|
|
} else {
|
|
|
|
goToTask( task );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-20 16:16:22 +00:00
|
|
|
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>;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isComplete && ! keepCompletedTaskList ) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<TaskListCompleted
|
|
|
|
hideTasks={ hideTasks }
|
|
|
|
keepTasks={ keepTasks }
|
2022-03-15 17:40:22 +00:00
|
|
|
twoColumns={ false }
|
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
|
|
|
|
className={ classnames(
|
2022-03-15 17:40:22 +00:00
|
|
|
`woocommerce-task-dashboard__container two-column-experiment woocommerce-task-list__${ id }`,
|
2021-10-20 16:16:22 +00:00
|
|
|
{ 'two-columns': twoColumns !== false }
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
<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">
|
2021-10-26 08:40:35 +00:00
|
|
|
{ headerData?.task &&
|
|
|
|
createElement(
|
|
|
|
taskHeaders[ headerData.task.id ],
|
|
|
|
headerData
|
|
|
|
) }
|
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 ) => {
|
|
|
|
++index;
|
|
|
|
const className = classnames(
|
|
|
|
'woocommerce-task-list__item index-' + index,
|
|
|
|
{
|
|
|
|
complete: task.isComplete,
|
|
|
|
'is-active': task.id === activeTaskId,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<TaskItem
|
|
|
|
key={ task.id }
|
|
|
|
className={ className }
|
2021-10-27 09:41:51 +00:00
|
|
|
title={ task.title }
|
2021-10-20 16:16:22 +00:00
|
|
|
completed={ task.isComplete }
|
|
|
|
content={ task.content }
|
|
|
|
onClick={ () => {
|
|
|
|
onTaskSelected( task );
|
|
|
|
} }
|
|
|
|
onDismiss={
|
|
|
|
task.isDismissable
|
|
|
|
? () => onDismissTask( task.id )
|
|
|
|
: undefined
|
|
|
|
}
|
2022-03-15 17:40:22 +00:00
|
|
|
action={ () => {} }
|
2021-10-20 16:16:22 +00:00
|
|
|
actionLabel={ task.actionLabel }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
</List>
|
|
|
|
</Card>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default TaskList;
|