Skip to content

Commit b23c658

Browse files
committed
Add stub functions to periodically query resource blob
1 parent cc96d18 commit b23c658

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

peerprep/components/questionpage/Matchmaking.tsx

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,58 @@
11
"use client";
2-
import React from "react";
2+
import React, { useEffect, useState } from "react";
33
import { useRouter } from "next/navigation";
44
import PeerprepButton from "../shared/PeerprepButton";
55
import { useQuestionFilter } from "@/contexts/QuestionFilterContext";
6+
const QUERY_INTERVAL_MILLISECONDS = 3000;
7+
const usePeriodicCallback = (
8+
callback: () => void,
9+
intervalTime: number,
10+
isActive: boolean
11+
) => {
12+
useEffect(() => {
13+
if (!isActive) return;
14+
15+
const interval = setInterval(callback, intervalTime);
16+
17+
return () => clearInterval(interval);
18+
}, [callback, intervalTime, isActive]);
19+
};
620

721
const Matchmaking = () => {
822
const router = useRouter();
23+
const [isMatching, setIsMatching] = useState(false);
924
const { difficulty, topics } = useQuestionFilter();
1025

1126
const handleMatch = () => {
12-
console.log("Match attempted");
13-
console.log("Selected Difficulty:", difficulty);
14-
console.log("Selected Topics:", topics);
27+
if (!isMatching) {
28+
setIsMatching(true);
29+
console.log("Match attempted");
30+
console.log("Selected Difficulty:", difficulty);
31+
console.log("Selected Topics:", topics);
32+
} else {
33+
setIsMatching(false);
34+
console.debug("User stopped matching");
35+
}
36+
1537
// username as userid?
1638
// should probably just use the questionlist selections as params
1739
};
1840

41+
const queryResource = () => {
42+
console.debug("Querying resource blob for matchmaking status");
43+
};
44+
45+
usePeriodicCallback(queryResource, QUERY_INTERVAL_MILLISECONDS, isMatching);
46+
1947
return (
2048
// TODO: move this to some admin panel or something
2149
<div className="p-4 space-x-4">
2250
<PeerprepButton onClick={() => router.push(`questions/new`)}>
2351
Add Question
2452
</PeerprepButton>
25-
<PeerprepButton onClick={handleMatch}>Find Match</PeerprepButton>
53+
<PeerprepButton onClick={handleMatch}>
54+
{isMatching ? "Cancel Match" : "Find Match"}
55+
</PeerprepButton>
2656
</div>
2757
);
2858
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
// maybe store SAFE user info and wrap the whole damn app in it
2+
// username, userid?, userstate (matching, idle, inmenu)

peerprep/middleware.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { cookies } from "next/headers";
22
import { NextResponse } from "next/server";
3-
import type { NextRequest } from "next/server";
3+
import { NextRequest } from "next/server";
44

55
function isNoSession(request: NextRequest): boolean {
66
return (
@@ -15,6 +15,11 @@ function isSession(request: NextRequest): boolean {
1515
}
1616

1717
export function middleware(request: NextRequest) {
18+
// TODO DELETE THIS LATER
19+
if (process.env.NEXT_BYPASS_LOGIN === "yesplease") {
20+
return NextResponse.next();
21+
}
22+
1823
if (isNoSession(request)) {
1924
return NextResponse.redirect(new URL("/questions", request.url));
2025
}

0 commit comments

Comments
 (0)