"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { Loader2, Save } from "lucide-react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; const formSchema = z.object({ name: z.string().min(1, "Name is required"), }); type FormData = z.infer; interface User { id: string; name: string; email: string; } interface ProfileFormProps { user: User; } export function ProfileForm({ user }: ProfileFormProps) { const router = useRouter(); const [isLoading, setIsLoading] = useState(false); const form = useForm({ resolver: zodResolver(formSchema), defaultValues: { name: user.name || "", }, }); async function onSubmit(data: FormData) { setIsLoading(true); try { const response = await fetch("/api/settings/profile", { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data), }); if (!response.ok) { const result = await response.json(); throw new Error(result.error || "Failed to update profile"); } toast.success("Profile updated successfully"); router.refresh(); } catch (error) { toast.error(error instanceof Error ? error.message : "An error occurred"); } finally { setIsLoading(false); } } return (

Email cannot be changed

( Name )} /> ); }