Skip to content

Commit 1675d46

Browse files
committed
Add stub for user context and request time
1 parent b23c658 commit 1675d46

File tree

3 files changed

+73
-9
lines changed

3 files changed

+73
-9
lines changed

peerprep/app/questions/page.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ import React from "react";
22
import QuestionList from "@/components/questionpage/QuestionList";
33
import Matchmaking from "@/components/questionpage/Matchmaking";
44
import { QuestionFilterProvider } from "@/contexts/QuestionFilterContext";
5+
import { UserInfoProvider } from "@/contexts/UserInfoContext";
56

67
const QuestionsPage = () => {
78
return (
8-
<QuestionFilterProvider>
9-
<Matchmaking></Matchmaking>
10-
<QuestionList></QuestionList>
11-
</QuestionFilterProvider>
9+
<UserInfoProvider>
10+
<QuestionFilterProvider>
11+
<Matchmaking></Matchmaking>
12+
<QuestionList></QuestionList>
13+
</QuestionFilterProvider>
14+
</UserInfoProvider>
1215
);
1316
};
1417

peerprep/components/questionpage/Matchmaking.tsx

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,28 @@ 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;
6+
import { useUserInfo } from "@/contexts/UserInfoContext";
7+
8+
const QUERY_INTERVAL_MILLISECONDS = 2000;
9+
10+
const getMatchRequestTime = (): string => {
11+
const now = new Date();
12+
const options: Intl.DateTimeFormatOptions = {
13+
year: "numeric",
14+
month: "2-digit",
15+
day: "2-digit",
16+
hour: "2-digit",
17+
minute: "2-digit",
18+
second: "2-digit",
19+
hour12: false,
20+
};
21+
22+
const formattedDate = now.toLocaleString("en-CA", options); // gives YYYY-MM-DD, HH:mm:ss
23+
const finalDate = formattedDate.replace(",", "").replace(/:/g, "-");
24+
25+
return finalDate;
26+
};
27+
728
const usePeriodicCallback = (
829
callback: () => void,
930
intervalTime: number,
@@ -22,13 +43,16 @@ const Matchmaking = () => {
2243
const router = useRouter();
2344
const [isMatching, setIsMatching] = useState(false);
2445
const { difficulty, topics } = useQuestionFilter();
46+
const { userid } = useUserInfo();
2547

2648
const handleMatch = () => {
2749
if (!isMatching) {
2850
setIsMatching(true);
2951
console.log("Match attempted");
3052
console.log("Selected Difficulty:", difficulty);
3153
console.log("Selected Topics:", topics);
54+
console.debug("Request time: ", getMatchRequestTime());
55+
console.debug("User id: ", userid);
3256
} else {
3357
setIsMatching(false);
3458
console.debug("User stopped matching");
@@ -46,13 +70,18 @@ const Matchmaking = () => {
4670

4771
return (
4872
// TODO: move this to some admin panel or something
49-
<div className="p-4 space-x-4">
73+
<div className="p-4 flex flex-row items-center space-x-4">
5074
<PeerprepButton onClick={() => router.push(`questions/new`)}>
5175
Add Question
5276
</PeerprepButton>
53-
<PeerprepButton onClick={handleMatch}>
54-
{isMatching ? "Cancel Match" : "Find Match"}
55-
</PeerprepButton>
77+
<div className="flex flex-row items-center space-x-4">
78+
<PeerprepButton onClick={handleMatch}>
79+
{isMatching ? "Cancel Match" : "Find Match"}
80+
</PeerprepButton>
81+
{isMatching && (
82+
<div className="w-5 h-5 bg-difficulty-hard rounded-full ml-2" />
83+
)}
84+
</div>
5685
</div>
5786
);
5887
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,34 @@
11
// maybe store SAFE user info and wrap the whole damn app in it
22
// username, userid?, userstate (matching, idle, inmenu)
3+
4+
"use client";
5+
import React, { createContext, useContext, useState, ReactNode } from "react";
6+
7+
interface UserInfoContextType {
8+
userid: string;
9+
setUserid: (userid: string) => void;
10+
}
11+
12+
const UserInfoContext = createContext<UserInfoContextType | undefined>(
13+
undefined
14+
);
15+
16+
export const UserInfoProvider: React.FC<{ children: ReactNode }> = ({
17+
children,
18+
}) => {
19+
const [userid, setUserid] = useState<string>("dummy-user");
20+
21+
return (
22+
<UserInfoContext.Provider value={{ userid, setUserid }}>
23+
{children}
24+
</UserInfoContext.Provider>
25+
);
26+
};
27+
28+
export const useUserInfo = (): UserInfoContextType => {
29+
const context = useContext(UserInfoContext);
30+
if (!context) {
31+
throw new Error("useUserInfo must be used within a UserInfoProvider");
32+
}
33+
return context;
34+
};

0 commit comments

Comments
 (0)