Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] 방장이 아닌 사용자가 카운트다운 후 게임 시작 안되는 오류 해결 #294

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import Button from '@/components/common/Button/Button';

interface StartButtonProps {
show: () => void;
startCountdown: () => void;
}

const StartButton = ({ show, startCountdown }: StartButtonProps) => {
const { memberInfo, handleGameStart } = useGameStart({ showModal: show, startCountdown });
const StartButton = ({ show }: StartButtonProps) => {
const { memberInfo, handleGameStart } = useGameStart({ showModal: show });

return (
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ const isServerError = (status: number) => status >= 500 && status !== 555;

interface UseGameStartProps {
showModal: () => void;
startCountdown: () => void;
}

export const useGameStart = ({ showModal, startCountdown }: UseGameStartProps) => {
export const useGameStart = ({ showModal }: UseGameStartProps) => {
const [memberInfo, setMemberInfo] = useRecoilState(memberInfoState);
const { roomId } = useParams();
const { show } = useToast();
Expand All @@ -36,7 +35,6 @@ export const useGameStart = ({ showModal, startCountdown }: UseGameStartProps) =
const handleGameStart = () => {
if (memberInfo.isMaster) {
startGameMutation.mutate();
startCountdown();
}
Comment on lines 35 to 38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🌸 칭찬 🌸

그렇네요, 이렇게하면 master 일때만 startCountdown() 함수가 실행되겠군요

};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import useCountdown from './hooks/useCountdown';
import StartButton from './StartButton/StartButton';
import AlertModal from '../common/AlertModal/AlertModal';

import { useGetRoomInfo } from '@/hooks/useGetRoomInfo';
import useModal from '@/hooks/useModal';

const StartButtonContainer = () => {
const { isOpen, show, close } = useModal();
const { isCountdownStart, startCountdown, goToGame } = useCountdown();
const { isGameStart } = useGetRoomInfo();
const { isCountdownStart, goToGame } = useCountdown({ isGameStart });

return (
<>
{isCountdownStart && <Countdown goToGame={goToGame} />}
<StartButton show={show} startCountdown={startCountdown} />
<StartButton show={show} />
<AlertModal
isOpen={isOpen}
onClose={close}
Expand Down
16 changes: 13 additions & 3 deletions frontend/src/components/StartButtonContainer/hooks/useCountdown.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { useNavigate, useParams } from 'react-router-dom';

import { ROUTES } from '@/constants/routes';

const useCountdown = () => {
interface UseCountdownProps {
isGameStart: boolean;
}

const useCountdown = ({ isGameStart }: UseCountdownProps) => {
const navigate = useNavigate();
const { roomId } = useParams();
const [isCountdownStart, setIsCountdownStart] = useState(false);
Expand All @@ -16,7 +20,13 @@ const useCountdown = () => {
navigate(ROUTES.game(Number(roomId)));
};

return { isCountdownStart, startCountdown, goToGame };
useEffect(() => {
if (isGameStart) {
startCountdown();
}
}, [isGameStart]);

return { isCountdownStart, goToGame };
};

export default useCountdown;
1 change: 1 addition & 0 deletions frontend/src/hooks/useGetRoomInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ export const useGetRoomInfo = () => {
members: data?.members,
roomSetting: data?.roomSetting,
master: data?.master,
isGameStart: data?.isGameStart,
};
};
Loading