| 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}> |