2021-09-21 19:33:44 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-03-10 14:58:02 +00:00
|
|
|
import {
|
|
|
|
WooHeaderNavigationItem,
|
|
|
|
WooHeaderPageTitle,
|
|
|
|
} from '@woocommerce/admin-layout';
|
2021-09-21 19:33:44 +00:00
|
|
|
import { WooOnboardingTask } from '@woocommerce/onboarding';
|
2021-10-01 19:53:22 +00:00
|
|
|
import { getHistory, getNewPath } from '@woocommerce/navigation';
|
2021-10-06 18:47:41 +00:00
|
|
|
import { ONBOARDING_STORE_NAME, TaskType } from '@woocommerce/data';
|
2021-10-01 19:53:22 +00:00
|
|
|
import { useCallback } from '@wordpress/element';
|
|
|
|
import { useDispatch } from '@wordpress/data';
|
2021-12-14 16:56:42 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { BackButton } from './back-button';
|
|
|
|
|
2021-09-01 21:28:24 +00:00
|
|
|
export type TaskProps = {
|
2022-04-20 08:48:53 +00:00
|
|
|
query: { task?: string };
|
|
|
|
task: TaskType;
|
2021-09-01 21:28:24 +00:00
|
|
|
};
|
|
|
|
|
2021-10-06 18:47:41 +00:00
|
|
|
export const Task: React.FC< TaskProps > = ( { query, task } ) => {
|
2022-05-23 05:27:11 +00:00
|
|
|
const id = query.task || '';
|
|
|
|
if ( ! id ) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.warn( 'No task id provided' );
|
|
|
|
// eslint-enable-next-line no-console
|
|
|
|
}
|
|
|
|
|
2022-06-21 08:37:34 +00:00
|
|
|
const { invalidateResolutionForStoreSelector, optimisticallyCompleteTask } =
|
|
|
|
useDispatch( ONBOARDING_STORE_NAME );
|
2021-10-01 19:53:22 +00:00
|
|
|
|
2022-04-25 22:13:44 +00:00
|
|
|
const updateBadge = useCallback( () => {
|
2023-08-01 10:02:09 +00:00
|
|
|
const badgeElements: Array< HTMLElement > | null = Array.from(
|
|
|
|
document.querySelectorAll(
|
|
|
|
'#adminmenu .woocommerce-task-list-remaining-tasks-badge'
|
|
|
|
)
|
2022-04-25 22:13:44 +00:00
|
|
|
);
|
|
|
|
|
2023-08-01 10:02:09 +00:00
|
|
|
if ( ! badgeElements?.length ) {
|
2022-04-25 22:13:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-01 10:02:09 +00:00
|
|
|
badgeElements.forEach( ( badgeElement ) => {
|
|
|
|
const currentBadgeCount = Number( badgeElement.innerText );
|
2022-04-27 20:39:01 +00:00
|
|
|
|
2023-08-01 10:02:09 +00:00
|
|
|
if ( currentBadgeCount === 1 ) {
|
|
|
|
badgeElement.remove();
|
|
|
|
} else {
|
|
|
|
badgeElement.innerHTML = String( currentBadgeCount - 1 );
|
|
|
|
}
|
|
|
|
} );
|
2022-04-25 22:13:44 +00:00
|
|
|
}, [] );
|
|
|
|
|
2022-03-02 01:45:44 +00:00
|
|
|
const onComplete = useCallback(
|
|
|
|
( options ) => {
|
|
|
|
optimisticallyCompleteTask( id );
|
2022-05-30 06:51:33 +00:00
|
|
|
getHistory().push(
|
2022-03-02 01:45:44 +00:00
|
|
|
options && options.redirectPath
|
|
|
|
? options.redirectPath
|
|
|
|
: getNewPath( {}, '/', {} )
|
|
|
|
);
|
|
|
|
invalidateResolutionForStoreSelector( 'getTaskLists' );
|
2022-04-25 22:13:44 +00:00
|
|
|
updateBadge();
|
2022-03-02 01:45:44 +00:00
|
|
|
},
|
|
|
|
[ id ]
|
|
|
|
);
|
2021-10-01 19:53:22 +00:00
|
|
|
|
|
|
|
return (
|
2021-12-14 16:56:42 +00:00
|
|
|
<>
|
|
|
|
<WooHeaderNavigationItem>
|
|
|
|
<BackButton title={ task.title } />
|
|
|
|
</WooHeaderNavigationItem>
|
|
|
|
<WooHeaderPageTitle>{ task.title }</WooHeaderPageTitle>
|
|
|
|
<WooOnboardingTask.Slot
|
|
|
|
id={ id }
|
|
|
|
fillProps={ { onComplete, query, task } }
|
|
|
|
/>
|
|
|
|
</>
|
2021-10-01 19:53:22 +00:00
|
|
|
);
|
|
|
|
};
|