Skip to content

Commit 422e509

Browse files
committed
Add stopwatch for matchmaking and default difficulty to easy
1 parent a7f0a6c commit 422e509

File tree

5 files changed

+62
-12
lines changed

5 files changed

+62
-12
lines changed

peerprep/app/actions/server_actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export async function login(state: FormState, formData: FormData) {
6262
}
6363
}
6464

65-
export async function hydrateUid(): Promise<undefined|string> {
65+
export async function hydrateUid(): Promise<undefined | string> {
6666
if (!cookies().has("session")) {
6767
console.log("No session found - triggering switch back to login page.");
6868
redirect("/auth/login");

peerprep/components/questionpage/Matchmaking.tsx

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ import { useRouter } from "next/navigation";
44
import PeerprepButton from "../shared/PeerprepButton";
55
import { useQuestionFilter } from "@/contexts/QuestionFilterContext";
66
import { useUserInfo } from "@/contexts/UserInfoContext";
7-
import { isError, MatchRequest, MatchResponse } from "@/api/structs";
7+
import {
8+
Difficulty,
9+
isError,
10+
MatchRequest,
11+
MatchResponse,
12+
} from "@/api/structs";
813
import {
914
checkMatchStatus,
1015
findMatch,
1116
} from "@/app/api/internal/matching/helper";
17+
import ResettingStopwatch from "../shared/ResettingStopwatch";
1218

1319
const QUERY_INTERVAL_MILLISECONDS = 5000;
1420
const TIMEOUT_MILLISECONDS = 30000;
@@ -60,6 +66,18 @@ const Matchmaking = () => {
6066
}
6167
};
6268

69+
const getMatchMakingRequest = (): MatchRequest => {
70+
const matchRequest: MatchRequest = {
71+
userId: userid,
72+
// welp, just bandaid default easy
73+
difficulty: difficulty === Difficulty.All ? Difficulty.Easy : difficulty,
74+
topicTags: topics,
75+
requestTime: getMatchRequestTime(),
76+
};
77+
78+
return matchRequest;
79+
};
80+
6381
const handleMatch = async () => {
6482
if (!isMatching) {
6583
setIsMatching(true);
@@ -71,12 +89,7 @@ const Matchmaking = () => {
7189
}, TIMEOUT_MILLISECONDS);
7290

7391
// assemble the match request
74-
const matchRequest: MatchRequest = {
75-
userId: userid,
76-
difficulty: difficulty,
77-
topicTags: topics,
78-
requestTime: getMatchRequestTime(),
79-
};
92+
const matchRequest = getMatchMakingRequest();
8093
console.log("Match attempted");
8194
console.debug(matchRequest);
8295

@@ -126,9 +139,7 @@ const Matchmaking = () => {
126139
<PeerprepButton onClick={handleMatch}>
127140
{isMatching ? "Cancel Match" : "Find Match"}
128141
</PeerprepButton>
129-
{isMatching && (
130-
<div className="w-3 h-3 bg-difficulty-hard rounded-full ml-2" />
131-
)}
142+
{isMatching && <ResettingStopwatch isActive={isMatching} />}
132143
</div>
133144
</div>
134145
);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React, { useState, useEffect } from "react";
2+
3+
interface ResettingStopwatchProps {
4+
isActive: boolean;
5+
}
6+
7+
// pass isActive from parent component
8+
//
9+
const ResettingStopwatch: React.FC<ResettingStopwatchProps> = ({
10+
isActive,
11+
}) => {
12+
const [elapsedTime, setElapsedTime] = useState<number>(0);
13+
14+
useEffect(() => {
15+
let interval: NodeJS.Timeout | null = null;
16+
17+
if (isActive) {
18+
interval = setInterval(() => {
19+
setElapsedTime((prevTime) => prevTime + 1);
20+
}, 1000);
21+
}
22+
23+
return () => {
24+
if (interval) clearInterval(interval);
25+
setElapsedTime(0);
26+
};
27+
}, [isActive]);
28+
29+
const formatTime = (time: number) => {
30+
const minutes = Math.floor(time / 60);
31+
const seconds = time % 60;
32+
return `${minutes}:${seconds < 10 ? "0" : ""}${seconds}`;
33+
};
34+
35+
return <div>{formatTime(elapsedTime)}</div>;
36+
};
37+
38+
export default ResettingStopwatch;

peerprep/contexts/QuestionFilterContext.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const QuestionFilterProvider: React.FC<{ children: ReactNode }> = ({
1818
}) => {
1919
const [difficulty, setDifficulty] = useState<string>(Difficulty.All); // default to all
2020
const [topics, setTopics] = useState<string[]>(["all"]); // I guess default set this too the whole list of topics from questionlist
21+
//can also consider moving all the uniqu topics here?
2122

2223
return (
2324
<QuestionFilterContext.Provider

peerprep/middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function isSession(request: NextRequest): boolean {
1515
}
1616

1717
export function middleware(request: NextRequest) {
18-
// // UNCOMMENT AND ADD TO ENV IF JUST TESTING FRONTEND STUFF
18+
// UNCOMMENT AND ADD TO ENV IF JUST TESTING FRONTEND STUFF
1919
// if (process.env.NEXT_BYPASS_LOGIN === "yesplease") {
2020
// return NextResponse.next();
2121
// }

0 commit comments

Comments
 (0)