woocommerce/plugins/woocommerce-admin/client/tasks/deprecated-tasks.tsx

65 lines
1.6 KiB
TypeScript
Raw Normal View History

/**
* External dependencies
*/
import { registerPlugin } from '@wordpress/plugins';
import { WooOnboardingTask } from '@woocommerce/onboarding';
import { useSelect } from '@wordpress/data';
import {
ONBOARDING_STORE_NAME,
TaskType,
DeprecatedTaskType,
} from '@woocommerce/data';
import { useEffect, useState } from '@wordpress/element';
type MergedTask = TaskType & DeprecatedTaskType;
2022-04-21 04:44:19 +00:00
const DeprecatedWooOnboardingTaskFills = () => {
const [ deprecatedTasks, setDeprecatedTasks ] = useState< MergedTask[] >(
[]
);
const { isResolving, taskLists } = useSelect( ( select ) => {
return {
isResolving: select( ONBOARDING_STORE_NAME ).isResolving(
'getTaskLists'
),
taskLists: select( ONBOARDING_STORE_NAME ).getTaskLists(),
};
} );
useEffect( () => {
if ( taskLists && taskLists.length > 0 ) {
const deprecatedTasksWithContainer: MergedTask[] = [];
for ( const tasklist of taskLists ) {
for ( const task of tasklist.tasks ) {
if (
( task as MergedTask ).isDeprecated &&
( task as MergedTask ).container
) {
deprecatedTasksWithContainer.push( task as MergedTask );
}
}
}
setDeprecatedTasks( deprecatedTasksWithContainer );
}
}, [ taskLists ] );
if ( isResolving ) {
return null;
}
return (
<>
{ deprecatedTasks.map( ( task ) => (
<WooOnboardingTask id={ task.id } key={ task.id }>
2022-04-21 04:44:19 +00:00
{ () => task.container }
</WooOnboardingTask>
) ) }
</>
);
};
registerPlugin( 'wc-admin-deprecated-task-container', {
2022-04-21 04:44:19 +00:00
// @ts-expect-error @types/wordpress__plugins need to be updated
scope: 'woocommerce-tasks',
render: () => <DeprecatedWooOnboardingTaskFills />,
} );