-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
7 changed files
with
121 additions
and
72 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
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
32 changes: 32 additions & 0 deletions
32
frontend/src/pages/NicknamePage/hooks/useCreateOrEnterRoom.ts
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,32 @@ | ||
import { useRef } from 'react'; | ||
import { useParams } from 'react-router-dom'; | ||
|
||
import useCreateRoom from './useCreateRoom'; | ||
import useEnterRoom from './useEnterRoom'; | ||
|
||
const useCreateOrEnterRoom = () => { | ||
const nicknameInputRef = useRef<HTMLInputElement>(null); | ||
const { roomUuid } = useParams(); | ||
|
||
// roomUuId가 없다 -> 초대링크를 받지 않은 master이다. | ||
const isMaster = !roomUuid; | ||
|
||
const { createRoomMutation, handleCreateRoom } = useCreateRoom({ nicknameInputRef }); | ||
const { enterRoomMutation, handleEnterRoom } = useEnterRoom({ nicknameInputRef }); | ||
|
||
const handleCreateOrEnterRoom = () => { | ||
if (isMaster) { | ||
handleCreateRoom(); | ||
} else { | ||
handleEnterRoom(); | ||
} | ||
}; | ||
|
||
return { | ||
nicknameInputRef, | ||
handleCreateOrEnterRoom, | ||
isLoading: isMaster ? createRoomMutation.isPending : enterRoomMutation.isPending, | ||
}; | ||
}; | ||
|
||
export default useCreateOrEnterRoom; |
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,37 @@ | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { RefObject } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
import { createRoom } from '@/apis/room'; | ||
import { ROUTES } from '@/constants/routes'; | ||
import useThrottle from '@/hooks/useThrottle'; | ||
import { CreateOrEnterRoomResponse } from '@/types/room'; | ||
import { CustomError } from '@/utils/error'; | ||
|
||
interface useCreateRoomProps { | ||
nicknameInputRef: RefObject<HTMLInputElement>; | ||
} | ||
|
||
const useCreateRoom = ({ nicknameInputRef }: useCreateRoomProps) => { | ||
const navigate = useNavigate(); | ||
|
||
const createRoomMutation = useMutation<CreateOrEnterRoomResponse, CustomError, string>({ | ||
mutationFn: createRoom, | ||
onSuccess: (data) => { | ||
navigate(ROUTES.ready(Number(data.roomId)), { replace: true }); | ||
}, | ||
}); | ||
|
||
const throttledCreateRoom = useThrottle(createRoomMutation.mutate); | ||
|
||
const handleCreateRoom = () => { | ||
if (createRoomMutation.isPending) return; | ||
|
||
const nickname = nicknameInputRef.current?.value || nicknameInputRef.current?.placeholder || ''; | ||
throttledCreateRoom(nickname); | ||
}; | ||
|
||
return { createRoomMutation, handleCreateRoom }; | ||
}; | ||
|
||
export default useCreateRoom; |
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,42 @@ | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { RefObject } from 'react'; | ||
import { useNavigate, useParams } from 'react-router-dom'; | ||
|
||
import { enterRoom } from '@/apis/room'; | ||
import { ROUTES } from '@/constants/routes'; | ||
import useThrottle from '@/hooks/useThrottle'; | ||
import { CreateOrEnterRoomResponse } from '@/types/room'; | ||
import { CustomError } from '@/utils/error'; | ||
|
||
interface useCreateRoomProps { | ||
nicknameInputRef: RefObject<HTMLInputElement>; | ||
} | ||
|
||
const useEnterRoom = ({ nicknameInputRef }: useCreateRoomProps) => { | ||
const navigate = useNavigate(); | ||
const { roomUuid } = useParams(); | ||
|
||
const enterRoomMutation = useMutation< | ||
CreateOrEnterRoomResponse, | ||
CustomError, | ||
{ nickname: string; roomUuid: string } | ||
>({ | ||
mutationFn: ({ nickname, roomUuid }) => enterRoom(roomUuid, nickname), | ||
onSuccess: (data) => { | ||
navigate(ROUTES.ready(Number(data.roomId)), { replace: true }); | ||
}, | ||
}); | ||
|
||
const throttledEnterRoom = useThrottle(enterRoomMutation.mutate); | ||
|
||
const handleEnterRoom = () => { | ||
if (enterRoomMutation.isPending) return; | ||
|
||
const nickname = nicknameInputRef.current?.value || nicknameInputRef.current?.placeholder || ''; | ||
throttledEnterRoom({ nickname, roomUuid: roomUuid || '' }); | ||
}; | ||
|
||
return { enterRoomMutation, handleEnterRoom }; | ||
}; | ||
|
||
export default useEnterRoom; |
62 changes: 0 additions & 62 deletions
62
frontend/src/pages/NicknamePage/hooks/useMakeOrEnterRoom.ts
This file was deleted.
Oops, something went wrong.