|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import { useForm } from "react-hook-form"; |
| 5 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 6 | +import { authClient } from "@/lib/auth-client"; |
| 7 | +import { useToastStore } from "@/stores/useToastStore"; |
| 8 | +import { |
| 9 | + ProfileUpdateRequestSchema, |
| 10 | + type ProfileUpdateRequest, |
| 11 | +} from "@/schemas/auth/profile"; |
| 12 | +import { Button } from "@/components/ui/button"; |
| 13 | +import InputField from "@/components/common/InputField"; |
| 14 | +import { cn } from "@/lib/utils"; |
| 15 | + |
| 16 | +export default function ProfilePage() { |
| 17 | + const [isLoading, setIsLoading] = useState(true); |
| 18 | + const { showToast } = useToastStore(); |
| 19 | + |
| 20 | + const { |
| 21 | + register, |
| 22 | + handleSubmit, |
| 23 | + reset, |
| 24 | + formState: { errors, isSubmitting }, |
| 25 | + } = useForm<ProfileUpdateRequest>({ |
| 26 | + resolver: zodResolver(ProfileUpdateRequestSchema), |
| 27 | + defaultValues: { name: "", image: "" }, |
| 28 | + }); |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + const load = async () => { |
| 32 | + const { data } = await authClient.getSession(); |
| 33 | + if (data?.user) { |
| 34 | + const u = data.user as { name?: string; image?: string | null }; |
| 35 | + reset({ |
| 36 | + name: u.name || "", |
| 37 | + image: u.image || "", |
| 38 | + }); |
| 39 | + } |
| 40 | + setIsLoading(false); |
| 41 | + }; |
| 42 | + load(); |
| 43 | + }, [reset]); |
| 44 | + |
| 45 | + const onSubmit = async (payload: ProfileUpdateRequest) => { |
| 46 | + const { data, error } = await authClient.updateUser({ |
| 47 | + name: payload.name.trim(), |
| 48 | + image: payload.image?.trim() || undefined, |
| 49 | + }); |
| 50 | + |
| 51 | + if (error) { |
| 52 | + showToast(error.message || "수정에 실패했습니다.", "error"); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (data) { |
| 57 | + showToast("프로필이 수정되었습니다.", "success"); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + if (isLoading) { |
| 62 | + return ( |
| 63 | + <div className="flex min-h-[200px] items-center justify-center"> |
| 64 | + <span className="inline-block size-6 animate-spin rounded-full border-2 border-current border-t-transparent" /> |
| 65 | + </div> |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + return ( |
| 70 | + <form |
| 71 | + onSubmit={handleSubmit(onSubmit)} |
| 72 | + className="flex flex-col gap-6 max-w-md" |
| 73 | + > |
| 74 | + <InputField |
| 75 | + id="name" |
| 76 | + label="이름" |
| 77 | + register={register} |
| 78 | + errors={errors} |
| 79 | + autoComplete="name" |
| 80 | + /> |
| 81 | + <InputField |
| 82 | + id="image" |
| 83 | + label="프로필 이미지 URL" |
| 84 | + register={register} |
| 85 | + errors={errors} |
| 86 | + placeholder="https://..." |
| 87 | + /> |
| 88 | + <Button |
| 89 | + type="submit" |
| 90 | + disabled={isSubmitting} |
| 91 | + className={cn( |
| 92 | + "w-fit rounded-xl px-6 py-2 font-bold shadow-lg shadow-primary/20", |
| 93 | + "disabled:opacity-50" |
| 94 | + )} |
| 95 | + > |
| 96 | + {isSubmitting && ( |
| 97 | + <span |
| 98 | + className="mr-2 inline-block size-4 shrink-0 animate-spin rounded-full border-2 border-current border-t-transparent" |
| 99 | + aria-hidden |
| 100 | + /> |
| 101 | + )} |
| 102 | + 저장 |
| 103 | + </Button> |
| 104 | + </form> |
| 105 | + ); |
| 106 | +} |
0 commit comments