From 8f7d0f46d9f3359b4950a9b2cc66f2d314632e11 Mon Sep 17 00:00:00 2001 From: bottlewook Date: Sat, 2 Mar 2024 00:32:54 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=94=84=EB=A1=9C=ED=95=84=20=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=20api=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20#205?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/(my-page)/my-page/page.tsx | 9 ++--- src/app/(my-page)/my-page/profile/page.tsx | 44 ++++++++++++++++------ 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/src/app/(my-page)/my-page/page.tsx b/src/app/(my-page)/my-page/page.tsx index 8333d1c6..9a8d03fe 100644 --- a/src/app/(my-page)/my-page/page.tsx +++ b/src/app/(my-page)/my-page/page.tsx @@ -1,7 +1,6 @@ 'use client'; import { useEffect, useState } from 'react'; -import { useCookies } from 'react-cookie'; import classNames from 'classnames/bind'; import Link from 'next/link'; @@ -24,12 +23,12 @@ const TOP_MARGIN = 96; const BOTTOM_MARGIN = 97; function MyProfilePage() { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const [cookies, removeCookie] = useCookies(['token']); const logout = useLoggedOut(); - // eslint-disable-next-line max-len - const userId = useAppSelector((state) => { return state.user.id; }, (prev, curr) => { return prev === curr; }); + const userId = useAppSelector( + (state) => { return state.user.id; }, + (prev, curr) => { return prev === curr; }, + ); const [isLoggedIn, setIsLoggedIn] = useState(false); diff --git a/src/app/(my-page)/my-page/profile/page.tsx b/src/app/(my-page)/my-page/profile/page.tsx index 18efd744..564f5af9 100644 --- a/src/app/(my-page)/my-page/profile/page.tsx +++ b/src/app/(my-page)/my-page/profile/page.tsx @@ -1,11 +1,17 @@ 'use client'; +import { useEffect, useMemo } from 'react'; import { useForm } from 'react-hook-form'; import dynamic from 'next/dynamic'; +import { + AGE_MAP, AgeType, GENDER_MAP, GenderType, +} from '@constants/dropdownMap'; import { AGE_OPTIONS, GENDER_OPTIONS } from '@constants/myPage'; import VALIDATION_MESSAGE_MAP from '@constants/validationMessage'; +import useProfile from '@remote/queries/my-page/useProfile'; +import useUpdateProfile from '@remote/queries/my-page/useUpdateProfile'; import DropdownField from '@shared/dropdown-field/DropdownField'; import Header from '@shared/header/Header'; import Spacing from '@shared/spacing/Spacing'; @@ -16,26 +22,42 @@ const FixedBottomButton = dynamic(() => { return import('@shared/fixedBottomButt }); function ProfilePage() { - // TODO: api or store를 통해 defaultValue 설정하기 + const { data: profile } = useProfile(); + const { mutate } = useUpdateProfile(); + const { - register, watch, formState: { + register, watch, reset, formState: { errors, isDirty, isValid, }, getValues, } = useForm({ - defaultValues: { - id: 'washpedia123', - email: 'washpedia@gmail.com', - gender: '남성', - age: '20', - }, + defaultValues: useMemo(() => { + return { + id: profile?.value.id, + email: profile?.value.email, + gender: profile?.value.gender, + age: profile?.value.age, + }; + }, [profile?.value.age, profile?.value.email, profile?.value.gender, profile?.value.id]), mode: 'onBlur', }); const onSubmit = () => { // eslint-disable-next-line no-console - console.log(getValues()); + const { gender, age } = getValues(); + if (gender != null && age != null) { + mutate({ gender, age }); + } }; + useEffect(() => { + reset({ + id: profile?.value.id, + email: profile?.value.email, + gender: profile?.value.gender, + age: profile?.value.age, + }); + }, [profile?.value.age, profile?.value.email, profile?.value.gender, profile?.value.id, reset]); + return ( <>
@@ -67,7 +89,7 @@ function ProfilePage() {