Skip to content

Commit

Permalink
refactor: Modal Context 게임 시작 부분 적용 #272
Browse files Browse the repository at this point in the history
  • Loading branch information
rbgksqkr committed Sep 20, 2024
1 parent 7aba249 commit 86db138
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ import { useParams } from 'react-router-dom';
import { useRecoilState } from 'recoil';

import { startGame } from '@/apis/room';
import AlertModal from '@/components/common/AlertModal/AlertModal';
import useModal from '@/hooks/useModal';
import useToast from '@/hooks/useToast';
import { memberInfoState } from '@/recoil/atom';
import { CustomError, NetworkError } from '@/utils/error';

const isServerError = (status: number) => status >= 500 && status !== 555;

export const useGameStart = (showModal: () => void) => {
export const useGameStart = () => {
const [memberInfo, setMemberInfo] = useRecoilState(memberInfoState);
const { roomId } = useParams();
const { show } = useToast();
const { show: showModal } = useModal();

const startGameMutation = useMutation({
mutationFn: () => startGame(Number(roomId)),
Expand All @@ -22,7 +25,7 @@ export const useGameStart = (showModal: () => void) => {
return;
}

showModal();
showModal(AlertModal, { title: '게임 시작 에러', message: error.message });
},
networkMode: 'always',
throwOnError: (error: CustomError) => isServerError(error.status),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
import StartButton from './StartButton/StartButton';
import AlertModal from '../common/AlertModal/AlertModal';

import useModal from '@/hooks/useModal';

const StartButtonContainer = () => {
const { isOpen, show, close } = useModal();

return (
<>
<StartButton show={show} />
<AlertModal
isOpen={isOpen}
onClose={close}
title="게임 시작 에러"
message="게임을 시작할 수 없습니다."
/>
</>
);
return <StartButton />;
};

export default StartButtonContainer;
19 changes: 8 additions & 11 deletions frontend/src/hooks/useModal.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { useState } from 'react';
import { useContext } from 'react';

const useModal = () => {
const [isOpen, setIsOpen] = useState(false);

const show = () => {
setIsOpen(true);
};
import { ModalDispatchContext } from '@/providers/ModalProvider/ModalProvider';

const close = () => {
setIsOpen(false);
};
const useModal = () => {
const dispatch = useContext(ModalDispatchContext);

return { isOpen, show, close };
if (dispatch === null) {
throw new Error('ModalDispatchContext가 존재하지 않습니다.');
}
return dispatch;
};
export default useModal;

0 comments on commit 86db138

Please sign in to comment.