2021-09-01 21:28:24 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __, _n, sprintf } from '@wordpress/i18n';
|
2022-06-14 18:31:50 +00:00
|
|
|
import { useEffect, useRef, useState, useContext } from '@wordpress/element';
|
2021-09-01 21:28:24 +00:00
|
|
|
import { Card, CardHeader } from '@wordpress/components';
|
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import { Badge } from '@woocommerce/components';
|
2022-04-13 14:20:26 +00:00
|
|
|
import {
|
|
|
|
getVisibleTasks,
|
|
|
|
ONBOARDING_STORE_NAME,
|
|
|
|
TaskListType,
|
|
|
|
} from '@woocommerce/data';
|
2021-09-01 21:28:24 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
import { Text, List, CollapsibleList } from '@woocommerce/experimental';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { TaskListItem } from './task-list-item';
|
|
|
|
import { TaskListMenu } from './task-list-menu';
|
|
|
|
import './task-list.scss';
|
2022-03-21 13:52:04 +00:00
|
|
|
import { ProgressHeader } from '~/task-lists/progress-header';
|
2022-06-14 18:31:50 +00:00
|
|
|
import { LayoutContext } from '~/layout';
|
2021-09-01 21:28:24 +00:00
|
|
|
|
|
|
|
export type TaskListProps = TaskListType & {
|
|
|
|
query: {
|
2022-03-15 17:40:22 +00:00
|
|
|
task?: string;
|
2021-09-01 21:28:24 +00:00
|
|
|
};
|
2022-04-07 19:56:20 +00:00
|
|
|
eventName?: string;
|
|
|
|
twoColumns?: boolean;
|
2022-04-08 18:37:22 +00:00
|
|
|
keepCompletedTaskList?: 'yes' | 'no';
|
2022-04-07 19:56:20 +00:00
|
|
|
cesHeader?: boolean;
|
2021-09-01 21:28:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const TaskList: React.FC< TaskListProps > = ( {
|
|
|
|
id,
|
2022-03-01 12:34:18 +00:00
|
|
|
eventPrefix,
|
2021-09-01 21:28:24 +00:00
|
|
|
tasks,
|
|
|
|
title: listTitle,
|
|
|
|
isCollapsible = false,
|
|
|
|
isExpandable = false,
|
2022-03-21 13:52:04 +00:00
|
|
|
displayProgressHeader = false,
|
2021-09-01 21:28:24 +00:00
|
|
|
query,
|
|
|
|
} ) => {
|
2022-04-25 05:49:11 +00:00
|
|
|
const { profileItems } = useSelect( ( select ) => {
|
2021-09-01 21:28:24 +00:00
|
|
|
const { getProfileItems } = select( ONBOARDING_STORE_NAME );
|
|
|
|
|
|
|
|
return {
|
|
|
|
profileItems: getProfileItems(),
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
const prevQueryRef = useRef( query );
|
2022-04-13 14:20:26 +00:00
|
|
|
const visibleTasks = getVisibleTasks( tasks );
|
2022-06-14 18:31:50 +00:00
|
|
|
const layoutContext = useContext( LayoutContext );
|
2021-09-01 21:28:24 +00:00
|
|
|
|
|
|
|
const incompleteTasks = tasks.filter(
|
2021-09-29 23:47:01 +00:00
|
|
|
( task ) => ! task.isComplete && ! task.isDismissed
|
2021-09-01 21:28:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const [ expandedTask, setExpandedTask ] = useState(
|
|
|
|
incompleteTasks[ 0 ]?.id
|
|
|
|
);
|
|
|
|
|
|
|
|
const recordTaskListView = () => {
|
2022-03-01 12:34:18 +00:00
|
|
|
recordEvent( eventPrefix + 'view', {
|
2021-09-01 21:28:24 +00:00
|
|
|
number_tasks: visibleTasks.length,
|
|
|
|
store_connected: profileItems.wccom_connected,
|
2022-06-14 18:31:50 +00:00
|
|
|
context: layoutContext.toString(),
|
2021-09-01 21:28:24 +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 ] );
|
|
|
|
|
|
|
|
if ( ! visibleTasks.length ) {
|
|
|
|
return <div className="woocommerce-task-dashboard__container"></div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const expandLabel = sprintf(
|
|
|
|
/* translators: %d = number of hidden tasks */
|
|
|
|
_n(
|
|
|
|
'Show %d more task.',
|
|
|
|
'Show %d more tasks.',
|
|
|
|
visibleTasks.length - 2,
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2021-09-01 21:28:24 +00:00
|
|
|
),
|
|
|
|
visibleTasks.length - 2
|
|
|
|
);
|
2022-03-30 09:00:04 +00:00
|
|
|
const collapseLabel = __( 'Show less', 'woocommerce' );
|
2021-09-01 21:28:24 +00:00
|
|
|
|
2022-04-20 08:48:53 +00:00
|
|
|
const taskListItems = visibleTasks.map( ( task ) => (
|
|
|
|
<TaskListItem
|
|
|
|
key={ task.id }
|
|
|
|
isExpanded={ expandedTask === task.id }
|
|
|
|
isExpandable={ isExpandable }
|
|
|
|
task={ task }
|
|
|
|
setExpandedTask={ setExpandedTask }
|
|
|
|
/>
|
|
|
|
) );
|
2021-09-01 21:28:24 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-03-15 17:40:22 +00:00
|
|
|
<div
|
|
|
|
className={
|
|
|
|
'woocommerce-task-dashboard__container woocommerce-task-list__' +
|
|
|
|
id
|
|
|
|
}
|
|
|
|
>
|
2022-03-21 13:52:04 +00:00
|
|
|
{ displayProgressHeader ? (
|
|
|
|
<ProgressHeader taskListId={ id } />
|
|
|
|
) : null }
|
2021-09-01 21:28:24 +00:00
|
|
|
<Card
|
|
|
|
size="large"
|
|
|
|
className="woocommerce-task-card woocommerce-homescreen-card"
|
|
|
|
>
|
|
|
|
<CardHeader size="medium">
|
|
|
|
<div className="wooocommerce-task-card__header">
|
|
|
|
<Text
|
|
|
|
size="20"
|
|
|
|
lineHeight="28px"
|
|
|
|
variant="title.small"
|
|
|
|
>
|
|
|
|
{ listTitle }
|
|
|
|
</Text>
|
|
|
|
<Badge count={ incompleteTasks.length } />
|
|
|
|
</div>
|
|
|
|
<TaskListMenu id={ id } />
|
|
|
|
</CardHeader>
|
2022-04-20 08:48:53 +00:00
|
|
|
{ isCollapsible ? (
|
|
|
|
<CollapsibleList
|
|
|
|
animation="custom"
|
|
|
|
collapseLabel={ collapseLabel }
|
|
|
|
expandLabel={ expandLabel }
|
|
|
|
show={ 2 }
|
|
|
|
onCollapse={ () =>
|
|
|
|
recordEvent( eventPrefix + 'collapse', {} )
|
|
|
|
}
|
|
|
|
onExpand={ () =>
|
|
|
|
recordEvent( eventPrefix + 'expand', {} )
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{ taskListItems }
|
|
|
|
</CollapsibleList>
|
|
|
|
) : (
|
|
|
|
<List animation="custom">{ taskListItems }</List>
|
|
|
|
) }
|
2021-09-01 21:28:24 +00:00
|
|
|
</Card>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2022-03-15 17:40:22 +00:00
|
|
|
|
|
|
|
export default TaskList;
|