@@ -36,37 +36,35 @@ function Plans({ products, subscription }: PricingProps) {
36
36
const [ billingInterval , setBillingInterval ] =
37
37
useState < PricingPlanInterval > ( "month" ) ;
38
38
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
+ } ) ;
53
53
54
54
const handleBilling = ( interval : PricingPlanInterval ) => {
55
55
setBillingInterval ( interval ) ;
56
56
} ;
57
57
58
58
const handleStripeCheckout = async ( price : handleStripeCheckoutOptions ) => {
59
- setPriceIdLoading ( price . priceId ) ;
60
-
61
59
await checkoutWithStripe ( price ) ;
62
-
63
- setPriceIdLoading ( undefined ) ;
64
60
} ;
65
61
66
62
const handleBillingPortal = async ( ) => {
67
63
await stripePortal ( ) ;
68
64
} ;
69
65
66
+ const loading = checkoutLoading || stripePortalLoading ;
67
+
70
68
return (
71
69
< div >
72
70
< 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) {
101
99
minimumFractionDigits : 0 ,
102
100
} ) . format ( unitAmount / 100 ) ;
103
101
102
+ const active = subscription ?. priceId === price . id ;
104
103
return (
105
104
< PricingCard
106
- active = { subscription ?. priceId === price . id }
107
105
key = { product . id }
108
106
title = { product . name }
109
107
description = { product . description }
@@ -119,9 +117,10 @@ function Plans({ products, subscription }: PricingProps) {
119
117
priceType : price . type ,
120
118
} ) ;
121
119
} }
122
- loading = { priceIdLoading === price . id }
120
+ loading = { loading }
123
121
subscribedUnitAmount = { subscription ?. price . unitAmount }
124
122
unitAmount = { unitAmount }
123
+ variant = { active ? "default" : "secondary" }
125
124
/>
126
125
) ;
127
126
} ) }
0 commit comments