Skip to content

Commit 4b4f805

Browse files
committed
Merge branch 'fix-timer' into frontend-changes
2 parents d32a601 + 9d3bda7 commit 4b4f805

File tree

6 files changed

+75
-37
lines changed

6 files changed

+75
-37
lines changed

apps/frontend/src/app/matching/MatchingModal.tsx

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useState, useEffect } from 'react';
22
import {
33
Form,
4+
Button,
45
Modal,
56
} from 'antd';
67
import 'typeface-montserrat';
@@ -13,6 +14,7 @@ import MatchNotFoundContent from './modalContent/MatchNotFoundContent';
1314
import MatchCancelledContent from './modalContent/MatchCancelledContent';
1415
import useMatching from '../services/use-matching';
1516
import { ValidateUser } from '../services/user';
17+
import { useTimer } from 'react-timer-hook';
1618

1719
interface MatchingModalProps {
1820
isOpen: boolean;
@@ -23,12 +25,30 @@ export interface MatchParams {
2325
topics: string[],
2426
difficulties: string[],
2527
}
28+
const MATCH_TIMEOUT = 30;
29+
const JOIN_TIMEOUT = 5;
2630

2731
const MatchingModal: React.FC<MatchingModalProps> = ({ isOpen, close: _close }) => {
2832
const matchingState = useMatching();
2933
const [closedType, setClosedType] = useState<"finding" | "cancelled" | "joined">("finding");
30-
const [timeoutAfter, setTimeoutAfter] = useState<number>(9999);
3134
const isClosable = ["timeout", "closed"].includes(matchingState.state);
35+
const { totalSeconds, pause: pauseTimer, restart: restartTimer } = useTimer({
36+
expiryTimestamp: new Date(Date.now() + MATCH_TIMEOUT * 1000),
37+
autoStart: false,
38+
onExpire() {
39+
if (matchingState.state === "matching") {
40+
matchingState.timeout();
41+
return;
42+
}
43+
if (matchingState.state === "found") {
44+
matchingState.ok();
45+
setClosedType("joined");
46+
return;
47+
}
48+
console.warn(`matching is in ${matchingState.state}`)
49+
},
50+
});
51+
const passed = MATCH_TIMEOUT - totalSeconds;
3252

3353
function close() {
3454
// clean up matching and closedType State
@@ -49,42 +69,56 @@ const MatchingModal: React.FC<MatchingModalProps> = ({ isOpen, close: _close })
4969
});
5070
} : undefined;
5171

