/**
* WordPress dependencies
*/
import { Button, ExternalLink } from '@safe-wordpress/components';
import { useSelect, useDispatch } from '@safe-wordpress/data';
import { createInterpolateElement, useState } from '@safe-wordpress/element';
import { _x, sprintf } from '@safe-wordpress/i18n';
/**
* External dependencies
*/
import {
QuotaMeter,
QuotaPurchasePopover,
ConfirmationDialog,
} from '@nab/components';
import { store as NAB_DATA } from '@nab/data';
import { formatI18nDate } from '@nab/date';
import { getSubscribeLabel, getSubscribeLink } from '@nab/utils';
import type { SiteId } from '@nab/types';
/**
* Internal dependencies
*/
import './style.scss';
import { AgencyPlan } from './agency-plan';
import { AddonsPopover } from '../addons-popover';
import { LicensePopover } from '../license-popover';
import { UpgradePopover } from '../upgrade-popover';
import { store as NAB_ACCOUNT } from '../../store';
export type PlanProps = {
readonly siteId: SiteId;
};
export const Plan = ( { siteId }: PlanProps ): JSX.Element => {
const quota = useSubscriptionQuota();
const isAgencySummary = useIsAgencySummary();
if ( isAgencySummary ) {
return ;
}
return (
);
};
// ============
// HELPER VIEWS
// ============
const Title = () => {
const { period } = usePeriodAndPlan();
const productDisplay = useProductDisplay();
const state = useSubscriptionState();
const isFree = useIsFree();
const periodLabel =
'month' === period
? _x( 'Monthly', 'text', 'nelio-ab-testing' )
: _x( 'Yearly', 'text', 'nelio-ab-testing' );
return (
{ productDisplay }
{ isFree
? _x( 'Free', 'text', 'nelio-ab-testing' )
: periodLabel }
{ 'canceled' === state && (
{ _x(
'Canceled',
'text (account state)',
'nelio-ab-testing'
) }
) }
);
};
const Explanation = () => {
const isFree = useIsFree();
const isInvitation = useIsInvitation();
const state = useSubscriptionState();
const deactivationDate = useDeactivationDate();
const nextChargeDate = useNextChargeDate();
const nextChargeTotal = useNextChargeTotal();
if ( isFree ) {
return (
{ _x(
'You are using the free version of Nelio A/B Testing.',
'text',
'nelio-ab-testing'
) }
);
}
if ( isInvitation ) {
return (
{ createInterpolateElement(
sprintf(
/* translators: %s: Date. */
_x(
'You’re currently using a premium version of Nelio A/B Testing for free. This invitation will end on %s and the plugin will be downgraded to a free plan. Please consider subscribing before that.',
'user',
'nelio-ab-testing'
),
`${ formatI18nDate( nextChargeDate ) } `
),
{
date: ,
}
) }
);
}
if ( 'canceled' === state ) {
return (
{ createInterpolateElement(
sprintf(
/* translators: %s: Date. */
_x(
'Your subscription will end on %s.',
'text',
'nelio-ab-testing'
),
`${ formatI18nDate( deactivationDate ) } `
),
{
date: ,
}
) }
);
}
return (
{ createInterpolateElement(
sprintf(
/* translators: %1$s: Price and currency. %2$s: Date. */
_x(
'Next charge will be %1$s on %2$s.',
'text',
'nelio-ab-testing'
),
`${ nextChargeTotal } `,
`${ formatI18nDate( nextChargeDate ) } `
),
{
date: ,
money: ,
}
) }
);
};
const Actions = () => {
const isFree = useIsFree();
const isInvitation = useIsInvitation();
const state = useSubscriptionState();
if ( isFree || isInvitation ) {
return (
);
}
if ( 'canceled' === state ) {
return (
);
}
return (
);
};
const AddLicenseAction = () => {
const [ isPopoverVisible, setPopoverVisible ] = useState( false );
return (
setPopoverVisible( true ) }
>
{ _x( 'Use License', 'command', 'nelio-ab-testing' ) }
setPopoverVisible( false ) }
onClick={ () => setPopoverVisible( false ) }
isOpen={ isPopoverVisible }
/>
);
};
const SubscribeAction = () => {
const today = useToday();
return (
{ getSubscribeLabel( today ) }
);
};
const CancelSubscriptionAction = () => {
const [ isDialogOpen, setDialogOpen ] = useState( false );
const { cancelSubscription } = useDispatch( NAB_ACCOUNT );
const nextChargeDate = useNextChargeDate();
return (
<>
setDialogOpen( true ) }
>
{ _x( 'Cancel Subscription', 'command', 'nelio-ab-testing' ) }
setDialogOpen( false ) }
onConfirm={ () => {
setDialogOpen( false );
void cancelSubscription();
} }
isOpen={ isDialogOpen }
/>
>
);
};
const BuyMoreQuotaAction = () => {
const [ isPopoverVisible, setPopoverVisible ] = useState( false );
const { plan } = usePeriodAndPlan();
if ( 'enterprise' === plan ) {
return null;
}
return (
setPopoverVisible( ! isPopoverVisible ) }
>
{ _x( 'Buy More Quota', 'command', 'nelio-ab-testing' ) }
setPopoverVisible( false ) }
onFocusOutside={ () => setPopoverVisible( false ) }
isOpen={ isPopoverVisible }
/>
);
};
const AddonsAction = () => {
const [ isPopoverVisible, setPopoverVisible ] = useState( false );
return (
setPopoverVisible( ! isPopoverVisible ) }
>
{ _x( 'Addons', 'text', 'nelio-ab-testing' ) }
setPopoverVisible( false ) }
isOpen={ isPopoverVisible }
/>
);
};
const UpgradeAction = () => {
const [ isPopoverVisible, setPopoverVisible ] = useState( false );
return (
setPopoverVisible( ! isPopoverVisible ) }
>
{ _x( 'Upgrade Subscription', 'command', 'nelio-ab-testing' ) }
setPopoverVisible( false ) }
onFocusOutside={ () => setPopoverVisible( false ) }
isOpen={ isPopoverVisible }
/>
);
};
const ReactivateSubscriptionAction = () => {
const [ isDialogOpen, setDialogOpen ] = useState( false );
const { uncancelSubscription } = useDispatch( NAB_ACCOUNT );
const nextChargeDate = useNextChargeDate();
return (
<>
setDialogOpen( true ) }
>
{ _x(
'Reactivate Subscription',
'command',
'nelio-ab-testing'
) }
setDialogOpen( false ) }
onConfirm={ () => {
setDialogOpen( false );
void uncancelSubscription();
} }
isOpen={ isDialogOpen }
/>
>
);
};
// =====
// HOOKS
// =====
const usePeriodAndPlan = () =>
useSelect(
( select ) => ( {
period: select( NAB_ACCOUNT ).getPeriod(),
plan: select( NAB_ACCOUNT ).getPlan(),
} ),
[]
);
const useSubscriptionState = () =>
useSelect( ( select ) => select( NAB_ACCOUNT ).getState(), [] );
const useIsFree = () =>
useSelect( ( select ) => 'free' === select( NAB_ACCOUNT ).getMode(), [] );
const useIsInvitation = () =>
useSelect(
( select ) => 'invitation' === select( NAB_ACCOUNT ).getMode(),
[]
);
const useIsAgencySummary = () =>
useSelect(
( select ) =>
select( NAB_ACCOUNT ).isAgency() &&
! select( NAB_ACCOUNT ).isAgencyFullViewEnabled(),
[]
);
const useProductDisplay = () =>
useSelect( ( select ) => select( NAB_ACCOUNT ).getProductDisplay(), [] );
const useDeactivationDate = () =>
useSelect(
( select ) =>
select( NAB_ACCOUNT ).getDeactivationDate() ||
select( NAB_DATA ).getToday(),
[]
);
const useNextChargeDate = () =>
useSelect(
( select ) =>
select( NAB_ACCOUNT ).getNextChargeDate() ||
select( NAB_DATA ).getToday(),
[]
);
const useNextChargeTotal = () =>
useSelect( ( select ) => select( NAB_ACCOUNT ).getNextChargeTotal(), [] );
const useToday = () =>
useSelect( ( select ) => select( NAB_DATA ).getToday(), [] );
const useSubscriptionQuota = () =>
useSelect(
( select ) => ( {
data: select( NAB_ACCOUNT ).getSubscriptionQuota(),
isLoading: ! select( NAB_ACCOUNT ).hasFinishedResolution(
'getSubscriptionQuota'
),
} ),
[]
);