Skip to content

Commit

Permalink
Add actions for each status
Browse files Browse the repository at this point in the history
  • Loading branch information
IanRamosC committed Dec 23, 2024
1 parent 5a07845 commit 60f51b1
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import styles from './style.module.scss';
import usePricingData from './use-pricing-data';

const RecommendationActions = ( { slug }: { slug: string } ) => {
const { secondaryAction, purchaseAction, isActivating } = usePricingData( slug );
const { secondaryAction, primaryAction, isActivating } = usePricingData( slug );

return (
<div className={ styles.actions }>
<div className={ clsx( styles.buttons, styles.upsell ) }>
{ purchaseAction && (
<Button size="small" { ...purchaseAction }>
{ purchaseAction.label }
{ primaryAction && (
<Button size="small" { ...primaryAction }>
{ primaryAction.label }
</Button>
) }
{ secondaryAction && (
<Button size="small" variant="secondary" disabled={ isActivating } { ...secondaryAction }>
{ secondaryAction.label }
</Button>
) }
<Button size="small" variant="secondary" disabled={ isActivating } { ...secondaryAction }>
{ secondaryAction.label }
</Button>
</div>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ProductCamelCase } from '../../data/types';
import { getMyJetpackWindowInitialState } from '../../data/utils/get-my-jetpack-window-state';
import useAnalytics from '../../hooks/use-analytics';
import useMyJetpackConnection from '../../hooks/use-my-jetpack-connection';
import useInstallStandalonePlugin from '../../data/products/use-install-standalone-plugin';

const parsePricingData = ( pricingForUi: ProductCamelCase[ 'pricingForUi' ] ) => {
const { tiers, wpcomFreeProductSlug, introductoryOffer } = pricingForUi;
Expand Down Expand Up @@ -52,20 +53,51 @@ const parsePricingData = ( pricingForUi: ProductCamelCase[ 'pricingForUi' ] ) =>
};
};

const getPurchaseAction = ( detail: ProductCamelCase, onCheckout: () => void ) => {
// type for onCheckout and onActivate
type Actions = {
onCheckout: () => void;
onActivate: () => void;
onInstall: () => void;
onManage: () => void;
};

const getPrimaryAction = (
detail: ProductCamelCase,
{ onCheckout, onActivate, onInstall, onManage }: Actions
) => {
const isUpgradable =
detail.status === PRODUCT_STATUSES.ACTIVE &&
( detail.isUpgradableByBundle.length || detail.isUpgradable );
const upgradeHasPrice =
detail.pricingForUi.fullPrice || detail.pricingForUi.tiers?.upgraded?.fullPrice;

if ( detail.status === PRODUCT_STATUSES.MODULE_DISABLED ) {
return { label: __( 'Activate', 'jetpack-my-jetpack' ), onClick: onActivate };
}

if ( detail.status === PRODUCT_STATUSES.ABSENT ) {
return { label: __( 'Install', 'jetpack-my-jetpack' ), onClick: onInstall };
}

if ( detail.status === PRODUCT_STATUSES.USER_CONNECTION_ERROR ) {
return { label: __( 'Connect', 'jetpack-my-jetpack' ), href: '#/connection' };
}

if ( detail.status === PRODUCT_STATUSES.CAN_UPGRADE || isUpgradable ) {
if ( upgradeHasPrice ) {
return { label: __( 'Upgrade', 'jetpack-my-jetpack' ), onClick: onCheckout };
}
return null;
}

if ( detail.isFeature ) {
return {
label: __( 'Manage', 'jetpack-my-jetpack' ),
href: detail.manageUrl,
onClick: onCheckout,
};
}

return { label: __( 'Purchase', 'jetpack-my-jetpack' ), onClick: onCheckout };
};

Expand Down Expand Up @@ -98,6 +130,7 @@ const usePricingData = ( slug: string ) => {
const { wpcomProductSlug, wpcomFreeProductSlug, ...data } = parsePricingData(
detail.pricingForUi
);
const { install: installPlugin, isPending: isInstalling } = useInstallStandalonePlugin( slug );

const { isUserConnected } = useMyJetpackConnection();
const { myJetpackUrl, siteSuffix } = getMyJetpackWindowInitialState();
Expand Down Expand Up @@ -135,9 +168,27 @@ const usePricingData = ( slug: string ) => {
runCheckout();
}, [ activate, recordEvent, runCheckout, slug ] );

const handleInstall = useCallback( () => {
recordEvent( 'jetpack_myjetpack_evaluation_recommendations_install_plugin_click', {
product: slug,
} );
installPlugin();
}, [ slug, installPlugin, recordEvent ] );

const handleManage = useCallback( () => {
recordEvent( 'jetpack_myjetpack_evaluation_recommendations_manage_click', {
product: slug,
} );
}, [ slug, recordEvent ] );

return {
secondaryAction: getSecondaryAction( detail, handleActivate ),
purchaseAction: getPurchaseAction( detail, handleCheckout ),
primaryAction: getPrimaryAction( detail, {
onCheckout: handleCheckout,
onActivate: handleActivate,
onInstall: handleInstall,
onManage: handleManage,
} ),
isActivating,
...data,
};
Expand Down
1 change: 1 addition & 0 deletions projects/packages/my-jetpack/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ interface Window {
has_paid_plan_for_product: boolean;
features_by_tier: Array< string >;
is_bundle: boolean;
is_feature: boolean;
is_plugin_active: boolean;
is_upgradable: boolean;
is_upgradable_by_bundle: string[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ abstract class Module_Product extends Product {
*/
public static $module_name = null;

/**
* Whether this module is a Jetpack feature
*
* @var boolean
*/
public static $is_feature = false;

/**
* Get the plugin slug - ovewrite it ans return Jetpack's
*
Expand Down
8 changes: 8 additions & 0 deletions projects/packages/my-jetpack/src/products/class-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ abstract class Product {
*/
const EXPIRATION_CUTOFF_TIME = '+2 months';

/**
* Whether this module is a Jetpack feature
*
* @var boolean
*/
public static $is_feature = false;

/**
* Whether this product requires a site connection
*
Expand Down Expand Up @@ -182,6 +189,7 @@ public static function get_info() {
'is_plugin_active' => static::is_plugin_active(),
'is_upgradable' => static::is_upgradable(),
'is_upgradable_by_bundle' => static::is_upgradable_by_bundle(),
'is_feature' => static::$is_feature,
'supported_products' => static::get_supported_products(),
'wpcom_product_slug' => static::get_wpcom_product_slug(),
'requires_user_connection' => static::$requires_user_connection,
Expand Down

0 comments on commit 60f51b1

Please sign in to comment.