Skip to content

Commit 68e2d67

Browse files
authored
fix(clerk-js): Display "Switch to this plan" when a subscription already exists (#5722)
1 parent 2beea29 commit 68e2d67

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

.changeset/clear-trains-rhyme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
For each plan inside the `<PricingTable/>` display "Switch to this plan" instead of "Get started" when a subscription already exists.

packages/clerk-js/src/ui/components/Subscriptions/SubscriptionsList.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { usePlansContext } from '../../contexts';
44
import { Badge, Box, Button, localizationKeys, Span, Table, Tbody, Td, Text, Th, Thead, Tr } from '../../customizables';
55

66
export function SubscriptionsList() {
7-
const { subscriptions, handleSelectPlan, buttonPropsForPlan } = usePlansContext();
7+
const { subscriptions, handleSelectPlan, buttonPropsForPlan, shouldDisplayPlanButton } = usePlansContext();
88

99
const handleSelectSubscription = (subscription: __experimental_CommerceSubscriptionResource) => {
1010
handleSelectPlan({
@@ -68,12 +68,14 @@ export function SubscriptionsList() {
6868
textAlign: 'right',
6969
})}
7070
>
71-
<Button
72-
size='xs'
73-
textVariant='buttonSmall'
74-
onClick={() => handleSelectSubscription(subscription)}
75-
{...buttonPropsForPlan({ subscription })}
76-
/>
71+
{shouldDisplayPlanButton({ subscription }) && (
72+
<Button
73+
size='xs'
74+
textVariant='buttonSmall'
75+
onClick={() => handleSelectSubscription(subscription)}
76+
{...buttonPropsForPlan({ subscription })}
77+
/>
78+
)}
7779
</Td>
7880
</Tr>
7981
))}

packages/clerk-js/src/ui/contexts/components/Plans.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,21 @@ export const usePlansContext = () => {
148148
return ctx.subscriptions.length === 0;
149149
}, [ctx.subscriptions]);
150150

151+
const shouldDisplayPlanButton = useCallback(
152+
({
153+
plan,
154+
subscription: sub,
155+
}: {
156+
plan?: __experimental_CommercePlanResource;
157+
subscription?: __experimental_CommerceSubscriptionResource;
158+
}) => {
159+
const subscription = sub ?? (plan ? activeOrUpcomingSubscription(plan) : undefined);
160+
161+
return !subscription || !subscription.canceledAt;
162+
},
163+
[activeOrUpcomingSubscription],
164+
);
165+
151166
// return the CTA button props for a plan
152167
const buttonPropsForPlan = useCallback(
153168
({
@@ -166,7 +181,10 @@ export const usePlansContext = () => {
166181
? subscription.canceledAt
167182
? localizationKeys('__experimental_commerce.reSubscribe')
168183
: localizationKeys('__experimental_commerce.manageSubscription')
169-
: localizationKeys('__experimental_commerce.getStarted'),
184+
: // If there are no active or grace period subscriptions, show the get started button
185+
ctx.subscriptions.length > 0
186+
? localizationKeys('__experimental_commerce.switchPlan')
187+
: localizationKeys('__experimental_commerce.getStarted'),
170188
variant: isCompact || !!subscription ? 'bordered' : 'solid',
171189
colorScheme: isCompact || !!subscription ? 'secondary' : 'primary',
172190
};
@@ -228,5 +246,6 @@ export const usePlansContext = () => {
228246
isDefaultPlanImplicitlyActive,
229247
handleSelectPlan,
230248
buttonPropsForPlan,
249+
shouldDisplayPlanButton,
231250
};
232251
};

0 commit comments

Comments
 (0)