Skip to content

Commit 1694527

Browse files
committed
fix: loading state in card
1 parent 3543387 commit 1694527

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

src/components/billing/pricing/index.tsx

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,37 +36,35 @@ function Plans({ products, subscription }: PricingProps) {
3636
const [billingInterval, setBillingInterval] =
3737
useState<PricingPlanInterval>("month");
3838

39-
const [priceIdLoading, setPriceIdLoading] = useState<string>();
40-
41-
const { mutateAsync: checkoutWithStripe } = api.billing.checkout.useMutation({
42-
onSuccess: async ({ stripeSessionId }) => {
43-
const stripe = await getStripeClient();
44-
await stripe?.redirectToCheckout({ sessionId: stripeSessionId });
45-
},
46-
});
47-
48-
const { mutateAsync: stripePortal } = api.billing.stripePortal.useMutation({
49-
onSuccess: ({ url }) => {
50-
router.push(url);
51-
},
52-
});
39+
const { mutateAsync: checkoutWithStripe, isLoading: checkoutLoading } =
40+
api.billing.checkout.useMutation({
41+
onSuccess: async ({ stripeSessionId }) => {
42+
const stripe = await getStripeClient();
43+
await stripe?.redirectToCheckout({ sessionId: stripeSessionId });
44+
},
45+
});
46+
47+
const { mutateAsync: stripePortal, isLoading: stripePortalLoading } =
48+
api.billing.stripePortal.useMutation({
49+
onSuccess: ({ url }) => {
50+
router.push(url);
51+
},
52+
});
5353

5454
const handleBilling = (interval: PricingPlanInterval) => {
5555
setBillingInterval(interval);
5656
};
5757

5858
const handleStripeCheckout = async (price: handleStripeCheckoutOptions) => {
59-
setPriceIdLoading(price.priceId);
60-
6159
await checkoutWithStripe(price);
62-
63-
setPriceIdLoading(undefined);
6460
};
6561

6662
const handleBillingPortal = async () => {
6763
await stripePortal();
6864
};
6965

66+
const loading = checkoutLoading || stripePortalLoading;
67+
7068
return (
7169
<div>
7270
<div className="inline-flex h-10 items-center justify-center rounded-md bg-muted p-1 text-muted-foreground">
@@ -101,9 +99,9 @@ function Plans({ products, subscription }: PricingProps) {
10199
minimumFractionDigits: 0,
102100
}).format(unitAmount / 100);
103101

102+
const active = subscription?.priceId === price.id;
104103
return (
105104
<PricingCard
106-
active={subscription?.priceId === price.id}
107105
key={product.id}
108106
title={product.name}
109107
description={product.description}
@@ -119,9 +117,10 @@ function Plans({ products, subscription }: PricingProps) {
119117
priceType: price.type,
120118
});
121119
}}
122-
loading={priceIdLoading === price.id}
120+
loading={loading}
123121
subscribedUnitAmount={subscription?.price.unitAmount}
124122
unitAmount={unitAmount}
123+
variant={active ? "default" : "secondary"}
125124
/>
126125
);
127126
})}

src/components/billing/pricing/pricing-card.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ interface PricingCardProps extends Omit<ButtonProps, "children"> {
1515
price: string;
1616
interval: PricingPlanInterval;
1717
subscribed: boolean;
18-
active: boolean;
1918
subscribedUnitAmount?: bigint | null;
2019
unitAmount: number;
2120
}
@@ -33,7 +32,6 @@ export function PricingCard({
3332
interval,
3433
price,
3534
subscribed,
36-
active,
3735
subscribedUnitAmount: subscribedUnitAmount_,
3836
unitAmount,
3937
...rest
@@ -56,10 +54,10 @@ export function PricingCard({
5654
<CardFooter>
5755
<Button {...rest}>
5856
{subscribedUnitAmount
59-
? subscribedUnitAmount < unitAmount
60-
? "Upgrade Plan"
61-
: subscribedUnitAmount > unitAmount
62-
? "Downgrade Plan"
57+
? unitAmount < subscribedUnitAmount
58+
? "Downgrade Plan"
59+
: unitAmount > subscribedUnitAmount
60+
? "Upgrade Plan"
6361
: "Manage Current Plan"
6462
: "Subscribe"}
6563
</Button>

0 commit comments

Comments
 (0)