2022-04-22 15:04:51 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2022-06-01 17:10:20 +00:00
|
|
|
import { getNewPath, navigateTo } from '@woocommerce/navigation';
|
2022-04-29 19:31:26 +00:00
|
|
|
import {
|
|
|
|
ONBOARDING_STORE_NAME,
|
|
|
|
TaskType,
|
|
|
|
useUserPreferences,
|
|
|
|
} from '@woocommerce/data';
|
2022-04-22 15:04:51 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
import { TaskItem, useSlot } from '@woocommerce/experimental';
|
2022-06-14 18:31:50 +00:00
|
|
|
import { useCallback, useContext } from '@wordpress/element';
|
2022-04-22 15:04:51 +00:00
|
|
|
import { useDispatch } from '@wordpress/data';
|
|
|
|
import { WooOnboardingTaskListItem } from '@woocommerce/onboarding';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
|
2022-06-14 18:31:50 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { LayoutContext } from '~/layout';
|
|
|
|
|
2022-04-22 15:04:51 +00:00
|
|
|
export type TaskListItemProps = {
|
|
|
|
task: TaskType;
|
|
|
|
eventPrefix?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const TaskListItem: React.FC< TaskListItemProps > = ( {
|
|
|
|
task,
|
|
|
|
eventPrefix,
|
|
|
|
} ) => {
|
|
|
|
const { createNotice } = useDispatch( 'core/notices' );
|
2022-05-24 03:20:04 +00:00
|
|
|
|
2022-04-22 15:04:51 +00:00
|
|
|
const {
|
2022-05-24 03:20:04 +00:00
|
|
|
visitedTask,
|
2022-04-22 15:04:51 +00:00
|
|
|
dismissTask,
|
|
|
|
undoDismissTask,
|
|
|
|
snoozeTask,
|
|
|
|
undoSnoozeTask,
|
2022-05-24 03:20:04 +00:00
|
|
|
} = useDispatch( ONBOARDING_STORE_NAME );
|
2022-04-22 15:04:51 +00:00
|
|
|
|
2022-06-14 18:31:50 +00:00
|
|
|
const layoutContext = useContext( LayoutContext );
|
|
|
|
|
2022-04-22 15:04:51 +00:00
|
|
|
const slot = useSlot(
|
|
|
|
`woocommerce_onboarding_task_list_item_${ task.id }`
|
|
|
|
);
|
|
|
|
const hasFills = Boolean( slot?.fills?.length );
|
|
|
|
|
2022-04-29 19:31:26 +00:00
|
|
|
const userPreferences = useUserPreferences();
|
|
|
|
|
|
|
|
const getTaskStartedCount = () => {
|
|
|
|
const trackedStartedTasks =
|
|
|
|
userPreferences.task_list_tracked_started_tasks;
|
|
|
|
if ( ! trackedStartedTasks || ! trackedStartedTasks[ task.id ] ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return trackedStartedTasks[ task.id ];
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateTrackStartedCount = () => {
|
|
|
|
const newCount = getTaskStartedCount() + 1;
|
|
|
|
const trackedStartedTasks =
|
|
|
|
userPreferences.task_list_tracked_started_tasks || {};
|
|
|
|
|
|
|
|
visitedTask( task.id );
|
|
|
|
userPreferences.updateUserPreferences( {
|
|
|
|
task_list_tracked_started_tasks: {
|
|
|
|
...( trackedStartedTasks || {} ),
|
|
|
|
[ task.id ]: newCount,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2022-04-22 15:04:51 +00:00
|
|
|
const trackClick = () => {
|
2022-04-22 15:15:22 +00:00
|
|
|
recordEvent( `${ eventPrefix }click`, {
|
2022-04-22 15:04:51 +00:00
|
|
|
task_name: task.id,
|
2022-06-14 18:31:50 +00:00
|
|
|
context: layoutContext.toString(),
|
2022-04-22 15:04:51 +00:00
|
|
|
} );
|
2022-04-29 19:31:26 +00:00
|
|
|
|
|
|
|
if ( ! task.isComplete ) {
|
|
|
|
updateTrackStartedCount();
|
|
|
|
}
|
2022-04-22 15:04:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const onTaskSelected = () => {
|
|
|
|
trackClick();
|
|
|
|
|
|
|
|
if ( task.actionUrl ) {
|
2022-06-01 17:10:20 +00:00
|
|
|
navigateTo( {
|
|
|
|
url: task.actionUrl,
|
|
|
|
} );
|
2022-04-22 15:04:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-01 17:10:20 +00:00
|
|
|
navigateTo( { url: getNewPath( { task: task.id }, '/', {} ) } );
|
2022-04-22 15:04:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const onDismiss = useCallback( () => {
|
|
|
|
dismissTask( task.id );
|
2022-07-08 10:37:30 +00:00
|
|
|
createNotice( 'success', __( 'Task dismissed', 'woocommerce' ), {
|
2022-04-22 15:04:51 +00:00
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
label: __( 'Undo', 'woocommerce' ),
|
|
|
|
onClick: () => undoDismissTask( task.id ),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
} );
|
|
|
|
}, [ task.id ] );
|
|
|
|
|
|
|
|
const onSnooze = useCallback( () => {
|
|
|
|
snoozeTask( task.id );
|
|
|
|
createNotice(
|
|
|
|
'success',
|
|
|
|
__( 'Task postponed until tomorrow', 'woocommerce' ),
|
|
|
|
{
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
label: __( 'Undo', 'woocommerce' ),
|
|
|
|
onClick: () => undoSnoozeTask( task.id ),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}, [ task.id ] );
|
|
|
|
|
|
|
|
const className = classnames( 'woocommerce-task-list__item', {
|
|
|
|
complete: task.isComplete,
|
|
|
|
'is-disabled': task.isDisabled,
|
|
|
|
} );
|
|
|
|
|
|
|
|
const taskItemProps = {
|
|
|
|
completed: task.isComplete,
|
|
|
|
onSnooze: task.isSnoozeable && onSnooze,
|
|
|
|
onDismiss: task.isDismissable && onDismiss,
|
|
|
|
};
|
|
|
|
|
|
|
|
const DefaultTaskItem = useCallback(
|
|
|
|
( props ) => {
|
|
|
|
const onClickActions = () => {
|
|
|
|
if ( props.onClick ) {
|
|
|
|
trackClick();
|
|
|
|
return props.onClick();
|
|
|
|
}
|
|
|
|
return onTaskSelected();
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<TaskItem
|
|
|
|
key={ task.id }
|
|
|
|
className={ className }
|
|
|
|
title={ task.title }
|
|
|
|
completed={ task.isComplete }
|
|
|
|
expanded={ ! task.isComplete }
|
|
|
|
additionalInfo={ task.additionalInfo }
|
|
|
|
onDismiss={ task.isDismissable && onDismiss }
|
|
|
|
action={ () => {} }
|
|
|
|
actionLabel={ task.actionLabel }
|
|
|
|
{ ...props }
|
2022-04-28 17:47:06 +00:00
|
|
|
onClick={ ( e: React.ChangeEvent ) => {
|
|
|
|
if ( task.isDisabled || e.target.tagName === 'A' ) {
|
2022-04-22 15:04:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
onClickActions();
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
[
|
|
|
|
task.id,
|
|
|
|
task.title,
|
|
|
|
task.content,
|
|
|
|
task.time,
|
|
|
|
task.actionLabel,
|
|
|
|
task.isComplete,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
return hasFills ? (
|
|
|
|
<WooOnboardingTaskListItem.Slot
|
|
|
|
id={ task.id }
|
|
|
|
fillProps={ {
|
|
|
|
defaultTaskItem: DefaultTaskItem,
|
|
|
|
isComplete: task.isComplete,
|
|
|
|
...taskItemProps,
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<DefaultTaskItem />
|
|
|
|
);
|
|
|
|
};
|