From b51f83f2173767364db6d84d6d60433e55579930 Mon Sep 17 00:00:00 2001 From: ukkodeveloper Date: Thu, 19 Oct 2023 09:41:27 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20nickname=20=EB=B3=80=EA=B2=BD=20api=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/features/member/hooks/useNickname.ts | 17 +++++++++++------ .../src/features/member/remotes/nickname.ts | 11 +++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/frontend/src/features/member/hooks/useNickname.ts b/frontend/src/features/member/hooks/useNickname.ts index 377fff13..1e718561 100644 --- a/frontend/src/features/member/hooks/useNickname.ts +++ b/frontend/src/features/member/hooks/useNickname.ts @@ -1,4 +1,4 @@ -import { useMemo, useState } from 'react'; +import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { useAuthContext } from '@/features/auth/components/AuthProvider'; import { updateNickname } from '@/features/member/remotes/nickname'; @@ -7,15 +7,20 @@ import { useMutation } from '@/shared/hooks/useMutation'; const useNickname = () => { const { user, logout } = useAuthContext(); + // TODO: 피드백 반영하여 error throw. 그러나 error 핸들링 반드시 필요 + if (!user) { + throw new Error('현재 user 로그인된 정보가 없습니다.'); + } + + const [nicknameEntered, setNicknameEntered] = useState(user.nickname); - const [nicknameEntered, setNicknameEntered] = useState(user?.nickname); const [nicknameErrorMessage, setNicknameErrorMessage] = useState('이전과 다른 닉네임으로 변경해주세요.'); - const mutateNickname = useMemo( - () => updateNickname(user?.memberId, nicknameEntered), - [nicknameEntered, user?.memberId] + + const { mutateData: changeNickname } = useMutation(() => + updateNickname(user.memberId, nicknameEntered) ); - const { mutateData: changeNickname } = useMutation(mutateNickname); + const navigate = useNavigate(); const hasError = nicknameErrorMessage.length !== 0; diff --git a/frontend/src/features/member/remotes/nickname.ts b/frontend/src/features/member/remotes/nickname.ts index 5a3b2cd5..a900c289 100644 --- a/frontend/src/features/member/remotes/nickname.ts +++ b/frontend/src/features/member/remotes/nickname.ts @@ -1,8 +1,7 @@ import fetcher from '@/shared/remotes'; -export const updateNickname = - (memberId: number | undefined, nickname: string | undefined) => () => { - return fetcher(`/members/${memberId}/nickname`, 'PATCH', { - nickname, - }); - }; +export const updateNickname = (memberId: number, nickname: string) => { + return fetcher(`/members/${memberId}/nickname`, 'PATCH', { + nickname, + }); +};