Skip to content

Commit dbc01f8

Browse files
committed
feat: add active class name
1 parent 1f69b1a commit dbc01f8

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Modal, { type ModalProps } from "@/components/common/modal";
55
import { Button } from "@/components/ui/button";
66
import type { PricingPlanInterval, PricingType } from "@/prisma/enums";
77
import { api } from "@/trpc/react";
8+
import type { TypeZodStripePortalMutationSchema } from "@/trpc/routers/billing-router/schema";
89
import type { RouterOutputs } from "@/trpc/shared";
910
import { useRouter } from "next/navigation";
1011
import { useState } from "react";
@@ -61,14 +62,16 @@ function Plans({ products, subscription }: PricingProps) {
6162
await checkoutWithStripe(price);
6263
};
6364

64-
const handleBillingPortal = async () => {
65-
await stripePortal();
65+
const handleBillingPortal = async (
66+
data: TypeZodStripePortalMutationSchema,
67+
) => {
68+
await stripePortal(data);
6669
};
6770

6871
const loading = checkoutLoading || stripePortalLoading;
6972

7073
return (
71-
<div>
74+
<div className="flex flex-col items-center justify-center">
7275
<div className="inline-flex h-10 items-center justify-center rounded-md bg-muted p-1 text-muted-foreground">
7376
{intervals.includes("month") && (
7477
<PricingButton
@@ -112,7 +115,17 @@ function Plans({ products, subscription }: PricingProps) {
112115
subscribed={!!subscription}
113116
onClick={() => {
114117
if (subscription) {
115-
return handleBillingPortal();
118+
return handleBillingPortal({
119+
...(active
120+
? {
121+
type: "cancel",
122+
subscription: subscription.id,
123+
}
124+
: {
125+
type: "update",
126+
subscription: subscription.id,
127+
}),
128+
});
116129
}
117130
return handleStripeCheckout({
118131
priceId: price.id,
@@ -122,7 +135,6 @@ function Plans({ products, subscription }: PricingProps) {
122135
loading={loading}
123136
subscribedUnitAmount={subscription?.price.unitAmount}
124137
unitAmount={unitAmount}
125-
variant={active ? "default" : "secondary"}
126138
/>
127139
);
128140
})}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
CardHeader,
77
CardTitle,
88
} from "@/components/ui/card";
9+
import { cn } from "@/lib/utils";
910
import type { PricingPlanInterval } from "@/prisma/enums";
1011

1112
interface PricingCardProps extends Omit<ButtonProps, "children"> {
@@ -38,6 +39,9 @@ export function PricingCard({
3839
const subscribedUnitAmount = subscribedUnitAmount_
3940
? Number(subscribedUnitAmount_)
4041
: null;
42+
43+
const active = unitAmount === subscribedUnitAmount;
44+
4145
return (
4246
<Card>
4347
<CardHeader>
@@ -51,13 +55,20 @@ export function PricingCard({
5155
<CardDescription>{description}</CardDescription>
5256
</CardHeader>
5357
<CardFooter>
54-
<Button {...rest}>
58+
<Button
59+
{...rest}
60+
className={cn(
61+
rest.className,
62+
!active && "bg-teal-500 hover:bg-teal-500/80",
63+
)}
64+
{...(active && { variant: "destructive" })}
65+
>
5566
{subscribedUnitAmount
5667
? unitAmount < subscribedUnitAmount
5768
? "Downgrade Plan"
5869
: unitAmount > subscribedUnitAmount
5970
? "Upgrade Plan"
60-
: "Manage Current Plan"
71+
: "Cancel Current Plan"
6172
: "Subscribe"}
6273
</Button>
6374
</CardFooter>

0 commit comments

Comments
 (0)