-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f33079
commit 7bb0169
Showing
3 changed files
with
111 additions
and
76 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useMemo, useState } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { useAuthContext } from '@/features/auth/components/AuthProvider'; | ||
import { updateNickname } from '@/features/member/remotes/nickname'; | ||
import ROUTE_PATH from '@/shared/constants/path'; | ||
import { useMutation } from '@/shared/hooks/useMutation'; | ||
|
||
const useNickname = () => { | ||
const { user, logout } = useAuthContext(); | ||
|
||
const [nicknameEntered, setNicknameEntered] = useState(user?.nickname); | ||
const [nicknameErrorMessage, setNicknameErrorMessage] = | ||
useState('이전과 다른 닉네임으로 변경해주세요.'); | ||
const mutateNickname = useMemo( | ||
() => updateNickname(user?.memberId, nicknameEntered), | ||
[nicknameEntered, user?.memberId] | ||
); | ||
const { mutateData: changeNickname } = useMutation(mutateNickname); | ||
const navigate = useNavigate(); | ||
|
||
const hasError = nicknameErrorMessage.length !== 0; | ||
const handleChangeNickname: React.ChangeEventHandler<HTMLInputElement> = (event) => { | ||
const currentNickname = event.currentTarget.value; | ||
setNicknameEntered(currentNickname); | ||
if (currentNickname.length < 2 || currentNickname.length > 10) { | ||
setNicknameErrorMessage('2글자 이상 10글자 이하 문자만 가능합니다.'); | ||
} else if (currentNickname === user?.nickname) { | ||
setNicknameErrorMessage('이전과 다른 닉네임으로 변경해주세요.'); | ||
} else { | ||
setNicknameErrorMessage(''); | ||
} | ||
}; | ||
|
||
const submitNicknameChanged = async () => { | ||
await changeNickname(); | ||
logout(); | ||
navigate(ROUTE_PATH.LOGIN); | ||
}; | ||
|
||
return { | ||
nicknameEntered, | ||
nicknameErrorMessage, | ||
hasError, | ||
handleChangeNickname, | ||
submitNicknameChanged, | ||
setNicknameErrorMessage, | ||
}; | ||
}; | ||
|
||
export default useNickname; |
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,23 @@ | ||
import { useNavigate } from 'react-router-dom'; | ||
import { useAuthContext } from '@/features/auth/components/AuthProvider'; | ||
import { deleteMember } from '@/features/member/remotes/member'; | ||
import ROUTE_PATH from '@/shared/constants/path'; | ||
import { useMutation } from '@/shared/hooks/useMutation'; | ||
|
||
const useWithdrawal = () => { | ||
const navigate = useNavigate(); | ||
const { user, logout } = useAuthContext(); | ||
const { mutateData: withdrawMember } = useMutation(deleteMember(user?.memberId)); | ||
|
||
const handleWithdrawal = async () => { | ||
await withdrawMember(); | ||
logout(); | ||
navigate(ROUTE_PATH.ROOT); | ||
}; | ||
|
||
return { | ||
handleWithdrawal, | ||
}; | ||
}; | ||
|
||
export default useWithdrawal; |
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