Skip to content

Commit 9e5a543

Browse files
committed
fix: fix bugs and add noti on queue timeout
1 parent 0cdabaf commit 9e5a543

File tree

3 files changed

+91
-52
lines changed

3 files changed

+91
-52
lines changed

frontend/src/app/components/button/QueueButton.tsx

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { useState } from "react";
33
import useAccurateInterval from "../../hooks/useAccurateInterval";
44
import Button from "./Button";
55
import { RxCross1 } from "react-icons/rx";
6-
import { innkeeperWriteAtom } from "@/libs/room-jotai";
6+
import { innkeeperWriteAtom, isQueuingAtom } from "@/libs/room-jotai";
77
import { atom, useAtom, useSetAtom } from "jotai";
8+
import { notification } from "antd";
89

910
interface QueueButtonProps {
1011
enterQueue: () => void;
12+
selectedDifficulty: "EASY" | "MEDIUM" | "HARD";
1113
}
1214

1315
const triggerLeaveQueueAtom = atom(null, (get, set) => {
@@ -17,15 +19,32 @@ const triggerLeaveQueueAtom = atom(null, (get, set) => {
1719
});
1820
});
1921

20-
const QueueButton = ({ enterQueue }: QueueButtonProps) => {
22+
const QueueButton = ({ enterQueue, selectedDifficulty }: QueueButtonProps) => {
2123
const [time, setTime] = useState(0);
2224
const [isStarted, setIsStarted] = useState(false);
2325
const [_, handleLeaveQueue] = useAtom(triggerLeaveQueueAtom);
26+
const setIsQueueing = useSetAtom(isQueuingAtom);
27+
28+
const [api, contextHolder] = notification.useNotification();
29+
30+
const openNotification = () => {
31+
api.error({
32+
message: `Match not Found`,
33+
description: (
34+
<span>
35+
There appears to be no match for <b>{selectedDifficulty}</b> at this
36+
time. Please try again later or with another difficulty.
37+
</span>
38+
),
39+
placement: "top",
40+
});
41+
};
2442

2543
const handleClick = () => {
2644
setTime(0);
2745
if (isStarted) {
2846
handleLeaveQueue();
47+
setIsQueueing(false);
2948
} else {
3049
enterQueue();
3150
}
@@ -40,6 +59,8 @@ const QueueButton = ({ enterQueue }: QueueButtonProps) => {
4059
} else {
4160
handleLeaveQueue();
4261
setIsStarted(false);
62+
setIsQueueing(false);
63+
openNotification();
4364
}
4465
},
4566
isStarted ? 1000 : null,
@@ -56,6 +77,7 @@ const QueueButton = ({ enterQueue }: QueueButtonProps) => {
5677

5778
return (
5879
<div>
80+
{contextHolder}
5981
<Button
6082
className={`${isStarted ? "btn-accent" : "btn-success"} w-48`}
6183
onClick={handleClick}

frontend/src/app/components/matching/MatchingPage.tsx

Lines changed: 66 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { useState } from "react";
33
import QueueButton from "../button/QueueButton";
44
import { QuestionType } from "../../admin/question/page";
55
import { atom, useAtom } from "jotai";
6-
import { innkeeperWriteAtom } from "@/libs/room-jotai";
6+
import { innkeeperWriteAtom, isQueuingAtom } from "@/libs/room-jotai";
77
import { fetchAllQuestionsDoneByUser } from "@/app/api";
88
import { useQuery } from "@tanstack/react-query";
9-
import { Skeleton, Table } from "antd";
9+
import { Skeleton, Table, notification } from "antd";
1010

1111
const sendMatchRequestAtom = atom(
1212
null,
@@ -23,9 +23,17 @@ const MatchingPage = () => {
2323
questionDifficulty: "EASY" | "MEDIUM" | "HARD",
2424
) => void = useAtom(sendMatchRequestAtom)[1];
2525

26+
const [isQueueing, setIsQueueing] = useAtom(isQueuingAtom);
27+
2628
const [difficulty, setDifficulty] = useState<"EASY" | "MEDIUM" | "HARD">(
2729
"EASY",
2830
);
31+
32+
const handleChangeDiff = (difficulty: "EASY" | "MEDIUM" | "HARD") => {
33+
if (!isQueueing) {
34+
setDifficulty(difficulty);
35+
}
36+
};
2937
const activityTableColumns: any = [
3038
{
3139
title: "Question",
@@ -105,55 +113,63 @@ const MatchingPage = () => {
105113
console.log({ allQuestions });
106114

107115
return (
108-
<main className="flex h-full flex-col items-center justify-center">
109-
<section className="flex items-center gap-4">
110-
<label>
111-
<span>Difficulty Setting:</span>
112-
</label>
113-
<div className="join">
114-
<button
115-
type="button"
116-
className={`btn btn-primary join-item text-white ${
117-
difficulty == "EASY" && "btn-success"
118-
}`}
119-
onClick={() => setDifficulty("EASY")}
120-
>
121-
Easy
122-
</button>
123-
<button
124-
type="button"
125-
className={`btn btn-primary join-item text-white ${
126-
difficulty == "MEDIUM" && "btn-warning"
127-
}`}
128-
onClick={() => setDifficulty("MEDIUM")}
129-
>
130-
Medium
131-
</button>
132-
<button
133-
type="button"
134-
className={`btn btn-primary join-item text-white ${
135-
difficulty == "HARD" && "btn-error"
136-
}`}
137-
onClick={() => setDifficulty("HARD")}
138-
>
139-
Hard
140-
</button>
116+
<>
117+
<main className="flex h-full flex-col items-center justify-center">
118+
<section className="flex items-center gap-4">
119+
<label>
120+
<span>Difficulty Setting:</span>
121+
</label>
122+
<div className="join">
123+
<button
124+
type="button"
125+
className={`btn btn-primary join-item text-white ${
126+
difficulty == "EASY" && "btn-success"
127+
}`}
128+
onClick={() => handleChangeDiff("EASY")}
129+
>
130+
Easy
131+
</button>
132+
<button
133+
type="button"
134+
className={`btn btn-primary join-item text-white ${
135+
difficulty == "MEDIUM" && "btn-warning"
136+
}`}
137+
onClick={() => handleChangeDiff("MEDIUM")}
138+
>
139+
Medium
140+
</button>
141+
<button
142+
type="button"
143+
className={`btn btn-primary join-item text-white ${
144+
difficulty == "HARD" && "btn-error"
145+
}`}
146+
onClick={() => handleChangeDiff("HARD")}
147+
>
148+
Hard
149+
</button>
150+
</div>
151+
<QueueButton
152+
enterQueue={() => {
153+
setIsQueueing(true);
154+
sendMatchRequest(difficulty);
155+
}}
156+
selectedDifficulty={difficulty}
157+
/>
158+
</section>
159+
<div className="m-7">
160+
<h1 className="mb-2 block text-5xl font-bold text-white underline">
161+
Completed Questions
162+
</h1>
163+
<Table
164+
className="mt-4"
165+
bordered
166+
columns={activityTableColumns}
167+
dataSource={allQuestions as any}
168+
pagination={{ position: ["bottomCenter"] }}
169+
/>
141170
</div>
142-
<QueueButton enterQueue={() => sendMatchRequest(difficulty)} />
143-
</section>
144-
<div className="m-7">
145-
<h1 className="mb-2 block text-5xl font-bold text-white underline">
146-
Completed Questions
147-
</h1>
148-
<Table
149-
className="mt-4"
150-
bordered
151-
columns={activityTableColumns}
152-
dataSource={allQuestions as any}
153-
pagination={{ position: ["bottomCenter"] }}
154-
/>
155-
</div>
156-
</main>
171+
</main>
172+
</>
157173
);
158174
};
159175

frontend/src/libs/room-jotai.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const codeLangAtom = atom("python");
1212
export const socketAtom = atom<InnkeeperSocket | null>(null);
1313
export const isConnectedAtom = atom(false);
1414
export const isQuestionModalOpenAtom = atom(false);
15+
export const isQueuingAtom = atom(false);
1516

1617
/**
1718
* Note that the server only supports unmatched or matched, closed is a frontend

0 commit comments

Comments
 (0)