"use client"; import { useState, useTransition } from "react"; import { updateUserName, type FormData } from "@/actions/update-user-name"; import { zodResolver } from "@hookform/resolvers/zod"; import { User } from "@prisma/client"; import { useSession } from "next-auth/react"; import { useTranslations } from "next-intl"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { userNameSchema } from "@/lib/validations/user"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { SectionColumns } from "@/components/dashboard/section-columns"; import { Icons } from "@/components/shared/icons"; interface UserNameFormProps { user: Pick; } export function UserNameForm({ user }: UserNameFormProps) { const { update } = useSession(); const [updated, setUpdated] = useState(false); const [isPending, startTransition] = useTransition(); const updateUserNameWithId = updateUserName.bind(null, user.id); const t = useTranslations("Setting"); const checkUpdate = (value) => { setUpdated(user.name !== value); }; const { handleSubmit, register, formState: { errors }, } = useForm({ resolver: zodResolver(userNameSchema), defaultValues: { name: user?.name || "", }, }); const onSubmit = handleSubmit((data) => { startTransition(async () => { const { status } = await updateUserNameWithId(data); if (status !== "success") { toast.error("Something went wrong.", { description: "Your name was not updated. Please try again.", }); } else { await update(); setUpdated(false); toast.success("Your name has been updated."); } }); }); return (
checkUpdate(e.target.value)} />
{errors?.name && (

{errors.name.message}

)}

{t("Max 32 characters")}

); }