-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #238 from Kernel360/feature-my-page-api
페이지 기능: 마이페이지 api 기능 추가
- Loading branch information
Showing
8 changed files
with
100 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,4 +86,4 @@ | |
"msw": { | ||
"workerDirectory": "public" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: '[email protected]', | ||
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 ( | ||
<> | ||
<Header /> | ||
|
@@ -67,15 +89,15 @@ function ProfilePage() { | |
<Spacing size={12} /> | ||
<DropdownField | ||
label="성별" | ||
selectedLabel={watch('gender')} | ||
selectedLabel={GENDER_MAP[watch('gender') as GenderType]} | ||
type="profile" | ||
options={GENDER_OPTIONS} | ||
{...register('gender')} | ||
/> | ||
<Spacing size={12} /> | ||
<DropdownField | ||
label="연령대" | ||
selectedLabel={watch('age')} | ||
selectedLabel={AGE_MAP[watch('age') as AgeType]} | ||
type="profile" | ||
options={AGE_OPTIONS} | ||
{...register('age')} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { UserInfoType } from '../../types/auth'; | ||
import { getRequest } from '../requests.api'; | ||
|
||
/* ----- 내 정보 조회 ----- */ | ||
export const getProfile = async () => { | ||
const response = await getRequest<UserInfoType>('/mypage/member'); | ||
|
||
return response; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { IUpdateProfile } from '../../types/auth'; | ||
import { ICommon } from '../../types/common'; | ||
import { patchRequest } from '../requests.api'; | ||
|
||
/* ----- 내 정보 조회 ----- */ | ||
export const updateProfile = async ({ gender, age }: IUpdateProfile) => { | ||
const response = await patchRequest<ICommon<null>, IUpdateProfile>('/mypage/member', { | ||
gender, | ||
age, | ||
}); | ||
|
||
return response; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import { getProfile } from '@remote/api/requests/my-page/myPage.get.api'; | ||
import { UserInfoType } from '@remote/api/types/auth'; | ||
|
||
function useProfile() { | ||
return useQuery<UserInfoType>({ queryKey: ['profile'], queryFn: getProfile }); | ||
} | ||
|
||
export default useProfile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { toast } from 'react-toastify'; | ||
|
||
import { useMutation } from '@tanstack/react-query'; | ||
import { useRouter } from 'next/navigation'; | ||
|
||
import { updateProfile } from '@/remote/api/requests/my-page/myPage.patch.api'; | ||
|
||
function useUpdateProfile() { | ||
const router = useRouter(); | ||
const onSuccess = () => { | ||
router.push('/'); | ||
toast.success('회원정보가 수정되었습니다.'); | ||
}; | ||
const onError = () => { | ||
toast.error('유효하지 않은 값입니다.'); | ||
}; | ||
|
||
return useMutation({ mutationFn: updateProfile, onSuccess, onError }); | ||
} | ||
|
||
export default useUpdateProfile; |