Skip to content

Commit 7111e13

Browse files
authored
Merge pull request #99 from CS3219-AY2324S1/add-timer-to-matching-form
Add timer
2 parents cad5ba5 + bd61053 commit 7111e13

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

MatchingService/src/matching/matching.service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export function initializeMatchingService(io: Server) {
1212
socket.on('startMatching', (preferences: any) => {
1313
// Add the user to the list of active users with their preferences
1414
activeUsers.push({ socket, preferences });
15+
//console.log('user is in queue to match with another user');
1516

1617
// Attempt to find a match for the user
1718
tryMatchForUser(socket, preferences);
@@ -61,6 +62,7 @@ export function initializeMatchingService(io: Server) {
6162
removeUserFromActiveList(matchedUser.socket);
6263
const randomSeed = Date.now();
6364
// Emit "matchFound" to both users
65+
//console.log('match found');
6466
socket.emit('matchFound', {matchedUserPreferences: matchedUser.preferences, seed: randomSeed});
6567
matchedUser.socket.emit('matchFound', {matchedUserPreferences: preferences, seed: randomSeed});
6668
} else {

frontend/src/components/MatchingService/MatchingForm.tsx

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ const MatchingForm = React.forwardRef(function MatchingForm() {
6565
const userEmail = user?.email;
6666
const [openSnackbar, setOpenSnackbar] = React.useState(false);
6767
const [snackbarMessage, setSnackbarMessage] = React.useState("");
68+
const [remainingTime, setRemainingTime] = React.useState(0); // State for the countdown timer
69+
const [timerId, setTimerId] = React.useState<NodeJS.Timeout | null>(null); // State to keep track of the timer
70+
71+
72+
const startCountdown = () => {
73+
setRemainingTime(30); // Start the timer at 30 seconds
74+
setIsMatching(true);
75+
// Clear any existing timer
76+
if (timerId) clearInterval(timerId);
77+
// Start a new timer
78+
const id = setInterval(() => {
79+
setRemainingTime((time) => time - 1);
80+
}, 1000);
81+
setTimerId(id);
82+
};
6883

6984
const handleConnect = async () => {
7085
const preferences = {
@@ -88,8 +103,24 @@ const MatchingForm = React.forwardRef(function MatchingForm() {
88103

89104
socket.emit("startMatching", preferences);
90105
setIsMatching(true);
106+
startCountdown();
91107
};
92108

109+
React.useEffect(() => {
110+
if (remainingTime === 0 && timerId) {
111+
clearInterval(timerId);
112+
setIsMatching(false); // Update isMatching state to false as matching ends
113+
setSnackbarMessage("Matching timed out after 30 seconds.");
114+
setOpenSnackbar(true);
115+
}
116+
}, [remainingTime, timerId]);
117+
118+
React.useEffect(() => {
119+
return () => {
120+
if (timerId) clearInterval(timerId);
121+
};
122+
}, [timerId]);
123+
93124
const generateConsistentRandomIndex = (seed: any, arrayLength: number) => {
94125
return seed % arrayLength;
95126
};
@@ -127,6 +158,8 @@ const MatchingForm = React.forwardRef(function MatchingForm() {
127158
getCategories();
128159
}, []);
129160

161+
162+
130163
React.useEffect(() => {
131164
socket.on("matchFound", async (matchedUserPreferences) => {
132165
console.log("Match Found:", matchedUserPreferences);
@@ -203,7 +236,10 @@ const MatchingForm = React.forwardRef(function MatchingForm() {
203236
</select>
204237
</div>
205238
{isMatching ? (
206-
<div>Loading...</div>
239+
<div>Loading...
240+
<p>Time remaining: {remainingTime} seconds</p>
241+
</div>
242+
207243
) : (
208244
<Button sx={{ mt: "5%" }} variant="contained" onClick={handleConnect}>
209245
Connect
@@ -229,4 +265,4 @@ const MatchingForm = React.forwardRef(function MatchingForm() {
229265
);
230266
});
231267

232-
export default MatchingForm;
268+
export default MatchingForm;

0 commit comments

Comments
 (0)