Skip to content

fix(clerk-js): Display "Switch to this plan" when a subscription already exists #5722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clear-trains-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

For each plan inside the `<PricingTable/>` display "Switch to this plan" instead of "Get started" when a subscription already exists.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { usePlansContext } from '../../contexts';
import { Badge, Box, Button, localizationKeys, Span, Table, Tbody, Td, Text, Th, Thead, Tr } from '../../customizables';

export function SubscriptionsList() {
const { subscriptions, handleSelectPlan, buttonPropsForPlan } = usePlansContext();
const { subscriptions, handleSelectPlan, buttonPropsForPlan, shouldDisplayPlanButton } = usePlansContext();

const handleSelectSubscription = (subscription: __experimental_CommerceSubscriptionResource) => {
handleSelectPlan({
Expand Down Expand Up @@ -68,12 +68,14 @@ export function SubscriptionsList() {
textAlign: 'right',
})}
>
<Button
size='xs'
textVariant='buttonSmall'
onClick={() => handleSelectSubscription(subscription)}
{...buttonPropsForPlan({ subscription })}
/>
{shouldDisplayPlanButton({ subscription }) && (
<Button
size='xs'
textVariant='buttonSmall'
onClick={() => handleSelectSubscription(subscription)}
{...buttonPropsForPlan({ subscription })}
/>
)}
</Td>
</Tr>
))}
Expand Down
21 changes: 20 additions & 1 deletion packages/clerk-js/src/ui/contexts/components/Plans.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,21 @@ export const usePlansContext = () => {
return ctx.subscriptions.length === 0;
}, [ctx.subscriptions]);

const shouldDisplayPlanButton = useCallback(
({
plan,
subscription: sub,
}: {
plan?: __experimental_CommercePlanResource;
subscription?: __experimental_CommerceSubscriptionResource;
}) => {
const subscription = sub ?? (plan ? activeOrUpcomingSubscription(plan) : undefined);

return !subscription || !subscription.canceledAt;
},
[activeOrUpcomingSubscription],
);

// return the CTA button props for a plan
const buttonPropsForPlan = useCallback(
({
Expand All @@ -166,7 +181,10 @@ export const usePlansContext = () => {
? subscription.canceledAt
? localizationKeys('__experimental_commerce.reSubscribe')
: localizationKeys('__experimental_commerce.manageSubscription')
: localizationKeys('__experimental_commerce.getStarted'),
: // If there are no active or grace period subscriptions, show the get started button
ctx.subscriptions.length > 0
? localizationKeys('__experimental_commerce.switchPlan')
: localizationKeys('__experimental_commerce.getStarted'),
variant: isCompact || !!subscription ? 'bordered' : 'solid',
colorScheme: isCompact || !!subscription ? 'secondary' : 'primary',
};
Expand Down Expand Up @@ -228,5 +246,6 @@ export const usePlansContext = () => {
isDefaultPlanImplicitlyActive,
handleSelectPlan,
buttonPropsForPlan,
shouldDisplayPlanButton,
};
};