Skip to content

Commit

Permalink
refactor: 상수화, props 계층 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
mahwin committed Nov 29, 2023
1 parent fbfa634 commit b3781f8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
8 changes: 5 additions & 3 deletions frontend/src/components/SocketTimer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ interface Props {
socket: Socket;
isConnected: boolean;
endsAt: Date;
pingTime: number;
socketEvent: string;
onTimeout?: () => void;
}

export default function SocketTimer(props: Props) {
let { socket, endsAt, isConnected, onTimeout } = props;
let { socket, endsAt, isConnected, onTimeout, pingTime, socketEvent } = props;
// 대회 시간 검증이 안 되어 있어서, 끝나는 시간이 현재 시간보다 모두 전입니다. 그래서 지금 시간 기준으로 120분 더하고 마지막 시간이다라고 가정합니다.
const min = 120;
endsAt = new Date(new Date().getTime() + min * 60 * 1000);

const { remainMiliSeconds, isTimeout } = useSocketTimer({
socket,
endsAt,
socketEvent: 'ping',
pingTime: 5000,
socketEvent,
pingTime,
});

useEffect(() => {
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/hooks/timer/useSocketTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export default function useSocketTimer({ socket, endsAt, socketEvent, pingTime }
const [remainMiliSeconds, setRemainMiliSeconds] = useState<number>(-1);

useEffect(() => {
if (pingIntervalId.current) clearInterval(pingIntervalId.current);

socket.emit(socketEvent);
socket.on(socketEvent, handlePingMessage);

Expand Down Expand Up @@ -50,5 +52,6 @@ export default function useSocketTimer({ socket, endsAt, socketEvent, pingTime }
setIsTimeout(true);
}
}, [remainMiliSeconds]);

return { remainMiliSeconds, isTimeout };
}
4 changes: 4 additions & 0 deletions frontend/src/pages/ContestPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { isNil } from '@/utils/type';
const RUN_SIMULATION = '테스트 실행';
const CANCEL_SIMULATION = '실행 취소';
const DASHBOARD_URL = '/contest/dashboard';
const COMPEITION_PING_TIME = 5 * 1000;
const COMPEITION_SOCKET_EVENT = 'ping';

export default function ContestPage() {
const { id } = useParams<{ id: string }>();
Expand Down Expand Up @@ -107,6 +109,8 @@ export default function ContestPage() {
socket={socket.current}
isConnected={isConnected}
endsAt={new Date(endsAt)}
pingTime={COMPEITION_PING_TIME}
socketEvent={COMPEITION_SOCKET_EVENT}
onTimeout={handleTimeout}
/>
</section>
Expand Down

0 comments on commit b3781f8

Please sign in to comment.