forked from shadcn-ui/taxonomy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
579 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { DashboardHeader } from "@/components/dashboard/header" | ||
import { DashboardShell } from "@/components/dashboard/shell" | ||
import { Card } from "@/ui/card" | ||
|
||
export default function DashboardBillingLoading() { | ||
return ( | ||
<DashboardShell> | ||
<DashboardHeader | ||
heading="Billing" | ||
text="Manage billing and your subscription plan." | ||
/> | ||
<div className="grid gap-10"> | ||
<Card.Skeleton /> | ||
</div> | ||
</DashboardShell> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { redirect } from "next/navigation" | ||
|
||
import { getCurrentUser } from "@/lib/session" | ||
import { authOptions } from "@/lib/auth" | ||
import { getUserSubscriptionPlan as getUserSubscriptionPlan } from "@/lib/subscription" | ||
import { DashboardHeader } from "@/components/dashboard/header" | ||
import { DashboardShell } from "@/components/dashboard/shell" | ||
import { BillingForm } from "@/components/dashboard/billing-form" | ||
|
||
export default async function BillingPage() { | ||
const user = await getCurrentUser() | ||
|
||
if (!user) { | ||
redirect(authOptions.pages.signIn) | ||
} | ||
|
||
const subscriptionPlan = await getUserSubscriptionPlan(user.id) | ||
|
||
return ( | ||
<DashboardShell> | ||
<DashboardHeader | ||
heading="Billing" | ||
text="Manage billing and your subscription plan." | ||
/> | ||
<div className="grid gap-10"> | ||
<BillingForm subscriptionPlan={subscriptionPlan} /> | ||
</div> | ||
</DashboardShell> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
"use client" | ||
|
||
import * as React from "react" | ||
|
||
import { UserSubscriptionPlan } from "types" | ||
import { cn, formatDate } from "@/lib/utils" | ||
import { Card } from "@/ui/card" | ||
import { toast } from "@/ui/toast" | ||
import { Icons } from "@/components/icons" | ||
|
||
interface BillingFormProps extends React.HTMLAttributes<HTMLFormElement> { | ||
subscriptionPlan: UserSubscriptionPlan | ||
} | ||
|
||
export function BillingForm({ | ||
subscriptionPlan, | ||
className, | ||
...props | ||
}: BillingFormProps) { | ||
const [isLoading, setIsLoading] = React.useState<boolean>(false) | ||
|
||
async function onSubmit(event) { | ||
event.preventDefault() | ||
setIsLoading(!isLoading) | ||
|
||
// Get a Stripe session URL. | ||
const response = await fetch("/api/users/stripe") | ||
|
||
if (!response?.ok) { | ||
return toast({ | ||
title: "Something went wrong.", | ||
message: "Please refresh the page and try again.", | ||
type: "error", | ||
}) | ||
} | ||
|
||
// Redirect to the Stripe session. | ||
// This could be a checkout page for initial upgrade. | ||
// Or portal to manage existing subscription. | ||
const session = await response.json() | ||
if (session) { | ||
window.location.href = session.url | ||
} | ||
} | ||
|
||
return ( | ||
<form className={cn(className)} onSubmit={onSubmit} {...props}> | ||
<Card> | ||
<Card.Header> | ||
<Card.Title>Plan</Card.Title> | ||
<Card.Description> | ||
You are currently on the <strong>{subscriptionPlan.name}</strong>{" "} | ||
plan. | ||
</Card.Description> | ||
</Card.Header> | ||
<Card.Content>{subscriptionPlan.description}</Card.Content> | ||
<Card.Footer className="flex items-center justify-between"> | ||
<button | ||
type="submit" | ||
className={cn( | ||
"relative inline-flex h-9 items-center rounded-md border border-transparent bg-brand-500 px-4 py-2 text-sm font-medium text-white hover:bg-brand-400 focus:outline-none focus:ring-2 focus:ring-brand-500 focus:ring-offset-2", | ||
{ | ||
"cursor-not-allowed opacity-60": isLoading, | ||
} | ||
)} | ||
disabled={isLoading} | ||
> | ||
{isLoading && ( | ||
<Icons.spinner className="mr-2 h-4 w-4 animate-spin" /> | ||
)} | ||
{subscriptionPlan.isPro ? "Manage Subscription" : "Upgrade to PRO"} | ||
</button> | ||
{subscriptionPlan.isPro ? ( | ||
<p className="rounded-full text-xs font-medium"> | ||
{subscriptionPlan.isCanceled | ||
? "Your plan will be canceled on " | ||
: "Your plan renews on "} | ||
{formatDate(subscriptionPlan.stripeCurrentPeriodEnd)}. | ||
</p> | ||
) : null} | ||
</Card.Footer> | ||
</Card> | ||
</form> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { SubscriptionPlan } from "types" | ||
|
||
export const freePlan: SubscriptionPlan = { | ||
name: "Free", | ||
description: | ||
"The free plan is limited to 3 posts. Upgrade to the PRO plan for unlimited posts.", | ||
stripePriceId: null, | ||
} | ||
|
||
export const proPlan: SubscriptionPlan = { | ||
name: "PRO", | ||
description: "The PRO plan has unlimited posts.", | ||
stripePriceId: process.env.SUBSCRIPTION_PLAN_PRICE_ID_PRO, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class RequiresProPlanError extends Error { | ||
constructor(message = "This action requires a pro plan") { | ||
super(message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Stripe from "stripe" | ||
|
||
export const stripe = new Stripe(process.env.STRIPE_API_KEY, { | ||
apiVersion: "2022-11-15", | ||
typescript: true, | ||
}) |
Oops, something went wrong.