"use client"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { useRouter } from "next/navigation"; import { useAuth } from "@/hooks/use-auth"; import { Me, UpdateProfileSchema, updateProfile } from "@/lib/api"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardFooter } from "@/components/ui/card"; import { Form, FormField, FormItem, FormLabel, FormControl, FormMessage, FormDescription, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; interface EditProfileFormProps { currentUser: Me; } export function EditProfileForm({ currentUser }: EditProfileFormProps) { const router = useRouter(); const { token } = useAuth(); const form = useForm>({ resolver: zodResolver(UpdateProfileSchema), defaultValues: { displayName: currentUser.displayName ?? undefined, bio: currentUser.bio ?? undefined, avatarUrl: currentUser.avatarUrl ?? undefined, headerUrl: currentUser.headerUrl ?? undefined, customCss: currentUser.customCss ?? undefined, topFriends: currentUser.topFriends ?? [], }, }); async function onSubmit(values: z.infer) { if (!token) return; toast.info("Updating your profile..."); try { await updateProfile(values, token); toast.success("Profile updated successfully!"); // Redirect to the profile page to see the changes router.push(`/users/${currentUser.username}`); router.refresh(); // Ensure fresh data is loaded } catch (err) { toast.error("Failed to update profile."); } } return (
( Display Name )} /> ( Bio