104 lines
3.7 KiB
TypeScript
104 lines
3.7 KiB
TypeScript
import { zodResolver } from '@hookform/resolvers/zod'
|
|
import { createFileRoute } from '@tanstack/react-router'
|
|
import { useState } from 'react'
|
|
import { useForm } from 'react-hook-form'
|
|
import { z } from 'zod'
|
|
|
|
import { Button } from '@/components/ui/button'
|
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
|
|
import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel } from '@/components/ui/field'
|
|
import { Input } from '@/components/ui/input'
|
|
import { Switch } from '@/components/ui/switch'
|
|
import { useSession } from '@/lib/auth-client'
|
|
|
|
export const Route = createFileRoute('/_authenticated/settings')({
|
|
component: SettingsPage,
|
|
})
|
|
|
|
const profileSchema = z.object({
|
|
name: z.string().min(1, 'Name is required').max(80),
|
|
email: z.email('Enter a valid email address'),
|
|
})
|
|
|
|
type ProfileValues = z.infer<typeof profileSchema>
|
|
|
|
function SettingsPage() {
|
|
const { data: session } = useSession()
|
|
const [saved, setSaved] = useState(false)
|
|
|
|
const form = useForm<ProfileValues>({
|
|
resolver: zodResolver(profileSchema),
|
|
defaultValues: {
|
|
name: session?.user.name ?? '',
|
|
email: session?.user.email ?? '',
|
|
},
|
|
})
|
|
|
|
return (
|
|
<div className="grid max-w-3xl gap-4 md:gap-6">
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Profile</CardTitle>
|
|
<CardDescription>This is how other people will see you.</CardDescription>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<form
|
|
onSubmit={form.handleSubmit(() => {
|
|
// Wire this to a `PATCH /users/me` feature on the API.
|
|
setSaved(true)
|
|
})}
|
|
>
|
|
<FieldGroup>
|
|
<Field data-invalid={!!form.formState.errors.name}>
|
|
<FieldLabel htmlFor="name">Name</FieldLabel>
|
|
<Input id="name" placeholder="Ada Lovelace" {...form.register('name')} />
|
|
<FieldError errors={[form.formState.errors.name]} />
|
|
</Field>
|
|
|
|
<Field data-invalid={!!form.formState.errors.email}>
|
|
<FieldLabel htmlFor="email">Email</FieldLabel>
|
|
<Input
|
|
id="email"
|
|
type="email"
|
|
placeholder="ada@example.com"
|
|
{...form.register('email')}
|
|
/>
|
|
<FieldDescription>We only use it to sign you in.</FieldDescription>
|
|
<FieldError errors={[form.formState.errors.email]} />
|
|
</Field>
|
|
|
|
<div className="flex items-center gap-4">
|
|
<Button type="submit" disabled={form.formState.isSubmitting}>
|
|
Save changes
|
|
</Button>
|
|
{saved && <span className="text-muted-foreground text-sm">Saved locally.</span>}
|
|
</div>
|
|
</FieldGroup>
|
|
</form>
|
|
</CardContent>
|
|
</Card>
|
|
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Appearance</CardTitle>
|
|
<CardDescription>Notifications and display preferences.</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="flex flex-col gap-4">
|
|
{[
|
|
{ id: 'digest', label: 'Weekly digest', hint: 'A summary of your lists every Monday.' },
|
|
{ id: 'mentions', label: 'Mentions', hint: 'Ping me when someone mentions my name.' },
|
|
].map((option) => (
|
|
<div key={option.id} className="flex items-center justify-between gap-4">
|
|
<div>
|
|
<p className="text-sm font-medium">{option.label}</p>
|
|
<p className="text-muted-foreground text-sm">{option.hint}</p>
|
|
</div>
|
|
<Switch defaultChecked aria-label={option.label} />
|
|
</div>
|
|
))}
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
)
|
|
}
|