Skip to content

Commit e8a1082

Browse files
authored
Merge pull request #89 from CS3219-AY2425S1/improvement/amqp_over_ws
Bug fixes and remove redundant features
2 parents f961011 + 30ff2fe commit e8a1082

File tree

4 files changed

+40
-24
lines changed

4 files changed

+40
-24
lines changed

peerprep-fe/src/app/(main)/match/page.tsx

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,53 @@ import { User, Code } from 'lucide-react';
66
import { Button } from '@/components/ui/button';
77
import { useAuthStore } from '@/state/useAuthStore';
88
import { consumeMessageFromQueue, sendMessageToQueue } from '@/lib/rabbitmq';
9+
import { UserMatchingResponse } from '@/types/types';
910

1011
export default function LoadingPage() {
1112
const [elapsedTime, setElapsedTime] = useState(0);
12-
const [usersWaiting, setUsersWaiting] = useState(0);
1313
const [matchStatus, setMatchStatus] = useState('searching');
1414
const router = useRouter();
1515
const { user } = useAuthStore();
1616
const listenerInitialized = useRef(false);
1717

1818
// Function to consume messages from the RabbitMQ queue
1919
const handleStartListening = () => {
20-
const onMessageReceived = (message: any) => {
20+
const onMessageReceived = (message: UserMatchingResponse) => {
2121
if (message.status == "matched") {
22-
console.log('Match found, your partner is', message.match?.user);
22+
console.log('Match found, your partner is', message.match.name);
2323
setMatchStatus('matched');
2424
} else {
2525
console.log('Match failed');
2626
setMatchStatus('failed');
2727
}
2828
};
29-
consumeMessageFromQueue(user?.id!, onMessageReceived);
29+
if (user?.id) {
30+
consumeMessageFromQueue(user.id, onMessageReceived);
31+
} else {
32+
console.warn("User ID is undefined. This is not supposed to happen.");
33+
}
3034
};
3135

3236
useEffect(() => {
3337
if (!listenerInitialized.current) {
34-
setElapsedTime(0);
35-
setMatchStatus('searching');
36-
handleStartListening(); // Set up listener only once
37-
38+
handleStartListening();
3839
listenerInitialized.current = true; // Mark as initialized
40+
}
3941

40-
const interval = setInterval(() => {
41-
setElapsedTime((prevTime) => prevTime + 1);
42-
}, 1000);
42+
setElapsedTime(0);
43+
setMatchStatus('searching');
4344

44-
// Cleanup function
45-
return () => {
46-
clearInterval(interval); // Clean up interval on unmount
47-
};
48-
}
45+
const interval = setInterval(() => {
46+
setElapsedTime((prevTime) => prevTime + 1);
47+
}, 1000);
48+
49+
// Cleanup function
50+
return () => {
51+
clearInterval(interval); // Clean up interval on unmount
52+
};
4953
}, []);
5054

5155
useEffect(() => {
52-
console.log(usersWaiting);
5356
if (elapsedTime >= 60 && matchStatus === 'searching') {
5457
console.log('Elapsed time reached 60 seconds. Match timed out.');
5558
setMatchStatus('timeout');
@@ -95,10 +98,6 @@ export default function LoadingPage() {
9598
Time elapsed: {elapsedTime} seconds
9699
</div>
97100
</div>
98-
<div className="flex items-center space-x-2 text-sm">
99-
<User className="h-4 w-4" />
100-
<span>{usersWaiting} users waiting</span>
101-
</div>
102101
<Button
103102
onClick={handleCancel}
104103
className="w-full max-w-md bg-purple-600 hover:bg-purple-700"

peerprep-fe/src/components/dialogs/PreMatch.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { TopicsPopover } from '@/app/(main)/components/filter/TopicsPopover';
1515
import { sendMessageToQueue } from '@/lib/rabbitmq';
1616
import { axiosAuthClient } from '@/network/axiosClient';
1717
import { DIFFICULTY_OPTIONS } from '@/lib/constants';
18+
import { UserMatchingRequest } from '@/types/types';
1819

1920
export function PreMatch() {
2021
const [open, setOpen] = useState(false);
@@ -25,7 +26,7 @@ export function PreMatch() {
2526
const handleConfirm = async () => {
2627
try {
2728
const profileDetails = await getProfileDetails();
28-
const message = {
29+
const message : UserMatchingRequest = {
2930
_id: profileDetails.id,
3031
name: profileDetails.username,
3132
topic: selectedTopics[0] || '', // TODO: change to list, but current backend only accepts 1

peerprep-fe/src/lib/rabbitmq.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import { UserMatchingRequest, UserMatchingResponse } from '@/types/types';
23
import { Client, IFrame, IMessage } from '@stomp/stompjs';
34

45
let isConnected = false;
56

6-
const sendMessageToQueue = async (message: Record<string, any>) => {
7+
const sendMessageToQueue = async (message: UserMatchingRequest) => {
78
const uri = process.env.NEXT_PUBLIC_RABBITMQ_URL;
89
const user = process.env.NEXT_PUBLIC_RABBITMQ_USER;
910
const pass = process.env.NEXT_PUBLIC_RABBITMQ_PW;
@@ -52,7 +53,7 @@ const sendMessageToQueue = async (message: Record<string, any>) => {
5253

5354
const consumeMessageFromQueue = async (
5455
queue: string,
55-
onMessage: (message: any) => void,
56+
onMessage: (message: UserMatchingResponse) => void,
5657
) => {
5758
const uri = process.env.NEXT_PUBLIC_RABBITMQ_URL;
5859
const user = process.env.NEXT_PUBLIC_RABBITMQ_USER;

peerprep-fe/src/types/types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,26 @@ interface User {
4848
username: string;
4949
}
5050

51+
interface UserMatchingRequest {
52+
_id: string;
53+
name?: string;
54+
difficulty?: string;
55+
topic?: string;
56+
type: string;
57+
}
58+
59+
interface UserMatchingResponse {
60+
status: string;
61+
match: UserMatchingRequest;
62+
}
63+
5164
export type {
5265
Problem,
5366
ProblemDialogData,
5467
ProblemRequestData,
5568
FilterBadgeProps,
5669
FilterSelectProps,
5770
User,
71+
UserMatchingResponse,
72+
UserMatchingRequest,
5873
};

0 commit comments

Comments
 (0)