2020-12-03 21:16:04 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-05-03 17:13:47 +00:00
|
|
|
import { __, _n, sprintf } from '@wordpress/i18n';
|
2021-06-01 18:43:17 +00:00
|
|
|
import { useEffect, useRef, useState } from '@wordpress/element';
|
2021-01-07 23:57:09 +00:00
|
|
|
import { Button, Card, CardBody, CardHeader } from '@wordpress/components';
|
2021-04-27 15:23:34 +00:00
|
|
|
import { useSelect, useDispatch } from '@wordpress/data';
|
2021-05-10 14:40:49 +00:00
|
|
|
import { EllipsisMenu, Badge } from '@woocommerce/components';
|
2021-04-27 15:23:34 +00:00
|
|
|
import { updateQueryString } from '@woocommerce/navigation';
|
|
|
|
import { OPTIONS_STORE_NAME, ONBOARDING_STORE_NAME } from '@woocommerce/data';
|
2020-12-03 21:16:04 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2021-05-13 14:05:30 +00:00
|
|
|
import {
|
|
|
|
Text,
|
|
|
|
List,
|
|
|
|
CollapsibleList,
|
|
|
|
TaskItem,
|
|
|
|
} from '@woocommerce/experimental';
|
2021-04-27 15:23:34 +00:00
|
|
|
|
2021-06-03 21:44:05 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './task-list.scss';
|
|
|
|
|
|
|
|
const DAY_IN_MS = 24 * 60 * 60 * 1000;
|
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
export const TaskList = ( {
|
|
|
|
query,
|
2021-06-01 18:43:17 +00:00
|
|
|
name,
|
|
|
|
eventName,
|
2021-04-27 15:23:34 +00:00
|
|
|
isComplete,
|
|
|
|
dismissedTasks,
|
2021-06-03 21:44:05 +00:00
|
|
|
remindMeLaterTasks,
|
2021-04-27 15:23:34 +00:00
|
|
|
tasks,
|
|
|
|
trackedCompletedTasks: totalTrackedCompletedTasks,
|
|
|
|
title: listTitle,
|
2021-06-01 18:43:17 +00:00
|
|
|
collapsible = false,
|
|
|
|
onComplete,
|
|
|
|
onHide,
|
|
|
|
expandingItems = false,
|
2021-04-27 15:23:34 +00:00
|
|
|
} ) => {
|
|
|
|
const { createNotice } = useDispatch( 'core/notices' );
|
|
|
|
const { updateOptions } = useDispatch( OPTIONS_STORE_NAME );
|
|
|
|
const { profileItems } = useSelect( ( select ) => {
|
|
|
|
const { getProfileItems } = select( ONBOARDING_STORE_NAME );
|
2020-12-03 21:16:04 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
return {
|
|
|
|
profileItems: getProfileItems(),
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
|
|
|
const prevQueryRef = useRef( query );
|
|
|
|
useEffect( () => {
|
|
|
|
recordTaskListView();
|
|
|
|
}, [] );
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
const { task: prevTask } = prevQueryRef.current;
|
2020-12-03 21:16:04 +00:00
|
|
|
const { task } = query;
|
|
|
|
|
|
|
|
if ( prevTask !== task ) {
|
|
|
|
window.document.documentElement.scrollTop = 0;
|
2021-04-27 15:23:34 +00:00
|
|
|
prevQueryRef.current = query;
|
2020-12-03 21:16:04 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
possiblyCompleteTaskList();
|
|
|
|
possiblyTrackCompletedTasks();
|
|
|
|
}, [ query ] );
|
2020-12-03 21:16:04 +00:00
|
|
|
|
2021-06-03 21:44:05 +00:00
|
|
|
const nowTimestamp = Date.now();
|
2021-06-01 18:04:21 +00:00
|
|
|
const visibleTasks = tasks.filter(
|
2021-06-03 21:44:05 +00:00
|
|
|
( task ) =>
|
|
|
|
task.visible &&
|
|
|
|
! dismissedTasks.includes( task.key ) &&
|
|
|
|
( ! remindMeLaterTasks[ task.key ] ||
|
|
|
|
remindMeLaterTasks[ task.key ] < nowTimestamp )
|
2021-06-01 18:04:21 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const completedTaskKeys = visibleTasks
|
|
|
|
.filter( ( task ) => task.completed )
|
|
|
|
.map( ( task ) => task.key );
|
|
|
|
|
|
|
|
const incompleteTasks = tasks.filter(
|
|
|
|
( task ) =>
|
|
|
|
task.visible &&
|
|
|
|
! task.completed &&
|
|
|
|
! dismissedTasks.includes( task.key )
|
|
|
|
);
|
|
|
|
|
2021-06-01 18:43:17 +00:00
|
|
|
const [ currentTask, setCurrentTask ] = useState(
|
|
|
|
incompleteTasks[ 0 ]?.key
|
|
|
|
);
|
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const possiblyCompleteTaskList = () => {
|
2021-01-27 18:40:02 +00:00
|
|
|
const taskListVariableName = `woocommerce_${ name }_complete`;
|
|
|
|
const taskListToComplete = isComplete
|
|
|
|
? { [ taskListVariableName ]: 'no' }
|
|
|
|
: { [ taskListVariableName ]: 'yes' };
|
2020-12-03 21:16:04 +00:00
|
|
|
|
|
|
|
if (
|
2021-06-01 18:04:21 +00:00
|
|
|
( ! incompleteTasks.length && ! isComplete ) ||
|
|
|
|
( incompleteTasks.length && isComplete )
|
2020-12-03 21:16:04 +00:00
|
|
|
) {
|
|
|
|
updateOptions( {
|
|
|
|
...taskListToComplete,
|
|
|
|
} );
|
2021-06-01 18:43:17 +00:00
|
|
|
|
|
|
|
if ( typeof onComplete === 'function' ) {
|
|
|
|
onComplete();
|
|
|
|
}
|
2020-12-03 21:16:04 +00:00
|
|
|
}
|
2021-04-27 15:23:34 +00:00
|
|
|
};
|
2020-12-03 21:16:04 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const getTrackedIncompletedTasks = (
|
|
|
|
partialCompletedTasks,
|
|
|
|
allTrackedTask
|
|
|
|
) => {
|
2021-06-01 18:04:21 +00:00
|
|
|
return visibleTasks
|
2021-01-27 18:40:02 +00:00
|
|
|
.filter(
|
|
|
|
( task ) =>
|
|
|
|
allTrackedTask.includes( task.key ) &&
|
|
|
|
! partialCompletedTasks.includes( task.key )
|
|
|
|
)
|
|
|
|
.map( ( task ) => task.key );
|
2021-04-27 15:23:34 +00:00
|
|
|
};
|
2021-01-27 18:40:02 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const possiblyTrackCompletedTasks = () => {
|
|
|
|
const trackedCompletedTasks = getTrackedCompletedTasks(
|
2020-12-03 21:16:04 +00:00
|
|
|
completedTaskKeys,
|
|
|
|
totalTrackedCompletedTasks
|
|
|
|
);
|
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const trackedIncompleteTasks = getTrackedIncompletedTasks(
|
2021-01-27 18:40:02 +00:00
|
|
|
trackedCompletedTasks,
|
|
|
|
totalTrackedCompletedTasks
|
|
|
|
);
|
|
|
|
|
2020-12-03 21:16:04 +00:00
|
|
|
if (
|
2021-04-27 15:23:34 +00:00
|
|
|
shouldUpdateCompletedTasks(
|
2020-12-03 21:16:04 +00:00
|
|
|
trackedCompletedTasks,
|
2021-01-27 18:40:02 +00:00
|
|
|
trackedIncompleteTasks,
|
2020-12-03 21:16:04 +00:00
|
|
|
completedTaskKeys
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
updateOptions( {
|
2021-04-27 15:23:34 +00:00
|
|
|
woocommerce_task_list_tracked_completed_tasks: getTasksForUpdate(
|
2021-01-27 18:40:02 +00:00
|
|
|
completedTaskKeys,
|
|
|
|
totalTrackedCompletedTasks,
|
|
|
|
trackedIncompleteTasks
|
|
|
|
),
|
2020-12-03 21:16:04 +00:00
|
|
|
} );
|
|
|
|
}
|
2021-04-27 15:23:34 +00:00
|
|
|
};
|
2020-12-03 21:16:04 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const dismissTask = ( { key, onDismiss } ) => {
|
2020-12-03 21:16:04 +00:00
|
|
|
createNotice( 'success', __( 'Task dismissed' ), {
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
label: __( 'Undo', 'woocommerce-admin' ),
|
2021-04-27 15:23:34 +00:00
|
|
|
onClick: () => undoDismissTask( key ),
|
2020-12-03 21:16:04 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
} );
|
|
|
|
|
2021-06-03 21:44:05 +00:00
|
|
|
recordEvent( `${ eventName }_dismiss_task`, { task_name: key } );
|
2020-12-03 21:16:04 +00:00
|
|
|
|
|
|
|
updateOptions( {
|
|
|
|
woocommerce_task_list_dismissed_tasks: [ ...dismissedTasks, key ],
|
|
|
|
} );
|
|
|
|
if ( onDismiss ) {
|
|
|
|
onDismiss();
|
|
|
|
}
|
2021-04-27 15:23:34 +00:00
|
|
|
};
|
2020-12-03 21:16:04 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const undoDismissTask = ( key ) => {
|
2020-12-03 21:16:04 +00:00
|
|
|
const updatedDismissedTasks = dismissedTasks.filter(
|
|
|
|
( task ) => task !== key
|
|
|
|
);
|
|
|
|
|
|
|
|
updateOptions( {
|
|
|
|
woocommerce_task_list_dismissed_tasks: updatedDismissedTasks,
|
|
|
|
} );
|
2021-06-03 21:44:05 +00:00
|
|
|
recordEvent( `${ eventName }_undo_dismiss_task`, {
|
|
|
|
task_name: key,
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
const remindTaskLater = ( { key, onDismiss } ) => {
|
|
|
|
createNotice(
|
|
|
|
'success',
|
|
|
|
__( 'Task postponed until tomorrow', 'woocommerce-admin' ),
|
|
|
|
{
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
label: __( 'Undo', 'woocommerce-admin' ),
|
|
|
|
onClick: () => undoRemindTaskLater( key ),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
recordEvent( `${ eventName }_remindmelater_task`, {
|
|
|
|
task_name: key,
|
|
|
|
} );
|
|
|
|
|
|
|
|
const dismissTime = Date.now() + DAY_IN_MS;
|
|
|
|
updateOptions( {
|
|
|
|
woocommerce_task_list_remind_me_later_tasks: {
|
|
|
|
...remindMeLaterTasks,
|
|
|
|
[ key ]: dismissTime,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
if ( onDismiss ) {
|
|
|
|
onDismiss();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const undoRemindTaskLater = ( key ) => {
|
|
|
|
const {
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
[ key ]: oldValue,
|
|
|
|
...updatedRemindMeLaterTasks
|
|
|
|
} = remindMeLaterTasks;
|
|
|
|
|
|
|
|
updateOptions( {
|
|
|
|
woocommerce_task_list_remind_me_later_tasks: updatedRemindMeLaterTasks,
|
|
|
|
} );
|
|
|
|
recordEvent( `${ eventName }_undo_remindmelater_task`, {
|
|
|
|
task_name: key,
|
|
|
|
} );
|
2021-04-27 15:23:34 +00:00
|
|
|
};
|
2020-12-03 21:16:04 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const recordTaskListView = () => {
|
|
|
|
if ( query.task ) {
|
2020-12-03 21:16:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-01 18:43:17 +00:00
|
|
|
recordEvent( `${ eventName }_view`, {
|
2021-04-27 15:23:34 +00:00
|
|
|
number_tasks: visibleTasks.length,
|
2020-12-03 21:16:04 +00:00
|
|
|
store_connected: profileItems.wccom_connected,
|
|
|
|
} );
|
2021-04-27 15:23:34 +00:00
|
|
|
};
|
2020-12-03 21:16:04 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const hideTaskCard = ( action ) => {
|
2021-01-27 18:40:02 +00:00
|
|
|
const updateOptionsParams = {
|
|
|
|
[ `woocommerce_${ name }_hidden` ]: 'yes',
|
|
|
|
};
|
|
|
|
|
2021-06-01 18:43:17 +00:00
|
|
|
recordEvent( `${ eventName }_completed`, {
|
2020-12-03 21:16:04 +00:00
|
|
|
action,
|
2021-06-01 18:04:21 +00:00
|
|
|
completed_task_count: completedTaskKeys.length,
|
|
|
|
incomplete_task_count: incompleteTasks.length,
|
2020-12-03 21:16:04 +00:00
|
|
|
} );
|
2021-01-27 18:40:02 +00:00
|
|
|
updateOptions( {
|
|
|
|
...updateOptionsParams,
|
2020-12-03 21:16:04 +00:00
|
|
|
} );
|
2021-06-01 18:43:17 +00:00
|
|
|
|
|
|
|
if ( typeof onHide === 'function' ) {
|
|
|
|
onHide();
|
|
|
|
}
|
2021-04-27 15:23:34 +00:00
|
|
|
};
|
2020-12-03 21:16:04 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const renderMenu = () => {
|
2020-12-03 21:16:04 +00:00
|
|
|
return (
|
|
|
|
<div className="woocommerce-card__menu woocommerce-card__header-item">
|
|
|
|
<EllipsisMenu
|
|
|
|
label={ __( 'Task List Options', 'woocommerce-admin' ) }
|
|
|
|
renderContent={ () => (
|
|
|
|
<div className="woocommerce-task-card__section-controls">
|
|
|
|
<Button
|
2021-04-27 15:23:34 +00:00
|
|
|
onClick={ () => hideTaskCard( 'remove_card' ) }
|
2020-12-03 21:16:04 +00:00
|
|
|
>
|
|
|
|
{ __( 'Hide this', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
2021-04-27 15:23:34 +00:00
|
|
|
};
|
|
|
|
|
2021-06-01 18:04:21 +00:00
|
|
|
const listTasks = visibleTasks.map( ( task ) => {
|
2021-04-27 15:23:34 +00:00
|
|
|
if ( ! task.onClick ) {
|
|
|
|
task.onClick = ( e ) => {
|
2021-06-03 21:44:05 +00:00
|
|
|
recordEvent( `${ eventName }_click`, {
|
|
|
|
task_name: task.key,
|
|
|
|
} );
|
2021-04-27 15:23:34 +00:00
|
|
|
if ( e.target.nodeName === 'A' ) {
|
|
|
|
// This is a nested link, so don't activate this task.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateQueryString( { task: task.key } );
|
|
|
|
};
|
|
|
|
}
|
2020-12-03 21:16:04 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
return task;
|
|
|
|
} );
|
2020-12-03 21:16:04 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
if ( ! listTasks.length ) {
|
|
|
|
return <div className="woocommerce-task-dashboard__container"></div>;
|
|
|
|
}
|
2020-12-03 21:16:04 +00:00
|
|
|
|
2021-05-03 17:13:47 +00:00
|
|
|
const expandLabel = sprintf(
|
|
|
|
/* translators: %i = number of hidden tasks */
|
|
|
|
_n(
|
|
|
|
'Show %i more task.',
|
|
|
|
'Show %i more tasks.',
|
|
|
|
listTasks.length - 2,
|
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
|
|
|
listTasks.length - 2
|
|
|
|
);
|
|
|
|
const collapseLabel = __( 'Show less', 'woocommerce-admin' );
|
2021-06-01 18:43:17 +00:00
|
|
|
const ListComp = collapsible ? CollapsibleList : List;
|
|
|
|
|
|
|
|
const listProps = collapsible
|
|
|
|
? {
|
|
|
|
collapseLabel,
|
|
|
|
expandLabel,
|
|
|
|
show: 2,
|
|
|
|
onCollapse: () => recordEvent( `${ eventName }_collapse` ),
|
|
|
|
onExpand: () => recordEvent( `${ eventName }_expand` ),
|
|
|
|
}
|
|
|
|
: {};
|
2021-05-03 17:13:47 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className="woocommerce-task-dashboard__container">
|
|
|
|
<Card
|
|
|
|
size="large"
|
|
|
|
className="woocommerce-task-card woocommerce-homescreen-card"
|
2020-12-03 21:16:04 +00:00
|
|
|
>
|
2021-04-27 15:23:34 +00:00
|
|
|
<CardHeader size="medium">
|
|
|
|
<div className="wooocommerce-task-card__header">
|
|
|
|
<Text variant="title.small">{ listTitle }</Text>
|
2021-06-01 18:04:21 +00:00
|
|
|
<Badge count={ incompleteTasks.length } />
|
2020-12-03 21:16:04 +00:00
|
|
|
</div>
|
2021-04-27 15:23:34 +00:00
|
|
|
{ renderMenu() }
|
|
|
|
</CardHeader>
|
|
|
|
<CardBody>
|
2021-06-03 21:44:05 +00:00
|
|
|
<ListComp animation="custom" { ...listProps }>
|
2021-04-27 15:23:34 +00:00
|
|
|
{ listTasks.map( ( task ) => (
|
|
|
|
<TaskItem
|
|
|
|
key={ task.key }
|
|
|
|
title={ task.title }
|
|
|
|
completed={ task.completed }
|
|
|
|
content={ task.content }
|
2021-06-01 18:43:17 +00:00
|
|
|
onClick={
|
|
|
|
! expandingItems || task.completed
|
|
|
|
? task.onClick
|
|
|
|
: () => setCurrentTask( task.key )
|
|
|
|
}
|
2021-06-08 12:16:53 +00:00
|
|
|
expandable={ expandingItems }
|
2021-06-01 18:43:17 +00:00
|
|
|
expanded={
|
|
|
|
expandingItems &&
|
|
|
|
currentTask === task.key
|
|
|
|
}
|
2021-06-03 21:44:05 +00:00
|
|
|
onDismiss={
|
|
|
|
task.isDismissable
|
|
|
|
? () => dismissTask( task )
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
remindMeLater={
|
|
|
|
task.allowRemindMeLater
|
|
|
|
? () => remindTaskLater( task )
|
|
|
|
: undefined
|
|
|
|
}
|
2021-04-27 15:23:34 +00:00
|
|
|
time={ task.time }
|
2021-05-05 17:43:46 +00:00
|
|
|
level={ task.level }
|
2021-06-01 18:04:21 +00:00
|
|
|
action={ task.onClick }
|
|
|
|
actionLabel={ task.action }
|
|
|
|
additionalInfo={ task.additionalInfo }
|
2021-04-27 15:23:34 +00:00
|
|
|
/>
|
|
|
|
) ) }
|
2021-05-03 17:13:47 +00:00
|
|
|
</ListComp>
|
2021-04-27 15:23:34 +00:00
|
|
|
</CardBody>
|
|
|
|
</Card>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2020-12-03 21:16:04 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
function shouldUpdateCompletedTasks( tasks, untrackedTasks, completedTasks ) {
|
|
|
|
if ( untrackedTasks.length > 0 ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ( completedTasks.length === 0 ) {
|
|
|
|
return false;
|
2020-12-03 21:16:04 +00:00
|
|
|
}
|
2021-04-27 15:23:34 +00:00
|
|
|
return ! completedTasks.every(
|
|
|
|
( taskName ) => tasks.indexOf( taskName ) >= 0
|
|
|
|
);
|
2020-12-03 21:16:04 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
function getTrackedCompletedTasks( completedTasks, trackedTasks ) {
|
|
|
|
if ( ! trackedTasks ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return completedTasks.filter( ( taskName ) =>
|
|
|
|
trackedTasks.includes( taskName )
|
|
|
|
);
|
|
|
|
}
|
2020-12-03 21:16:04 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
function getTasksForUpdate(
|
|
|
|
completedTaskKeys,
|
|
|
|
totalTrackedCompletedTasks,
|
|
|
|
trackedIncompleteTasks
|
|
|
|
) {
|
|
|
|
const mergedLists = [
|
|
|
|
...new Set( [ ...completedTaskKeys, ...totalTrackedCompletedTasks ] ),
|
|
|
|
];
|
|
|
|
return mergedLists.filter(
|
|
|
|
( taskName ) => ! trackedIncompleteTasks.includes( taskName )
|
|
|
|
);
|
|
|
|
}
|
2020-12-03 21:16:04 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
export default TaskList;
|