|
| 1 | +import { ReactNode } from 'react'; |
| 2 | +import { cookies } from 'next/headers' |
| 3 | +import { redirect } from 'next/navigation'; |
| 4 | +import { revalidatePath } from 'next/cache' |
| 5 | +import Link from 'next/link'; |
| 6 | +import { |
| 7 | + getSession, |
| 8 | + getUserDetails, |
| 9 | + getSubscription |
| 10 | +} from '@/app/supabase-server'; |
| 11 | +import { createServerActionClient } from '@supabase/auth-helpers-nextjs' |
| 12 | +import ManageSubscriptionButton from './ManageSubscriptionButton'; |
| 13 | +import Button from '@/components/ui/Button'; |
| 14 | +import { Database } from '@/types_db'; |
| 15 | + |
| 16 | +export default async function Account() { |
| 17 | + const session = await getSession(); |
| 18 | + const user = session?.user; |
| 19 | + const userDetails = await getUserDetails(); |
| 20 | + const subscription = await getSubscription(); |
| 21 | + |
| 22 | + if (!session) { |
| 23 | + redirect('/signin'); |
| 24 | + } |
| 25 | + |
| 26 | + const subscriptionPrice = |
| 27 | + subscription && |
| 28 | + new Intl.NumberFormat('en-US', { |
| 29 | + style: 'currency', |
| 30 | + currency: subscription?.prices?.currency, |
| 31 | + minimumFractionDigits: 0 |
| 32 | + }).format((subscription?.prices?.unit_amount || 0) / 100); |
| 33 | + |
| 34 | + const updateName = async (formData: FormData) => { |
| 35 | + 'use server' |
| 36 | + |
| 37 | + const newName = formData.get('name') |
| 38 | + const supabase = createServerActionClient<Database>({ cookies }) |
| 39 | + const session = await getSession(); |
| 40 | + const user = session?.user; |
| 41 | + const { error } = await supabase.from('users').update({ full_name: newName }).eq('id', user?.id) |
| 42 | + if (error) { |
| 43 | + console.log(error) |
| 44 | + } |
| 45 | + revalidatePath('/account') |
| 46 | + } |
| 47 | + |
| 48 | + const updateEmail = async (formData: FormData) => { |
| 49 | + 'use server' |
| 50 | + |
| 51 | + const newEmail = formData.get('email') |
| 52 | + const supabase = createServerActionClient<Database>({ cookies }) |
| 53 | + const { error } = await supabase.auth.updateUser({ email: newEmail }) |
| 54 | + if (error) { |
| 55 | + console.log(error) |
| 56 | + } |
| 57 | + revalidatePath('/account') |
| 58 | + } |
| 59 | + |
| 60 | + return ( |
| 61 | + <section className="mb-32 bg-black"> |
| 62 | + <div className="max-w-6xl px-4 py-8 mx-auto sm:px-6 sm:pt-24 lg:px-8"> |
| 63 | + <div className="sm:align-center sm:flex sm:flex-col"> |
| 64 | + <h1 className="text-4xl font-extrabold text-white sm:text-center sm:text-6xl"> |
| 65 | + Account |
| 66 | + </h1> |
| 67 | + <p className="max-w-2xl m-auto mt-5 text-xl text-zinc-200 sm:text-center sm:text-2xl"> |
| 68 | + We partnered with Stripe for a simplified billing. |
| 69 | + </p> |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + <div className="p-4"> |
| 73 | + <Card |
| 74 | + title="Your Plan" |
| 75 | + description={ |
| 76 | + subscription |
| 77 | + ? `You are currently on the ${subscription?.prices?.products?.name} plan.` |
| 78 | + : 'You are not currently subscribed to any plan.' |
| 79 | + } |
| 80 | + footer={<ManageSubscriptionButton session={session} />} |
| 81 | + > |
| 82 | + <div className="mt-8 mb-4 text-xl font-semibold"> |
| 83 | + {subscription ? ( |
| 84 | + `${subscriptionPrice}/${subscription?.prices?.interval}` |
| 85 | + ) : ( |
| 86 | + <Link href="/">Choose your plan</Link> |
| 87 | + )} |
| 88 | + </div> |
| 89 | + </Card> |
| 90 | + <Card |
| 91 | + title="Your Name" |
| 92 | + description="Please enter your full name, or a display name you are comfortable with." |
| 93 | + footer={ |
| 94 | + <div className="flex flex-col items-start justify-between sm:flex-row sm:items-center"> |
| 95 | + <p className="pb-4 sm:pb-0">64 characters maximum</p> |
| 96 | + <Button variant="slim" type="submit" form="nameForm" disabled={true}> |
| 97 | + {/* WARNING - In Next.js 13.4.x server actions are in alpha and should not be used in production code! */} |
| 98 | + Update Name |
| 99 | + </Button> |
| 100 | + </div> |
| 101 | + } |
| 102 | + > |
| 103 | + <div className="mt-8 mb-4 text-xl font-semibold"> |
| 104 | + <form id="nameForm" action={updateName}> |
| 105 | + <input |
| 106 | + type="text" |
| 107 | + name="name" |
| 108 | + className="w-1/2 p-3 rounded-md bg-zinc-800" |
| 109 | + defaultValue={userDetails?.full_name} |
| 110 | + placeholder="Your name" |
| 111 | + maxLength={64} |
| 112 | + /> |
| 113 | + </form> |
| 114 | + </div> |
| 115 | + </Card> |
| 116 | + <Card |
| 117 | + title="Your Email" |
| 118 | + description="Please enter the email address you want to use to login." |
| 119 | + footer={ |
| 120 | + <div className="flex flex-col items-start justify-between sm:flex-row sm:items-center"> |
| 121 | + <p className="pb-4 sm:pb-0">We will email you to verify the change.</p> |
| 122 | + <Button variant="slim" type="submit" form="emailForm" disabled={true}> |
| 123 | + {/* WARNING - In Next.js 13.4.x server actions are in alpha and should not be used in production code! */} |
| 124 | + Update Email |
| 125 | + </Button> |
| 126 | + </div> |
| 127 | + } |
| 128 | + > |
| 129 | + <div className="mt-8 mb-4 text-xl font-semibold"> |
| 130 | + <form id="emailForm" action={updateEmail}> |
| 131 | + <input |
| 132 | + type="text" |
| 133 | + name="email" |
| 134 | + className="w-1/2 p-3 rounded-md bg-zinc-800" |
| 135 | + defaultValue={user ? user.email : ""} |
| 136 | + placeholder="Your email" |
| 137 | + maxLength={64} |
| 138 | + /> |
| 139 | + </form> |
| 140 | + </div> |
| 141 | + </Card> |
| 142 | + </div> |
| 143 | + </section> |
| 144 | + ); |
| 145 | +} |
| 146 | + |
| 147 | +interface Props { |
| 148 | + title: string; |
| 149 | + description?: string; |
| 150 | + footer?: ReactNode; |
| 151 | + children: ReactNode; |
| 152 | +} |
| 153 | + |
| 154 | +function Card({ title, description, footer, children }: Props) { |
| 155 | + return ( |
| 156 | + <div className="w-full max-w-3xl m-auto my-8 border rounded-md p border-zinc-700"> |
| 157 | + <div className="px-5 py-4"> |
| 158 | + <h3 className="mb-1 text-2xl font-medium">{title}</h3> |
| 159 | + <p className="text-zinc-300">{description}</p> |
| 160 | + {children} |
| 161 | + </div> |
| 162 | + <div className="p-4 border-t rounded-b-md border-zinc-700 bg-zinc-900 text-zinc-500"> |
| 163 | + {footer} |
| 164 | + </div> |
| 165 | + </div> |
| 166 | + ); |
| 167 | +} |
0 commit comments