2022-12-09 16:41:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useState } from '@wordpress/element';
|
|
|
|
import { useDispatch } from '@wordpress/data';
|
|
|
|
import { Button } from '@wordpress/components';
|
|
|
|
import { Pill } from '@woocommerce/components';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
import { PLUGINS_STORE_NAME } from '@woocommerce/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { PluginCardBody } from '~/marketing/components';
|
|
|
|
import { RecommendedPlugin } from '~/marketing/types';
|
2024-02-20 11:44:28 +00:00
|
|
|
import { getRecommendationSource } from '~/marketing/utils';
|
2022-12-09 16:41:00 +00:00
|
|
|
import { getInAppPurchaseUrl } from '~/lib/in-app-purchase';
|
|
|
|
import { createNoticesFromResponse } from '~/lib/notices';
|
2023-01-19 17:36:39 +00:00
|
|
|
import { useIsPluginInstalledNotActivated } from './useIsPluginInstalledNotActivated';
|
2022-12-09 16:41:00 +00:00
|
|
|
import './PluginCardBody.scss';
|
|
|
|
|
|
|
|
type SmartPluginCardBodyProps = {
|
|
|
|
plugin: RecommendedPlugin;
|
2023-03-17 16:47:54 +00:00
|
|
|
onInstalledAndActivated?: ( pluginSlug: string ) => void;
|
2022-12-09 16:41:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A smart wrapper around PluginCardBody that accepts a `plugin` prop.
|
|
|
|
*
|
|
|
|
* It knows how to render the action button for the plugin,
|
|
|
|
* and has the logic for installing and activating plugin.
|
|
|
|
* This allows users to install and activate multiple plugins at the same time.
|
|
|
|
*/
|
|
|
|
export const SmartPluginCardBody = ( {
|
|
|
|
plugin,
|
|
|
|
onInstalledAndActivated = () => {},
|
|
|
|
}: SmartPluginCardBodyProps ) => {
|
|
|
|
const [ currentPlugin, setCurrentPlugin ] = useState< string | null >(
|
|
|
|
null
|
|
|
|
);
|
|
|
|
const { installAndActivatePlugins } = useDispatch( PLUGINS_STORE_NAME );
|
2023-01-19 17:36:39 +00:00
|
|
|
const { isPluginInstalledNotActivated } =
|
|
|
|
useIsPluginInstalledNotActivated();
|
2022-12-09 16:41:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Install and activate a plugin.
|
|
|
|
*
|
|
|
|
* When the process is successful, `onInstalledAndActivated` will be called.
|
|
|
|
* A success notice will be displayed.
|
|
|
|
*
|
|
|
|
* When the process is not successful, an error notice will be displayed.
|
|
|
|
*/
|
|
|
|
const installAndActivate = async () => {
|
|
|
|
setCurrentPlugin( plugin.product );
|
|
|
|
|
|
|
|
try {
|
|
|
|
recordEvent( 'marketing_recommended_extension', {
|
|
|
|
name: plugin.title,
|
2024-02-20 11:44:28 +00:00
|
|
|
source: getRecommendationSource(),
|
2022-12-09 16:41:00 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
const response = await installAndActivatePlugins( [
|
|
|
|
plugin.product,
|
|
|
|
] );
|
|
|
|
|
2023-03-17 16:47:54 +00:00
|
|
|
onInstalledAndActivated( plugin.product );
|
2022-12-09 16:41:00 +00:00
|
|
|
createNoticesFromResponse( response );
|
|
|
|
} catch ( error ) {
|
|
|
|
createNoticesFromResponse( error );
|
|
|
|
}
|
|
|
|
|
|
|
|
setCurrentPlugin( null );
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderButton = () => {
|
|
|
|
const buttonDisabled = !! currentPlugin;
|
|
|
|
|
2023-01-19 17:36:39 +00:00
|
|
|
if ( isPluginInstalledNotActivated( plugin.product ) ) {
|
2023-01-19 16:36:34 +00:00
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
variant="secondary"
|
|
|
|
isBusy={ currentPlugin === plugin.product }
|
|
|
|
disabled={ buttonDisabled }
|
|
|
|
onClick={ installAndActivate }
|
|
|
|
>
|
|
|
|
{ __( 'Activate', 'woocommerce' ) }
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-09 16:41:00 +00:00
|
|
|
if ( plugin.direct_install ) {
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
variant="secondary"
|
|
|
|
isBusy={ currentPlugin === plugin.product }
|
|
|
|
disabled={ buttonDisabled }
|
|
|
|
onClick={ installAndActivate }
|
|
|
|
>
|
2023-07-06 15:50:56 +00:00
|
|
|
{ __( 'Install extension', 'woocommerce' ) }
|
2022-12-09 16:41:00 +00:00
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
variant="secondary"
|
|
|
|
href={ getInAppPurchaseUrl( plugin.url ) }
|
|
|
|
disabled={ buttonDisabled }
|
|
|
|
onClick={ () => {
|
|
|
|
recordEvent( 'marketing_recommended_extension', {
|
|
|
|
name: plugin.title,
|
2024-02-20 11:44:28 +00:00
|
|
|
source: getRecommendationSource(),
|
2022-12-09 16:41:00 +00:00
|
|
|
} );
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
{ __( 'View details', 'woocommerce' ) }
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PluginCardBody
|
|
|
|
icon={ <img src={ plugin.icon } alt={ plugin.title } /> }
|
|
|
|
name={ plugin.title }
|
|
|
|
pills={ plugin.tags.map( ( tag ) => (
|
|
|
|
<Pill key={ tag.slug }>{ tag.name }</Pill>
|
|
|
|
) ) }
|
|
|
|
description={ plugin.description }
|
|
|
|
button={ renderButton() }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|