Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
feat: "Manage billing" button in settings
Richie McIlroyfe912fe7d8c4
diff --git a/apps/web/app/api/settings/billing/manage/route.ts b/apps/web/app/api/settings/billing/manage/route.tsnew file mode 100644index 000000000..428235621--- /dev/null+++ b/apps/web/app/api/settings/billing/manage/route.ts@@ -0,0 +1,43 @@+import { stripe } from "@cap/utils";+import { getCurrentUser } from "@cap/database/auth/session";+import { NextRequest, NextResponse } from "next/server";+import { eq } from "drizzle-orm";+import { db } from "@cap/database";+import { users } from "@cap/database/schema";++export async function POST(request: NextRequest) {+ const user = await getCurrentUser();+ let customerId = user?.stripeCustomerId;++ if (!user) {+ console.error("User not found");++ return new Response(JSON.stringify({ error: true }), {+ status: 401,+ headers: {+ "Content-Type": "application/json",+ },+ });+ }++ if (!user.stripeCustomerId) {+ const customer = await stripe.customers.create({+ email: user.email,+ });++ await db+ .update(users)+ .set({+ stripeCustomerId: customer.id,+ })+ .where(eq(users.id, user.id));++ customerId = customer.id;+ }++ const { url } = await stripe.billingPortal.sessions.create({+ customer: customerId as string,+ return_url: `${process.env.NEXT_PUBLIC_URL}/dashboard/settings/billing`,+ });+ return NextResponse.json(url);+}diff --git a/apps/web/app/dashboard/_components/AdminNavbar/AdminNavItems.tsx b/apps/web/app/dashboard/_components/AdminNavbar/AdminNavItems.tsxindex 166aa10cd..26b78f7a9 100644--- a/apps/web/app/dashboard/_components/AdminNavbar/AdminNavItems.tsx+++ b/apps/web/app/dashboard/_components/AdminNavbar/AdminNavItems.tsx@@ -43,10 +43,23 @@ export const AdminNavItems = () => {// const { spaceData, activeSpace } = useSharedContext();const manageNavigation = [- { name: "My Caps", href: `/dashboard/caps`, icon: Clapperboard },+ {+ name: "My Caps",+ href: `/dashboard/caps`,+ icon: Clapperboard,+ subNav: [],+ },// { name: "Notifications", href: `/dashboard/notifications`, icon: Bell },// { name: "History", href: `/dashboard/history`, icon: History },- { name: "Settings", href: `/dashboard/settings`, icon: Settings },+ {+ name: "Settings",+ href: `/dashboard/settings`,+ icon: Settings,+ subNav: [+ { name: "My account", href: `/dashboard/settings` },+ { name: "Billing", href: `/dashboard/settings/billing` },+ ],+ },];const navItemClass =@@ -109,24 +122,47 @@ export const AdminNavItems = () => {><div className="space-y-1">{manageNavigation.map((item) => (- <Link- passHref- prefetch={false}- key={item.name}- href={item.href}- className={classNames(- pathname.includes(item.href)- ? "bg-gray-200"- : "opacity-75 hover:opacity-100",- navItemClass+ <div>+ <Link+ passHref+ prefetch={false}+ key={item.name}+ href={item.href}+ className={classNames(+ pathname.includes(item.href)+ ? "bg-gray-200 font-medium"+ : "opacity-75 hover:opacity-100",+ navItemClass+ )}+ >+ <item.icon+ className="flex-shrink-0 w-6 h-6 stroke-[1.8px]"+ aria-hidden="true"+ />+ <span className="text-base ml-2.5">{item.name}</span>+ </Link>+ {pathname.includes(item.href) && item.subNav.length > 0 && (+ <div className="mt-1 space-y-1 flex-grow w-full">+ {item.subNav.map((subItem) => (+ <Link+ passHref+ prefetch={false}+ key={subItem.name}+ href={subItem.href}+ className={classNames(+ pathname === subItem.href+ ? "font-medium"+ : "opacity-75 hover:opacity-100",+ navItemClass+ )}+ >+ <div className="w-6 h-6"></div>+ <span className="text-base ml-2.5">{subItem.name}</span>+ </Link>+ ))}+ </div>)}- >- <item.icon- className="flex-shrink-0 w-6 h-6 stroke-[1.8px]"- aria-hidden="true"- />- <span className="text-base ml-2.5">{item.name}</span>- </Link>+ </div>))}</div><div className="mt-auto">diff --git a/apps/web/app/dashboard/settings/Settings.tsx b/apps/web/app/dashboard/settings/Settings.tsxindex 0299ba491..34558513e 100644--- a/apps/web/app/dashboard/settings/Settings.tsx+++ b/apps/web/app/dashboard/settings/Settings.tsx@@ -22,6 +22,7 @@ export const Settings = ({}) => {const firstName = user ? user?.name : "";const lastName = user ? user?.lastName : "";+ const email = user ? user?.email : "";const router = useRouter();const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {@@ -84,6 +85,23 @@ export const Settings = ({</div></div></CardContent>+ <CardHeader>+ <CardTitle>Contact email address</CardTitle>+ <CardDescription>+ This is the email address you used to sign up to Cap with.+ </CardDescription>+ </CardHeader>+ <CardContent>+ <div>+ <Input+ type="email"+ value={email as string}+ id="contactEmail"+ name="contactEmail"+ disabled+ />+ </div>+ </CardContent><CardFooter className="border-t px-6 py-4"><Button type="submit" size="default">Savediff --git a/apps/web/app/dashboard/settings/billing/Billing.tsx b/apps/web/app/dashboard/settings/billing/Billing.tsxindex a21856705..6b287d936 100644--- a/apps/web/app/dashboard/settings/billing/Billing.tsx+++ b/apps/web/app/dashboard/settings/billing/Billing.tsx@@ -13,7 +13,7 @@ import {} from "@cap/ui";import { users } from "@cap/database/schema";import { useRouter } from "next/navigation";-import toast from "react-hot-toast";+import { useState } from "react";export const Billing = ({user,@@ -21,25 +21,45 @@ export const Billing = ({user: typeof users.$inferSelect | null;}) => {const router = useRouter();+ const [loading, setLoading] = useState(false);return (- <form>- <Card>- <CardHeader>- <CardTitle>Billing</CardTitle>- <CardDescription>Manage all things billing.</CardDescription>- </CardHeader>- <CardContent>- <CardTitle>View and manage your billing details</CardTitle>- <CardDescription>- View and edit your billing details, as well as cancel your- subscription.- </CardDescription>- <CardDescription className="mt-3">- <Button size="sm">Manage billing</Button>- </CardDescription>- </CardContent>- </Card>- </form>+ <Card>+ <CardHeader>+ <CardTitle>Billing</CardTitle>+ <CardDescription>Manage all things billing.</CardDescription>+ </CardHeader>+ <CardContent>+ <CardTitle>View and manage your billing details</CardTitle>+ <CardDescription>+ View and edit your billing details, as well as cancel your+ subscription.+ </CardDescription>+ <CardDescription className="mt-3">+ <Button+ type="button"+ size="sm"+ variant="default"+ onClick={() => {+ setLoading(true);+ fetch(`/api/settings/billing/manage`, {+ method: "POST",+ })+ .then(async (res) => {+ const url = await res.json();+ router.push(url);+ })+ .catch((err) => {+ alert(err);+ setLoading(false);+ });+ }}+ spinner={loading}+ >+ Manage Billing+ </Button>+ </CardDescription>+ </CardContent>+ </Card>);};diff --git a/apps/web/app/dashboard/settings/layout.tsx b/apps/web/app/dashboard/settings/layout.tsxindex b98289140..971a51b8d 100644--- a/apps/web/app/dashboard/settings/layout.tsx+++ b/apps/web/app/dashboard/settings/layout.tsx@@ -34,7 +34,7 @@ export default async function DashboardLayout({href={item.href}className={`${item.href === pathname- ? "text-black border-black primary"+ ? "text-black border-black primary font-medium": "text-gray-900 opacity-50 hover:opacity-100 border-transparent"} pb-2 border-b-2`}>diff --git a/apps/web/package.json b/apps/web/package.jsonindex c9bfa0e4f..076050318 100644--- a/apps/web/package.json+++ b/apps/web/package.json@@ -47,7 +47,6 @@"react-tooltip": "^5.26.3","resend": "1.1.0","server-only": "^0.0.1",- "stripe": "^14.24.0","tailwind-merge": "^2.1.0","zod": "^3.22.4"},diff --git a/packages/utils/package.json b/packages/utils/package.jsonindex 34b00b59e..1626d4324 100644--- a/packages/utils/package.json+++ b/packages/utils/package.json@@ -20,7 +20,8 @@},"dependencies": {"tailwind-merge": "^2.1.0",- "clsx": "^2.0.0"+ "clsx": "^2.0.0",+ "stripe": "^14.24.0"},"engines": {"node": "20"diff --git a/packages/utils/src/index.tsx b/packages/utils/src/index.tsxindex 852d0d070..d551394f8 100644--- a/packages/utils/src/index.tsx+++ b/packages/utils/src/index.tsx@@ -1,2 +1,3 @@export * from "./types/database";export * from "./helpers";+export * from "./lib/stripe/stripe";diff --git a/packages/utils/src/lib/stripe/stripe.ts b/packages/utils/src/lib/stripe/stripe.tsnew file mode 100644index 000000000..99a04a4b0--- /dev/null+++ b/packages/utils/src/lib/stripe/stripe.ts@@ -0,0 +1,14 @@+import Stripe from "stripe";++export const stripe = new Stripe(+ process.env.STRIPE_SECRET_KEY_TEST ??+ process.env.STRIPE_SECRET_KEY_LIVE ??+ "",+ {+ apiVersion: "2023-10-16",+ appInfo: {+ name: "Cap",+ version: "0.1.0",+ },+ }+);diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex c6f45f620..d824708c3 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -222,9 +222,6 @@ importers:server-only:specifier: ^0.0.1version: 0.0.1- stripe:- specifier: ^14.24.0- version: 14.24.0tailwind-merge:specifier: ^2.1.0version: 2.2.0@@ -486,6 +483,9 @@ importers:clsx:specifier: ^2.0.0version: 2.1.0+ stripe:+ specifier: ^14.24.0+ version: 14.24.0tailwind-merge:specifier: ^2.1.0version: 2.2.0