Skip to content

Commit

Permalink
hotfix: 유령 캐릭터 발생 에러 해결
Browse files Browse the repository at this point in the history
- 닉네임 입력 후 enter 클릭 시 여러개의 요청이 발생하는 것을 form으로 해결
  • Loading branch information
dooohun committed Dec 4, 2024
1 parent 7cfa8ee commit 77753e1
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions packages/client/src/pages/nickname/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,31 @@ export default function Nickname() {
const toast = toastController();
const [nickname, setNickname] = useState('');

const handleNicknameSubmit = async (nickname: string) => {
const socket = getQuizSocket();
const sid = await emitEventWithAck<string>(socket, 'session', {
pinCode: pinCode,
nickname: nickname,
});
if (!sid) {
toast.warning('방이 가득 찼습니다.');
navigate(`/`);
}
setCookie('sid', sid);
socket.emit('participant notice', { pinCode: pinCode });
navigate(`/quiz/wait/${pinCode}`);
};
const handleNicknameSubmit = async (e: React.FormEvent) => {
e.preventDefault();

const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') {
handleNicknameSubmit(nickname);
const socket = getQuizSocket();
try {
const sid = await emitEventWithAck<string>(socket, 'session', {
pinCode: pinCode,
nickname: nickname,
});
if (!sid) {
toast.warning('방이 가득 찼습니다.');
navigate(`/`);
return;
}
setCookie('sid', sid);
socket.emit('participant notice', { pinCode: pinCode });
navigate(`/quiz/wait/${pinCode}`);
} catch (error) {
toast.error('닉네임 등록 중 문제가 발생했습니다.');
}
};

return (
<div className="flex flex-col items-center px-64 pt-16 min-w-[980px]">
<div className="flex flex-col items-center gap-3 w-fit">
<form className="flex flex-col items-center gap-3 w-fit" onSubmit={handleNicknameSubmit}>
<label
htmlFor="nickname"
className="flex gap-2 ml-1 items-center text-xl font-bold self-start"
Expand All @@ -59,21 +60,19 @@ export default function Nickname() {
className="w-[488px] bg-transparent focus:outline-none"
value={nickname}
onChange={(e) => setNickname(e.target.value)}
onKeyDown={handleKeyDown}
maxLength={MAX_NICKNAME_LENGTH}
/>
<button
type="button"
type="submit"
className={`w-20 h-10 text-white bg-primary rounded-3xl ${
nickname.length === 0 ? 'opacity-50 cursor-not-allowed' : 'opacity-100'
}`}
onClick={() => handleNicknameSubmit(nickname)}
disabled={nickname.length === 0}
>
참가하기
</button>
</div>
</div>
</form>
</div>
);
}

0 comments on commit 77753e1

Please sign in to comment.