({
subscriptionPlan,
className,
...props
}: BillingFormProps)
| 23 | } |
| 24 | |
| 25 | export function BillingForm({ |
| 26 | subscriptionPlan, |
| 27 | className, |
| 28 | ...props |
| 29 | }: BillingFormProps) { |
| 30 | const [isLoading, setIsLoading] = React.useState<boolean>(false) |
| 31 | |
| 32 | async function onSubmit(event) { |
| 33 | event.preventDefault() |
| 34 | setIsLoading(!isLoading) |
| 35 | |
| 36 | // Get a Stripe session URL. |
| 37 | const response = await fetch("/api/users/stripe") |
| 38 | |
| 39 | if (!response?.ok) { |
| 40 | return toast({ |
| 41 | title: "Something went wrong.", |
| 42 | description: "Please refresh the page and try again.", |
| 43 | variant: "destructive", |
| 44 | }) |
| 45 | } |
| 46 | |
| 47 | // Redirect to the Stripe session. |
| 48 | // This could be a checkout page for initial upgrade. |
| 49 | // Or portal to manage existing subscription. |
| 50 | const session = await response.json() |
| 51 | if (session) { |
| 52 | window.location.href = session.url |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return ( |
| 57 | <form className={cn(className)} onSubmit={onSubmit} {...props}> |
| 58 | <Card> |
| 59 | <CardHeader> |
| 60 | <CardTitle>Subscription Plan</CardTitle> |
| 61 | <CardDescription> |
| 62 | You are currently on the <strong>{subscriptionPlan.name}</strong>{" "} |
| 63 | plan. |
| 64 | </CardDescription> |
| 65 | </CardHeader> |
| 66 | <CardContent>{subscriptionPlan.description}</CardContent> |
| 67 | <CardFooter className="flex flex-col items-start space-y-2 md:flex-row md:justify-between md:space-x-0"> |
| 68 | <button |
| 69 | type="submit" |
| 70 | className={cn(buttonVariants())} |
| 71 | disabled={isLoading} |
| 72 | > |
| 73 | {isLoading && ( |
| 74 | <Icons.spinner className="mr-2 h-4 w-4 animate-spin" /> |
| 75 | )} |
| 76 | {subscriptionPlan.isPro ? "Manage Subscription" : "Upgrade to PRO"} |
| 77 | </button> |
| 78 | {subscriptionPlan.isPro ? ( |
| 79 | <p className="rounded-full text-xs font-medium"> |
| 80 | {subscriptionPlan.isCanceled |
| 81 | ? "Your plan will be canceled on " |
| 82 | : "Your plan renews on "} |
nothing calls this directly
no test coverage detected