"use client" import { useTransition } from "react" import { zodResolver } from "@hookform/resolvers/zod" import { useForm } from "react-hook-form" import { z } from "zod" import { Loader2, Save } from "lucide-react" import { toast } from "sonner" import { Button } from "@/shared/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle, CardFooter } from "@/shared/components/ui/card" import { Input } from "@/shared/components/ui/input" import { Label } from "@/shared/components/ui/label" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/shared/components/ui/select" import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "@/shared/components/ui/form" import { UserProfile } from "@/modules/users/data-access" import { updateUserProfile } from "@/modules/users/actions" const profileFormSchema = z.object({ name: z.string().min(2, "Name must be at least 2 characters."), email: z.string().email().optional(), // Read only role: z.string().optional(), // Read only phone: z.string().optional(), address: z.string().optional(), gender: z.string().optional(), age: z.coerce.number().min(0).optional(), }) type ProfileFormValues = z.infer export function ProfileSettingsForm({ user }: { user: UserProfile }) { const [isPending, startTransition] = useTransition() const form = useForm({ // eslint-disable-next-line @typescript-eslint/no-explicit-any resolver: zodResolver(profileFormSchema) as any, defaultValues: { name: user.name ?? "", email: user.email ?? "", role: user.role ?? "", phone: user.phone ?? "", address: user.address ?? "", gender: user.gender ?? "", age: user.age ?? undefined, }, }) function onSubmit(data: ProfileFormValues) { startTransition(async () => { try { await updateUserProfile({ name: data.name, phone: data.phone || undefined, address: data.address || undefined, gender: data.gender || undefined, age: data.age || undefined, }) toast.success("Profile updated successfully") } catch (error) { toast.error("Failed to update profile") console.error(error) } }) } return ( Profile Information Update your personal information.
( Full Name )} /> ( Email Email cannot be changed. )} /> ( Phone )} /> ( Gender )} /> ( Age )} /> ( Role )} /> ( Address )} />
) }