Skip to content

Commit

Permalink
feat: nickname 변경 api 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
ukkodeveloper committed Oct 19, 2023
1 parent eacc7e0 commit b51f83f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
17 changes: 11 additions & 6 deletions frontend/src/features/member/hooks/useNickname.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand Down
11 changes: 5 additions & 6 deletions frontend/src/features/member/remotes/nickname.ts
Original file line number Diff line number Diff line change
@@ -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,
});
};

0 comments on commit b51f83f

Please sign in to comment.