Skip to content

Commit 7197f15

Browse files
committed
chore: fix loading state
1 parent 12f49c6 commit 7197f15

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/components/billing/pricing-modal/index.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function Plans({ products, subscription }: PricingProps) {
6868
await stripePortal(data);
6969
};
7070

71-
const loading = checkoutLoading || stripePortalLoading;
71+
const isSubmitting = checkoutLoading || stripePortalLoading;
7272

7373
return (
7474
<div className="flex flex-col items-center justify-center">
@@ -112,8 +112,7 @@ function Plans({ products, subscription }: PricingProps) {
112112
description={product.description}
113113
price={priceString}
114114
interval={billingInterval}
115-
subscribed={!!subscription}
116-
onClick={() => {
115+
handleClick={() => {
117116
if (subscription) {
118117
return handleBillingPortal({
119118
...(active
@@ -132,9 +131,9 @@ function Plans({ products, subscription }: PricingProps) {
132131
priceType: price.type,
133132
});
134133
}}
135-
loading={loading}
136134
subscribedUnitAmount={subscription?.price.unitAmount}
137135
unitAmount={unitAmount}
136+
isSubmitting={isSubmitting}
138137
/>
139138
);
140139
})}

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ import {
88
} from "@/components/ui/card";
99
import { cn } from "@/lib/utils";
1010
import type { PricingPlanInterval } from "@/prisma/enums";
11+
import { useState } from "react";
1112

12-
interface PricingCardProps extends Omit<ButtonProps, "children"> {
13+
interface PricingCardProps {
1314
title: string;
1415
description?: string | null;
1516
price: string;
1617
interval: PricingPlanInterval;
17-
subscribed: boolean;
1818
subscribedUnitAmount?: bigint | null;
1919
unitAmount: number;
20+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
21+
handleClick: () => Promise<any>;
22+
isSubmitting: boolean;
2023
}
2124

2225
const humanizedInterval: Record<PricingPlanInterval, string> = {
@@ -31,11 +34,12 @@ export function PricingCard({
3134
title,
3235
interval,
3336
price,
34-
subscribed,
3537
subscribedUnitAmount: subscribedUnitAmount_,
3638
unitAmount,
37-
...rest
39+
handleClick,
40+
isSubmitting,
3841
}: PricingCardProps) {
42+
const [isLoading, setIsLoading] = useState(false);
3943
const subscribedUnitAmount = subscribedUnitAmount_
4044
? Number(subscribedUnitAmount_)
4145
: null;
@@ -56,12 +60,15 @@ export function PricingCard({
5660
</CardHeader>
5761
<CardFooter>
5862
<Button
59-
{...rest}
60-
className={cn(
61-
rest.className,
62-
!active && "bg-teal-500 hover:bg-teal-500/80",
63-
)}
63+
className={cn(!active && "bg-teal-500 hover:bg-teal-500/80")}
6464
{...(active && { variant: "destructive" })}
65+
onClick={async () => {
66+
setIsLoading(true);
67+
await handleClick();
68+
setIsLoading(false);
69+
}}
70+
loading={isLoading}
71+
{...(!isLoading && { disabled: isSubmitting })}
6572
>
6673
{subscribedUnitAmount
6774
? unitAmount < subscribedUnitAmount

0 commit comments

Comments
 (0)