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';
|
|
|
|
|
2022-04-04 14:54:34 +00:00
|
|
|
const PurchaseTaskItem = ( { defaultTaskItem } ) => {
|
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">
|
2022-04-04 14:54:34 +00:00
|
|
|
{ ( { defaultTaskItem } ) => (
|
|
|
|
<PurchaseTaskItem defaultTaskItem={ defaultTaskItem } />
|
2021-09-21 19:33:44 +00:00
|
|
|
) }
|
|
|
|
</WooOnboardingTaskListItem>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
registerPlugin( 'woocommerce-admin-task-purchase', {
|
|
|
|
scope: 'woocommerce-tasks',
|
2022-04-04 14:54:34 +00:00
|
|
|
render: PurchaseTaskItemFill,
|
2021-09-21 19:33:44 +00:00
|
|
|
} );
|