72+
useEffect(() => {
73+
if (matchingState.state === "cancelling" || matchingState.state === "timeout") {
74+
pauseTimer();
75+
return;
76+
}
77+
if (matchingState.state === "found") {
78+
restartTimer(
79+
new Date(Date.now() + JOIN_TIMEOUT * 1000),
80+
)
81+
}
82+
}, [matchingState])
83+
5284
const renderModalContent = () => {
5385
switch (matchingState.state) {
5486
case 'closed':
5587
switch (closedType) {
5688
case "finding":
57-
return <FindMatchContent beginMatch={matchingState.start}/>;
89+
return <FindMatchContent beginMatch={params => {
90+
restartTimer(
91+
new Date(Date.now() + MATCH_TIMEOUT * 1000),
92+
);
93+
matchingState.start(params);
94+
}}/>;
5895
case "cancelled":
5996
return <MatchCancelledContent
6097
reselect={() => {
6198
setClosedType("finding");
6299
}}
63-
canceledIn={timeoutAfter}
100+
canceledIn={passed}
64101
/>;
65102
case "joined":
66103
return <JoinedMatchContent
67-
cancel={() => {
68-
setClosedType("cancelled");
69-
}}
70-
name1={matchingState.info?.myName || ""}
71-
name2={matchingState.info?.partnerName || ""}
104+
cancel={() => {
105+
setClosedType("cancelled");
106+
}}
107+
name1={matchingState.info?.myName ?? ""}
108+
name2={matchingState.info?.partnerName ??""}
72109
/>;
73110
}
74111
case 'matching':
75112
return <MatchingInProgressContent
76-
cancelMatch={(timeoutAfter: number) => {
113+
cancelMatch={() => {
77114
setClosedType("cancelled");
78-
setTimeoutAfter(timeoutAfter);
79115
matchingState.cancel();
116+
pauseTimer();
80117
}}
81-
timeout={(timeoutAfter: number) => {
82-
matchingState.timeout()
83-
setTimeoutAfter(timeoutAfter);
84-
}}
118+
timePassed={passed}
85119
/>;
86120
case 'cancelling':
87-
return <MatchingInProgressContent cancelMatch={() => {}} timeout={() => {}}/>;
121+
return <MatchingInProgressContent cancelMatch={() => {}} timePassed={passed}/>;
88122
case 'starting':
89123
return <FindMatchContent beginMatch={() => {}}/>
90124
case 'found':
@@ -99,9 +133,10 @@ const MatchingModal: React.FC<MatchingModalProps> = ({ isOpen, close: _close })
99133
}}
100134
name1={matchingState.info.myName}
101135
name2={matchingState.info.partnerName}
102-
/>
136+
joiningIn={totalSeconds}
137+
/>
103138
case 'timeout':
104-
return <MatchNotFoundContent reselect={matchingState.ok} timedOutIn={10}/>;
139+
return <MatchNotFoundContent reselect={matchingState.ok} timedOutIn={passed}/>;
105140
default:
106141
throw new Error('Invalid matching state.');
107142
}

apps/frontend/src/app/matching/modalContent/FindMatchContent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const FindMatchContent: React.FC<Props> = ({ beginMatch }) => {
4141
</Form.Item>
4242
</div>
4343
<button className="find-match-button" type="submit">
44-
FIND
44+
FIND MATCH
4545
</button>
4646
</div>
4747
)

apps/frontend/src/app/matching/modalContent/JoinedMatchContent.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'typeface-montserrat';
99
import './styles.scss';
1010
import { handleCancelMatch } from '../handlers';
1111
import { formatTime } from '@/utils/DateTime';
12+
import { useStopwatch } from 'react-timer-hook';
1213

1314

1415
interface Props {
@@ -21,6 +22,9 @@ const JoinedMatchContent: React.FC<Props> = ({cancel, name1: me, name2: you}) =>
2122
const matchAlreadyJoined = () => {
2223
throw new Error('Match already joined.');
2324
}
25+
const { totalSeconds } = useStopwatch({
26+
autoStart: true
27+
})
2428

2529
return (
2630
<div className="joined-match-content">
@@ -45,7 +49,7 @@ const JoinedMatchContent: React.FC<Props> = ({cancel, name1: me, name2: you}) =>
4549
</div>
4650
<div className="match-status-label">Match Found!</div>
4751
<div className="match-status-message">
48-
Waiting for others... {formatTime(83)}
52+
Waiting for others... {formatTime(totalSeconds)}
4953
</div>
5054
<button className="joined-match-deactivated-button"
5155
disabled

apps/frontend/src/app/matching/modalContent/MatchFoundContent.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,18 @@ import 'typeface-montserrat';
55
import './styles.scss';
66
import { handleCancelMatch, handleJoinMatch } from '../handlers';
77
import { formatTime } from '@/utils/DateTime';
8-
import { useTimer } from "react-timer-hook"
98

109
interface Props {
1110
join(): void,
1211
cancel(): void,
1312
name1: string, // user's username
1413
name2: string, // matched user's username
14+
joiningIn: number,
1515
}
1616

1717
const TIMEOUT = 10;
1818

19-
const MatchFoundContent: React.FC<Props> = ({join, cancel, name1: me, name2: you}) => {
20-
const { totalSeconds } = useTimer({
21-
expiryTimestamp: new Date(Date.now() + 10 * 1000),
22-
onExpire: join
23-
});
19+
const MatchFoundContent: React.FC<Props> = ({join, cancel, name1: me, name2: you, joiningIn}) => {
2420

2521
return (
2622
<div className="matching-found-content">
@@ -45,7 +41,7 @@ const MatchFoundContent: React.FC<Props> = ({join, cancel, name1: me, name2: you
4541
</div>
4642
<div className="match-status-label">Match Found!</div>
4743
<div className="match-status-message">
48-
Joining in... {formatTime(totalSeconds)}
44+
Joining in... {formatTime(joiningIn)}
4945
</div>
5046
<button className="join-match-button"
5147
onClick={join}

apps/frontend/src/app/matching/modalContent/MatchingInProgressContent.tsx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,24 @@ import { handleCancelMatch } from '../handlers';
66
import { useTimer } from "react-timer-hook"
77
import {formatTime} from '@/utils/DateTime';
88

9-
const TIMEOUT = 10;
10-
119
interface Props {
12-
cancelMatch(cancelledIn: number): void
13-
timeout(timeoutIn: number): void
10+
cancelMatch(): void
11+
timePassed: number
1412
}
1513

16-
const MatchingInProgressContent: React.FC<Props> = ({cancelMatch: cancel, timeout}) => {
17-
const { totalSeconds } = useTimer({
18-
expiryTimestamp: new Date(Date.now() + 10 * 1000),
19-
onExpire: () => timeout(TIMEOUT - totalSeconds),
20-
});
14+
const MatchingInProgressContent: React.FC<Props> = ({cancelMatch: cancel, timePassed}) => {
2115

2216
return (
2317
<div className="matching-in-progess-content">
2418
<LoadingOutlined className="loading-icon"/>
2519
<div className="match-status-label">Matching in Progress</div>
2620
<div className="match-status-message">
2721
Please be patient as we match you with other online users<br/><br/>
28-
{formatTime(TIMEOUT - totalSeconds)}
22+
{formatTime(timePassed)}
2923
</div>
3024
<button className="cancel-match-button"
3125
onClick={() => {
32-
cancel(TIMEOUT - totalSeconds);
26+
cancel();
3327
}}
3428
>
3529
Cancel

apps/frontend/src/app/matching/modalContent/styles.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,12 @@ button:disabled, .joined-match-deactivated-button {
227227
height: 60px;
228228
fill: #463F3A;
229229
}
230+
231+
.user-caption {
232+
display: flex;
233+
justify-content: center;
234+
}
235+
236+
.avatar-caption-container {
237+
width: 64px;
238+
}

0 commit comments

Comments
 (0)