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

[REFACTOR] 게임화면에서 선택 버튼을 누르지 않아도 옵션이 선택되도록 리팩토링 #269

Merged
merged 23 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a97403b
merge: conflict 해결 #261
rbgksqkr Sep 6, 2024
30495a6
refactor: SelectButton 쿼리 호출 제거하고 props로 받기 #261
rbgksqkr Sep 6, 2024
b8723e8
refactor: SelectContainer hooks 디렉토리로 분리 #261
rbgksqkr Sep 6, 2024
7ff3866
refactor: 라운드 타이머가 바뀔 때마다 실행하지 않고, 타이머가 끝날 때만 clearTimeout #261
rbgksqkr Sep 6, 2024
5b15ddc
merge: conflict 해결 #261
rbgksqkr Sep 14, 2024
1b9d1bf
refactor: Timer hooks 디렉토리로 분리 #261
rbgksqkr Sep 14, 2024
f8fa798
refactor: 타임아웃되도 선택 API를 호출하도록 구조 리팩토링 #261
rbgksqkr Sep 14, 2024
e8735a3
test: 게임 화면 UI 테스트를 위해 storybook 작성 #261
rbgksqkr Sep 14, 2024
aa4419b
design: timer 구조 변경으로 인한 스타일 수정 #261
rbgksqkr Sep 14, 2024
0267606
design: 대기 화면 공통 레이아웃 적용 #261
rbgksqkr Sep 14, 2024
408bec7
refactor: 시간 재는 타이머 관련 로직을 API 로직과 분리 #261
rbgksqkr Sep 14, 2024
ab85a2c
refactor: 투표 시간 측정 타이머에 맞게 voteTimer로 이름 수정 #261
rbgksqkr Sep 14, 2024
b22e1a6
refactor: 선택 완료 버튼 클릭 이벤트리스너명 vote로 수정 #261
rbgksqkr Sep 14, 2024
bbd04ff
test: 타이머가 종료되었을 때 선택된 옵션이 있으면 투표 테스트 코드 작성 #261
rbgksqkr Sep 14, 2024
309a7bb
refactor: 투표 종료 여부를 Timer 컴포넌트가 가지면서 리렌더링 최적화 #261
rbgksqkr Sep 14, 2024
55cedd6
refactor: roundVoteIsFinished 네이밍을 voteIsFinished로 수정 #261
rbgksqkr Sep 14, 2024
0fa5023
refactor: Timer를 SelectContainer 하위 디렉토리로 위치 수정 #261
rbgksqkr Sep 14, 2024
85ce80b
refactor: useTimer 의존성 배열 수정 #261
rbgksqkr Sep 16, 2024
20d4ddf
refactor: timer 유틸함수 분리 #261
rbgksqkr Sep 16, 2024
2a572d7
refactor: 모호한 함수명 수정 #261
rbgksqkr Sep 19, 2024
2f8e096
refactor: refetchInterval 및 refetchIntervalInBackground 를 통해 게임 화면으로 …
rbgksqkr Sep 19, 2024
70b2a92
fix: 투표를 한 상태여도 타이머가 끝난 후 투표 API 요청을 하는 오류 해결 #261
rbgksqkr Sep 19, 2024
b127e40
test: 타이머가 종료되었을 때 이미 투표를 했다면 또 투표를 하지 않는 테스트 코드 구현 #261
rbgksqkr Sep 19, 2024
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
4 changes: 3 additions & 1 deletion frontend/.storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ const preview: Preview = {
<ThemeProvider theme={Theme}>
<MemoryRouter initialEntries={['/']}>
<Global styles={GlobalStyle} />
<Story />
<ToastProvider>
<Story />
</ToastProvider>
Comment on lines +41 to +43
Copy link
Contributor

Choose a reason for hiding this comment

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

💭 질문 💭

image

현재 PR의 storybook 링크에서 ReadyMembersContainer 컴포넌트의 ToastContext 부분에서 에러가 발생하는데 확인 한 번 부탁합니다

Copy link
Contributor Author

@rbgksqkr rbgksqkr Sep 19, 2024

Choose a reason for hiding this comment

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

음 확인해보니 포메 PR에 커밋이 푸시되서 decorator에 ToastProvider가 포함되지 않았던 것 같아요! 제 PR 머지되면 상관없을 것 같네용

</MemoryRouter>
</ThemeProvider>
</RecoilRoot>
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/apis/balanceContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface myGameStatusParams {
currentRound: number;
}

interface RoundVoteIsFinished {
interface VoteIsFinished {
isFinished: boolean;
}
Comment on lines -22 to 24
Copy link
Contributor

Choose a reason for hiding this comment

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

💭 질문 💭

RoundVote라는 용어가 모두 Vote로 바뀌었는데
그럼 이제 Round라는 용어는 사용이 안되게 바뀌었나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

음 라운드 결과에서 다음 게임 화면으로 넘어가는 부분에서도 roundIsFinished 라는 필드가 쓰여 헷갈려서 바꿨습니다! 포메는 어떤가요??


Expand Down Expand Up @@ -113,12 +113,12 @@ export const fetchMatchingResult = async ({
};

// 현재 라운드가 끝났는지 여부 확인하기
export const fetchRoundVoteIsFinished = async ({
export const fetchVoteIsFinished = async ({
contentId,
roomId,
}: ContentResultParams): Promise<RoundVoteIsFinished> => {
}: ContentResultParams): Promise<VoteIsFinished> => {
const res = await fetcher.get({
url: API_URL.roundVoteIsFinished(roomId, contentId),
url: API_URL.voteIsFinished(roomId, contentId),
});

const data = await res.json();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const categoryContainerLayout = css`
flex-direction: column;
justify-content: space-between;
align-items: center;
width: 100%;
height: 10rem;
padding: 1.6rem 0 2.4rem;
border-radius: ${getBorderRadius('medium')};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const readyMembersContainerLayout = css`
display: flex;
flex-direction: column;
gap: 2rem;
width: 100%;
`;

export const membersContainer = css`
Expand Down
73 changes: 0 additions & 73 deletions frontend/src/components/SelectContainer/SelectContainer.hook.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { css } from '@emotion/react';

export const selectContainerLayout = css`
display: flex;
flex-basis: 45%;
flex-basis: 40%;
flex-direction: column;
justify-content: center;
justify-content: space-between;
align-items: center;
gap: 5rem;
gap: 5.6rem;
width: 100%;
`;

export const selectSection = css`
Expand Down
17 changes: 10 additions & 7 deletions frontend/src/components/SelectContainer/SelectContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useParams } from 'react-router-dom';

import { useRoundIsFinished, useSelectOption } from './SelectContainer.hook';
import useSelectOption from './hooks/useSelectOption';
import { selectContainerLayout, selectSection } from './SelectContainer.styled';
import Timer from './Timer/Timer';
import AlertModal from '../common/AlertModal/AlertModal';
import SelectButton from '../common/SelectButton/SelectButton';

Expand All @@ -11,17 +12,18 @@ import useModal from '@/hooks/useModal';

const SelectContainer = () => {
const { roomId } = useParams();
const { balanceContent, isFetching } = useBalanceContentQuery(Number(roomId));
const { balanceContent } = useBalanceContentQuery(Number(roomId));
const { selectedOption, handleClickOption, completeSelection } = useSelectOption();
const { isOpen, show, close } = useModal();

useRoundIsFinished({
contentId: balanceContent?.contentId,
isFetching,
});

return (
<div css={selectContainerLayout}>
<Timer
selectedId={selectedOption.id}
isVoted={selectedOption.isCompleted}
completeSelection={completeSelection}
showModal={show}
/>
<section css={selectSection}>
<SelectOption
option={balanceContent.firstOption}
Expand All @@ -36,6 +38,7 @@ const SelectContainer = () => {
/>
</section>
<SelectButton
contentId={balanceContent.contentId}
selectedId={selectedOption.id}
completeSelection={completeSelection}
showModal={show}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const shake = keyframes`
export const timerLayout = css`
display: flex;
position: relative;
flex-basis: 5%;
Copy link
Contributor

Choose a reason for hiding this comment

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

💭 질문 💭

storybook에서 Timer UI를 봤는데 감이 잘 안 잡혀서요. flex-basis: 5%;는 원래는 어떤 역할을 하고 있었나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

flex-basis는 flex box 내에서의 비율을 의미합니다! 그래서 원래는 Content의 레이아웃에 따라 비율을 가졌는데, Timer가 SelectContainer 내부로 들어가서 부모 레이아웃의 비율이 달라져 수정하였습니다!

align-items: center;
width: 100%;
height: 3.2rem;
Expand Down
73 changes: 73 additions & 0 deletions frontend/src/components/SelectContainer/Timer/Timer.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { act, renderHook } from '@testing-library/react';

import useTimer from './hooks/useTimer';
import { convertMsecToSecond, formatLeftRoundTime } from './Timer.util';

describe('Timer 테스트', () => {
describe('formatTimer 유틸 함수 테스트', () => {
it('30초의 제한시간을 입력받으면 00:30 으로 포맷팅한 string값을 반환한다.', () => {
const formattedTimer = formatLeftRoundTime(30);

expect(formattedTimer).toBe('00:30');
});

it('10000 밀리초의 제한시간을 입력받으면 10초를 number 타입으로 반환한다.', () => {
const convertedTime = convertMsecToSecond(10000);

expect(convertedTime).toBe(10);
});
});
describe('Timer 훅 테스트', () => {
jest.useFakeTimers();
const voteMock = jest.fn();
const timeLimit = 10000;

it('타이머가 종료되었을 때 선택 완료를 누르지 않아도 선택된 옵션이 있으면 투표한다.', () => {
const isSelectedOption = true;
const isVoted = false;

const { result } = renderHook(() =>
useTimer({ timeLimit, isSelectedOption, isVoted, vote: voteMock }),
);

// act : 인자로 받은 함수를 실행시켜서 가상의 DOM(jsdom)에 적용하는 역할
// 상태 변경과 그로 인한 DOM 업데이트가 모두 완료된 후에 테스트가 실행되도록 보장
act(() => {
jest.advanceTimersByTime(timeLimit);
});
Comment on lines +35 to +37
Copy link
Contributor

Choose a reason for hiding this comment

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

🌸 칭찬 🌸

신기한 기능을 하는 함수네요. 이렇게 하면 타이머 기능을 테스트할 때에도 타이머 시간을 다 기다리지 않아도 되겠군요!


expect(result.current.leftRoundTime).toBe(0);
expect(voteMock).toHaveBeenCalled();
});
it('타이머가 종료되었을 때 이미 투표를 했다면 또 투표를 하지 않는다.', () => {
const isSelectedOption = true;
const isVoted = true;

const { result } = renderHook(() =>
useTimer({ timeLimit, isSelectedOption, isVoted, vote: voteMock }),
);

act(() => {
jest.advanceTimersByTime(timeLimit);
});

expect(result.current.leftRoundTime).toBe(0);
expect(voteMock).not.toHaveBeenCalled();
});
it('타이머가 종료되었을 때 선택된 옵션이 없다면 투표되지 않고 기권한다.', () => {
const isSelectedOption = false;
const isVoted = false;

const { result } = renderHook(() =>
useTimer({ timeLimit, isSelectedOption, isVoted, vote: voteMock }),
);

act(() => {
jest.advanceTimersByTime(timeLimit);
});

expect(result.current.leftRoundTime).toBe(0);
expect(voteMock).not.toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useParams } from 'react-router-dom';

import useRoundTimer from './Timer.hook';
import useVoteTimer from './hooks/useVoteTimer';
Copy link
Contributor

Choose a reason for hiding this comment

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

🌸 칭찬 🌸

안 그래도 커밋대로 차근 차근 보다가 useRoudTimer와 useTimer에 대해 네이밍이 헷갈린다고 생각했는데 수정 좋네요 따봉 !!!!!!!!!!

import {
timerIcon,
timerIconShake,
Expand All @@ -10,12 +10,33 @@ import {
timerWrapper,
} from './Timer.styled';
import { formatLeftRoundTime } from './Timer.util';
import useVoteIsFinished from '../hooks/useVoteIsFinished';

import DdangkongTimer from '@/assets/images/ddangkongTimer.png';
import useBalanceContentQuery from '@/hooks/useBalanceContentQuery';

const Timer = () => {
interface TimerProps {
selectedId: number;
isVoted: boolean;
completeSelection: () => void;
showModal: () => void;
}

const Timer = ({ selectedId, isVoted, completeSelection, showModal }: TimerProps) => {
const { roomId } = useParams();
const { barWidthPercent, leftRoundTime, isAlmostFinished } = useRoundTimer(Number(roomId));
const { balanceContent, isFetching } = useBalanceContentQuery(Number(roomId));
const { barWidthPercent, leftRoundTime, isAlmostFinished } = useVoteTimer({
roomId: Number(roomId),
selectedId,
isVoted,
completeSelection,
showModal,
});

useVoteIsFinished({
contentId: balanceContent.contentId,
isFetching,
});

return (
<section css={timerLayout}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ export const convertMsecToSecond = (msec: number) => {
const UNIT_MSEC = POLLING_DELAY;
return msec / UNIT_MSEC;
};

export const calculateUnitRatio = (total: number, divisor: number) => {
return total / divisor;
};
Original file line number Diff line number Diff line change
@@ -1,45 +1,54 @@
import { useEffect, useRef, useState } from 'react';

import { convertMsecToSecond } from './Timer.util';
import { calculateUnitRatio, convertMsecToSecond } from '../Timer.util';

import { POLLING_DELAY } from '@/constants/config';
import useBalanceContentQuery from '@/hooks/useBalanceContentQuery';

const INITIAL_WIDTH = 100;
const DEFAULT_TIME_LIMIT_MSEC = 10000;
const ALMOST_FINISH_SECOND = 5;

const useRoundTimer = (roomId: number) => {
const { balanceContent } = useBalanceContentQuery(roomId);
const timeLimit = balanceContent.timeLimit || DEFAULT_TIME_LIMIT_MSEC;
interface UseTimerProps {
timeLimit: number;
isSelectedOption: boolean;
isVoted: boolean;
vote: () => void;
}

const useTimer = ({ timeLimit, isSelectedOption, isVoted, vote }: UseTimerProps) => {
const [leftRoundTime, setLeftRoundTime] = useState(convertMsecToSecond(timeLimit));
const [barWidthPercent, setBarWidthPercent] = useState(INITIAL_WIDTH);

const isVoteTimeout = leftRoundTime <= 0;
const isAlmostFinished = leftRoundTime <= ALMOST_FINISH_SECOND;

const timeout = useRef<NodeJS.Timeout>();

useEffect(() => {
if (leftRoundTime <= 0) {
if (isVoteTimeout) {
if (isSelectedOption && !isVoted) {
vote();
}

clearInterval(timeout.current);
}
}, [leftRoundTime]);
}, [isVoteTimeout, isSelectedOption, isVoted, vote]);

useEffect(() => {
const DECREASE_RATE = INITIAL_WIDTH / convertMsecToSecond(timeLimit);
setLeftRoundTime(convertMsecToSecond(timeLimit));
const timeLimitPerSecond = convertMsecToSecond(timeLimit);
const DECREASE_PERCENT = calculateUnitRatio(INITIAL_WIDTH, timeLimitPerSecond);
setLeftRoundTime(timeLimitPerSecond);

timeout.current = setInterval(() => {
setLeftRoundTime((prev) => prev - 1);
setBarWidthPercent((prevWidth) => (prevWidth > 0 ? prevWidth - DECREASE_RATE : 0));
setBarWidthPercent((prevWidth) => (prevWidth > 0 ? prevWidth - DECREASE_PERCENT : 0));
}, POLLING_DELAY);

return () => {
clearInterval(timeout.current);
};
}, [balanceContent, timeLimit]);
}, [timeLimit]);

return { leftRoundTime, barWidthPercent, isAlmostFinished };
};

export default useRoundTimer;
export default useTimer;
Loading
Loading