2021-09-01 21:28:24 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import {
|
|
|
|
getHistory,
|
|
|
|
getNewPath,
|
|
|
|
updateQueryString,
|
|
|
|
} from '@woocommerce/navigation';
|
|
|
|
import {
|
|
|
|
ONBOARDING_STORE_NAME,
|
|
|
|
TaskType,
|
|
|
|
useUserPreferences,
|
|
|
|
} from '@woocommerce/data';
|
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2021-09-21 19:33:44 +00:00
|
|
|
import { TaskItem, useSlot } from '@woocommerce/experimental';
|
2021-09-01 21:28:24 +00:00
|
|
|
import { useCallback } from '@wordpress/element';
|
|
|
|
import { useDispatch } from '@wordpress/data';
|
2021-09-21 19:33:44 +00:00
|
|
|
import { WooOnboardingTaskListItem } from '@woocommerce/onboarding';
|
2021-09-01 21:28:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './task-list.scss';
|
|
|
|
|
|
|
|
export type TaskListItemProps = {
|
|
|
|
isExpandable: boolean;
|
|
|
|
isExpanded: boolean;
|
|
|
|
setExpandedTask: ( id: string ) => void;
|
|
|
|
task: TaskType;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const TaskListItem: React.FC< TaskListItemProps > = ( {
|
|
|
|
isExpandable = false,
|
|
|
|
isExpanded = false,
|
|
|
|
setExpandedTask,
|
|
|
|
task,
|
|
|
|
} ) => {
|
|
|
|
const { createNotice } = useDispatch( 'core/notices' );
|
2021-09-21 19:33:44 +00:00
|
|
|
|
2021-09-01 21:28:24 +00:00
|
|
|
const {
|
|
|
|
dismissTask,
|
|
|
|
snoozeTask,
|
|
|
|
undoDismissTask,
|
|
|
|
undoSnoozeTask,
|
|
|
|
} = useDispatch( ONBOARDING_STORE_NAME );
|
|
|
|
const userPreferences = useUserPreferences();
|
|
|
|
|
|
|
|
const {
|
|
|
|
actionLabel,
|
|
|
|
actionUrl,
|
|
|
|
content,
|
|
|
|
id,
|
|
|
|
isComplete,
|
|
|
|
isDismissable,
|
2021-09-29 17:55:44 +00:00
|
|
|
isSnoozeable,
|
2021-09-01 21:28:24 +00:00
|
|
|
time,
|
|
|
|
title,
|
2021-10-01 18:46:23 +00:00
|
|
|
level,
|
|
|
|
additionalInfo,
|
2021-09-01 21:28:24 +00:00
|
|
|
} = task;
|
|
|
|
|
2021-09-21 19:33:44 +00:00
|
|
|
const slot = useSlot( `woocommerce_onboarding_task_list_item_${ id }` );
|
|
|
|
const hasFills = Boolean( slot?.fills?.length );
|
|
|
|
|
2021-09-01 21:28:24 +00:00
|
|
|
const onDismiss = useCallback( () => {
|
2021-09-29 17:55:44 +00:00
|
|
|
dismissTask( id );
|
2021-09-01 21:28:24 +00:00
|
|
|
createNotice( 'success', __( 'Task dismissed' ), {
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
label: __( 'Undo', 'woocommerce-admin' ),
|
|
|
|
onClick: () => undoDismissTask( id ),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
} );
|
|
|
|
}, [ id ] );
|
|
|
|
|
|
|
|
const onSnooze = useCallback( () => {
|
2021-09-29 17:55:44 +00:00
|
|
|
snoozeTask( id );
|
2021-09-01 21:28:24 +00:00
|
|
|
createNotice(
|
|
|
|
'success',
|
|
|
|
__( 'Task postponed until tomorrow', 'woocommerce-admin' ),
|
|
|
|
{
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
label: __( 'Undo', 'woocommerce-admin' ),
|
|
|
|
onClick: () => undoSnoozeTask( id ),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}, [ id ] );
|
|
|
|
|
|
|
|
const getTaskStartedCount = () => {
|
|
|
|
const trackedStartedTasks =
|
|
|
|
userPreferences.task_list_tracked_started_tasks;
|
|
|
|
if ( ! trackedStartedTasks || ! trackedStartedTasks[ id ] ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return trackedStartedTasks[ id ];
|
|
|
|
};
|
|
|
|
|
|
|
|
// @todo This would be better as a task endpoint that handles updating the count.
|
|
|
|
const updateTrackStartedCount = () => {
|
|
|
|
const newCount = getTaskStartedCount() + 1;
|
|
|
|
const trackedStartedTasks =
|
|
|
|
userPreferences.task_list_tracked_started_tasks || {};
|
|
|
|
|
|
|
|
userPreferences.updateUserPreferences( {
|
|
|
|
task_list_tracked_started_tasks: {
|
|
|
|
...( trackedStartedTasks || {} ),
|
|
|
|
[ id ]: newCount,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2021-09-21 19:33:44 +00:00
|
|
|
const trackClick = () => {
|
2021-09-01 21:28:24 +00:00
|
|
|
recordEvent( 'tasklist_click', {
|
|
|
|
task_name: id,
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( ! isComplete ) {
|
|
|
|
updateTrackStartedCount();
|
|
|
|
}
|
2021-09-21 19:33:44 +00:00
|
|
|
};
|
2021-09-01 21:28:24 +00:00
|
|
|
|
2021-09-21 19:33:44 +00:00
|
|
|
const onClickDefault = useCallback( () => {
|
2021-09-01 21:28:24 +00:00
|
|
|
if ( actionUrl ) {
|
|
|
|
if ( actionUrl.startsWith( 'http' ) ) {
|
|
|
|
window.location.href = actionUrl;
|
|
|
|
} else {
|
|
|
|
getHistory().push( getNewPath( {}, actionUrl, {} ) );
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.document.documentElement.scrollTop = 0;
|
|
|
|
updateQueryString( { task: id } );
|
|
|
|
}, [ id, isComplete, actionUrl ] );
|
|
|
|
|
2021-09-21 19:33:44 +00:00
|
|
|
const taskItemProps = {
|
|
|
|
expandable: isExpandable,
|
|
|
|
expanded: isExpandable && isExpanded,
|
|
|
|
completed: isComplete,
|
2021-09-29 17:55:44 +00:00
|
|
|
onSnooze: isSnoozeable && onSnooze,
|
2021-09-21 19:33:44 +00:00
|
|
|
onDismiss: isDismissable && onDismiss,
|
|
|
|
};
|
|
|
|
|
|
|
|
const DefaultTaskItem = useCallback(
|
|
|
|
( props ) => {
|
|
|
|
const onClickActions = () => {
|
|
|
|
trackClick();
|
|
|
|
|
|
|
|
if ( props.onClick ) {
|
|
|
|
return props.onClick();
|
|
|
|
}
|
|
|
|
|
|
|
|
return onClickDefault();
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<TaskItem
|
|
|
|
key={ id }
|
|
|
|
title={ title }
|
|
|
|
content={ content }
|
2021-10-01 18:46:23 +00:00
|
|
|
additionalInfo={ additionalInfo }
|
2021-09-21 19:33:44 +00:00
|
|
|
time={ time }
|
|
|
|
action={ onClickActions }
|
2021-10-01 18:46:23 +00:00
|
|
|
level={ level }
|
2021-09-21 19:33:44 +00:00
|
|
|
actionLabel={ actionLabel }
|
|
|
|
{ ...taskItemProps }
|
|
|
|
{ ...props }
|
|
|
|
onClick={
|
|
|
|
! isExpandable || isComplete
|
|
|
|
? onClickActions
|
|
|
|
: () => setExpandedTask( id )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
[ id, title, content, time, actionLabel, isExpandable, isComplete ]
|
|
|
|
);
|
|
|
|
|
|
|
|
return hasFills ? (
|
|
|
|
<WooOnboardingTaskListItem.Slot
|
|
|
|
id={ id }
|
|
|
|
fillProps={ {
|
|
|
|
defaultTaskItem: DefaultTaskItem,
|
|
|
|
isComplete,
|
|
|
|
...taskItemProps,
|
|
|
|
} }
|
2021-09-01 21:28:24 +00:00
|
|
|
/>
|
2021-09-21 19:33:44 +00:00
|
|
|
) : (
|
2021-10-06 18:47:41 +00:00
|
|
|
<DefaultTaskItem onClick={ task.onClick } />
|
2021-09-01 21:28:24 +00:00
|
|
|
);
|
|
|
|
};
|