2022-03-21 17:04:15 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-06-14 18:31:50 +00:00
|
|
|
import { useEffect, useRef, useState, useContext } from '@wordpress/element';
|
2022-03-21 17:04:15 +00:00
|
|
|
import { Panel, PanelBody, PanelRow } from '@wordpress/components';
|
|
|
|
import { useSelect, useDispatch } from '@wordpress/data';
|
2022-06-01 17:10:20 +00:00
|
|
|
import { ONBOARDING_STORE_NAME, getVisibleTasks } from '@woocommerce/data';
|
2022-03-21 17:04:15 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2022-04-22 15:04:51 +00:00
|
|
|
import { List } from '@woocommerce/experimental';
|
2022-03-21 17:04:15 +00:00
|
|
|
import classnames from 'classnames';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import '../tasks/task-list.scss';
|
|
|
|
import './sectioned-task-list.scss';
|
|
|
|
import TaskListCompleted from './completed';
|
|
|
|
import { TaskListProps } from '~/tasks/task-list';
|
2022-03-24 19:56:27 +00:00
|
|
|
import { ProgressHeader } from '~/task-lists/progress-header';
|
2022-04-13 14:20:26 +00:00
|
|
|
import { SectionPanelTitle } from './section-panel-title';
|
2022-04-22 15:04:51 +00:00
|
|
|
import { TaskListItem } from './task-list-item';
|
2022-05-16 13:03:46 +00:00
|
|
|
import { TaskListCompletedHeader } from './completed-header';
|
2022-06-14 18:31:50 +00:00
|
|
|
import { LayoutContext } from '~/layout';
|
2022-03-21 17:04:15 +00:00
|
|
|
|
|
|
|
type PanelBodyProps = Omit< PanelBody.Props, 'title' | 'onToggle' > & {
|
|
|
|
title: string | React.ReactNode | undefined;
|
|
|
|
onToggle?: ( isOpen: boolean ) => void;
|
|
|
|
};
|
2022-06-21 08:37:34 +00:00
|
|
|
const PanelBodyWithUpdatedType =
|
|
|
|
PanelBody as React.ComponentType< PanelBodyProps >;
|
2022-03-21 17:04:15 +00:00
|
|
|
|
|
|
|
export const SectionedTaskList: React.FC< TaskListProps > = ( {
|
|
|
|
query,
|
|
|
|
id,
|
2022-04-14 16:21:56 +00:00
|
|
|
eventPrefix,
|
2022-03-21 17:04:15 +00:00
|
|
|
tasks,
|
|
|
|
keepCompletedTaskList,
|
|
|
|
isComplete,
|
|
|
|
sections,
|
2022-03-24 19:56:27 +00:00
|
|
|
displayProgressHeader,
|
2022-04-21 19:40:48 +00:00
|
|
|
cesHeader = true,
|
2022-03-21 17:04:15 +00:00
|
|
|
} ) => {
|
2022-04-25 05:49:11 +00:00
|
|
|
const { profileItems } = useSelect( ( select ) => {
|
2022-03-21 17:04:15 +00:00
|
|
|
const { getProfileItems } = select( ONBOARDING_STORE_NAME );
|
|
|
|
return {
|
|
|
|
profileItems: getProfileItems(),
|
|
|
|
};
|
|
|
|
} );
|
2022-06-21 08:37:34 +00:00
|
|
|
const { hideTaskList, keepCompletedTaskList: keepCompletedTasks } =
|
|
|
|
useDispatch( ONBOARDING_STORE_NAME );
|
2022-03-21 17:04:15 +00:00
|
|
|
const [ openPanel, setOpenPanel ] = useState< string | null >(
|
|
|
|
sections?.find( ( section ) => ! section.isComplete )?.id || null
|
|
|
|
);
|
2022-06-14 18:31:50 +00:00
|
|
|
const layoutContext = useContext( LayoutContext );
|
2022-03-21 17:04:15 +00:00
|
|
|
|
|
|
|
const prevQueryRef = useRef( query );
|
|
|
|
|
2022-04-13 14:20:26 +00:00
|
|
|
const visibleTasks = getVisibleTasks( tasks );
|
2022-03-21 17:04:15 +00:00
|
|
|
|
|
|
|
const recordTaskListView = () => {
|
|
|
|
if ( query.task ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-14 16:21:56 +00:00
|
|
|
recordEvent( `${ eventPrefix }view`, {
|
2022-03-21 17:04:15 +00:00
|
|
|
number_tasks: visibleTasks.length,
|
|
|
|
store_connected: profileItems.wccom_connected,
|
2022-06-14 18:31:50 +00:00
|
|
|
context: layoutContext.toString(),
|
2022-03-21 17:04:15 +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 hideTasks = () => {
|
|
|
|
hideTaskList( id );
|
|
|
|
};
|
|
|
|
|
|
|
|
const keepTasks = () => {
|
2022-05-27 16:41:16 +00:00
|
|
|
keepCompletedTasks( id );
|
2022-03-21 17:04:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let selectedHeaderCard = visibleTasks.find(
|
|
|
|
( listTask ) => listTask.isComplete === false
|
|
|
|
);
|
|
|
|
|
|
|
|
// If nothing is selected, default to the last task since everything is completed.
|
|
|
|
if ( ! selectedHeaderCard ) {
|
|
|
|
selectedHeaderCard = visibleTasks[ visibleTasks.length - 1 ];
|
|
|
|
}
|
|
|
|
|
|
|
|
const getSectionTasks = ( sectionTaskIds: string[] ) => {
|
|
|
|
return visibleTasks.filter( ( task ) =>
|
|
|
|
sectionTaskIds.includes( task.id )
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( ! visibleTasks.length ) {
|
|
|
|
return <div className="woocommerce-task-dashboard__container"></div>;
|
|
|
|
}
|
|
|
|
|
2022-04-21 19:40:48 +00:00
|
|
|
if ( isComplete && keepCompletedTaskList !== 'yes' ) {
|
2022-03-21 17:04:15 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-04-21 19:40:48 +00:00
|
|
|
{ cesHeader ? (
|
2022-05-16 13:03:46 +00:00
|
|
|
<TaskListCompletedHeader
|
2022-04-21 19:40:48 +00:00
|
|
|
hideTasks={ hideTasks }
|
|
|
|
keepTasks={ keepTasks }
|
2022-05-17 17:25:06 +00:00
|
|
|
customerEffortScore={ true }
|
2022-04-21 19:40:48 +00:00
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<TaskListCompleted
|
|
|
|
hideTasks={ hideTasks }
|
|
|
|
keepTasks={ keepTasks }
|
|
|
|
twoColumns={ false }
|
|
|
|
/>
|
|
|
|
) }
|
2022-03-21 17:04:15 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-03-24 19:56:27 +00:00
|
|
|
{ displayProgressHeader ? (
|
|
|
|
<ProgressHeader taskListId={ id } />
|
|
|
|
) : null }
|
2022-03-21 17:04:15 +00:00
|
|
|
<div
|
|
|
|
className={ classnames(
|
|
|
|
`woocommerce-task-dashboard__container woocommerce-sectioned-task-list two-column-experiment woocommerce-task-list__${ id }`
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
<Panel>
|
|
|
|
{ ( sections || [] ).map( ( section ) => (
|
|
|
|
<PanelBodyWithUpdatedType
|
|
|
|
key={ section.id }
|
2022-04-13 14:20:26 +00:00
|
|
|
title={
|
|
|
|
<SectionPanelTitle
|
|
|
|
section={ section }
|
|
|
|
tasks={ tasks }
|
|
|
|
active={ openPanel === section.id }
|
|
|
|
/>
|
|
|
|
}
|
2022-03-21 17:04:15 +00:00
|
|
|
opened={ openPanel === section.id }
|
|
|
|
onToggle={ ( isOpen: boolean ) => {
|
|
|
|
if ( ! isOpen && openPanel === section.id ) {
|
2022-04-14 16:21:56 +00:00
|
|
|
recordEvent(
|
|
|
|
`${ eventPrefix }section_closed`,
|
|
|
|
{
|
|
|
|
id: section.id,
|
|
|
|
all: true,
|
|
|
|
}
|
|
|
|
);
|
2022-03-21 17:04:15 +00:00
|
|
|
setOpenPanel( null );
|
|
|
|
} else {
|
2022-04-14 16:21:56 +00:00
|
|
|
if ( openPanel ) {
|
|
|
|
recordEvent(
|
|
|
|
`${ eventPrefix }section_closed`,
|
|
|
|
{
|
|
|
|
id: openPanel,
|
|
|
|
all: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2022-03-21 17:04:15 +00:00
|
|
|
setOpenPanel( section.id );
|
|
|
|
}
|
2022-04-14 16:21:56 +00:00
|
|
|
if ( isOpen ) {
|
|
|
|
recordEvent(
|
|
|
|
`${ eventPrefix }section_opened`,
|
|
|
|
{
|
|
|
|
id: section.id,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2022-03-21 17:04:15 +00:00
|
|
|
} }
|
|
|
|
initialOpen={ false }
|
|
|
|
>
|
|
|
|
<PanelRow>
|
|
|
|
<List animation="custom">
|
|
|
|
{ getSectionTasks( section.tasks ).map(
|
2022-04-22 15:04:51 +00:00
|
|
|
( task ) => (
|
|
|
|
<TaskListItem
|
|
|
|
key={ task.id }
|
|
|
|
task={ task }
|
|
|
|
eventPrefix={ eventPrefix }
|
|
|
|
/>
|
|
|
|
)
|
2022-03-21 17:04:15 +00:00
|
|
|
) }
|
|
|
|
</List>
|
|
|
|
</PanelRow>
|
|
|
|
</PanelBodyWithUpdatedType>
|
|
|
|
) ) }
|
|
|
|
</Panel>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SectionedTaskList;
|