From ca731fb137465408a3c1729c13d785f7857e67e0 Mon Sep 17 00:00:00 2001 From: schererleander Date: Fri, 26 Dec 2025 16:09:31 +0100 Subject: refactor: modularize settings form into smaller components --- src/app/settings/profile-form.tsx | 115 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/app/settings/profile-form.tsx (limited to 'src/app/settings/profile-form.tsx') diff --git a/src/app/settings/profile-form.tsx b/src/app/settings/profile-form.tsx new file mode 100644 index 0000000..6f532f0 --- /dev/null +++ b/src/app/settings/profile-form.tsx @@ -0,0 +1,115 @@ +"use client" + +import { useState } from "react" +import { zodResolver } from "@hookform/resolvers/zod" +import { useForm } from "react-hook-form" +import { Loader2, User, Save } from "lucide-react" +import { toast } from "sonner" +import { Session } from "next-auth" + +import { Button } from "@/components/ui/button" +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" +import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form" +import { Input } from "@/components/ui/input" +import { updateProfileSchema, type UpdateProfileInput } from "@/lib/validation" + +interface ProfileFormProps { + user: { + name?: string | null + email?: string | null + } + update: (data?: { name?: string | null; email?: string | null; image?: string | null }) => Promise +} + +export function ProfileForm({ user, update }: ProfileFormProps) { + const [isLoading, setIsLoading] = useState(false) + + const form = useForm({ + resolver: zodResolver(updateProfileSchema), + defaultValues: { + name: user.name || "", + email: user.email || "", + }, + }) + + const onSubmit = async (data: UpdateProfileInput) => { + setIsLoading(true) + + try { + const response = await fetch("/api/user/profile", { + method: "PATCH", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(data), + }) + + const result = await response.json() + + if (!response.ok) { + toast.error(result.error || "Failed to update profile") + return + } + + await update({ + name: data.name, + email: data.email, + }) + + toast.success("Profile updated successfully!") + } catch { + toast.error("An unexpected error occurred") + } finally { + setIsLoading(false) + } + } + + return ( + + + + + Profile Information + + + Update your personal information + + + +
+ + ( + + Full Name + + + + + + )} + /> + ( + + Email Address + + + + + + )} + /> + + + +
+
+ ) +} -- cgit v1.3.1