2021-09-21 19:33:44 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import React from 'react';
|
|
|
|
import { registerPlugin } from '@wordpress/plugins';
|
|
|
|
import { WooOnboardingTaskListItem } from '@woocommerce/onboarding';
|
|
|
|
import { useState, useCallback } from '@wordpress/element';
|
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import { ONBOARDING_STORE_NAME, PLUGINS_STORE_NAME } from '@woocommerce/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import CartModal from '../../dashboard/components/cart-modal';
|
|
|
|
import { getCategorizedOnboardingProducts } from '../../dashboard/utils';
|
|
|
|
|
2023-05-09 03:28:57 +00:00
|
|
|
type PurchaseTaskItemProps = {
|
|
|
|
defaultTaskItem: React.ComponentType< {
|
|
|
|
onClick: () => void;
|
|
|
|
} >;
|
|
|
|
};
|
|
|
|
|
|
|
|
const PurchaseTaskItem = ( { defaultTaskItem }: PurchaseTaskItemProps ) => {
|
2021-09-21 19:33:44 +00:00
|
|
|
const [ cartModalOpen, setCartModalOpen ] = useState( false );
|
|
|
|
|
2021-10-13 16:15:47 +00:00
|
|
|
const { installedPlugins, productTypes, profileItems } = useSelect(
|
|
|
|
( select ) => {
|
|
|
|
const { getProductTypes, getProfileItems } = select(
|
|
|
|
ONBOARDING_STORE_NAME
|
|
|
|
);
|
|
|
|
const { getInstalledPlugins } = select( PLUGINS_STORE_NAME );
|
2021-09-21 19:33:44 +00:00
|
|
|
|
2021-10-13 16:15:47 +00:00
|
|
|
return {
|
|
|
|
installedPlugins: getInstalledPlugins(),
|
|
|
|
productTypes: getProductTypes(),
|
|
|
|
profileItems: getProfileItems(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
2021-09-21 19:33:44 +00:00
|
|
|
|
|
|
|
const toggleCartModal = useCallback( () => {
|
|
|
|
if ( ! cartModalOpen ) {
|
|
|
|
recordEvent( 'tasklist_purchase_extensions' );
|
|
|
|
}
|
|
|
|
|
|
|
|
setCartModalOpen( ! cartModalOpen );
|
|
|
|
}, [ cartModalOpen ] );
|
|
|
|
|
|
|
|
const groupedProducts = getCategorizedOnboardingProducts(
|
2021-10-13 16:15:47 +00:00
|
|
|
productTypes,
|
2021-09-21 19:33:44 +00:00
|
|
|
profileItems,
|
|
|
|
installedPlugins
|
|
|
|
);
|
|
|
|
const { remainingProducts } = groupedProducts;
|
2022-04-04 14:54:34 +00:00
|
|
|
const DefaultTaskItem = defaultTaskItem;
|
2021-09-21 19:33:44 +00:00
|
|
|
|
2022-04-04 14:54:34 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<DefaultTaskItem
|
|
|
|
onClick={ () => {
|
|
|
|
if ( remainingProducts.length ) {
|
|
|
|
toggleCartModal();
|
|
|
|
}
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
{ cartModalOpen && (
|
|
|
|
<CartModal
|
|
|
|
onClose={ () => toggleCartModal() }
|
|
|
|
onClickPurchaseLater={ () => toggleCartModal() }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const PurchaseTaskItemFill = () => {
|
2021-09-21 19:33:44 +00:00
|
|
|
return (
|
|
|
|
<WooOnboardingTaskListItem id="purchase">
|
2023-05-09 03:28:57 +00:00
|
|
|
{ ( { defaultTaskItem }: PurchaseTaskItemProps ) => (
|
2022-04-04 14:54:34 +00:00
|
|
|
<PurchaseTaskItem defaultTaskItem={ defaultTaskItem } />
|
2021-09-21 19:33:44 +00:00
|
|
|
) }
|
|
|
|
</WooOnboardingTaskListItem>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
registerPlugin( 'woocommerce-admin-task-purchase', {
|
2023-05-09 03:28:57 +00:00
|
|
|
// @ts-expect-error 'scope' does exist. @types/wordpress__plugins is outdated.
|
2021-09-21 19:33:44 +00:00
|
|
|
scope: 'woocommerce-tasks',
|
2022-04-04 14:54:34 +00:00
|
|
|
render: PurchaseTaskItemFill,
|
2021-09-21 19:33:44 +00:00
|
|
|
} );
|