Skip to content

Commit 6770d98

Browse files
committed
Revert filter hooks, add matching button dropdown
1 parent 258db87 commit 6770d98

File tree

3 files changed

+44
-30
lines changed

3 files changed

+44
-30
lines changed

peerprep/components/questionpage/Matchmaking.tsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
findMatch,
1616
} from "@/app/api/internal/matching/helper";
1717
import ResettingStopwatch from "../shared/ResettingStopwatch";
18+
import PeerprepDropdown from "../shared/PeerprepDropdown";
1819

1920
const QUERY_INTERVAL_MILLISECONDS = 5000;
2021
const TIMEOUT_MILLISECONDS = 30000;
@@ -54,7 +55,9 @@ const usePeriodicCallback = (
5455
const Matchmaking = () => {
5556
const router = useRouter();
5657
const [isMatching, setIsMatching] = useState<boolean>(false);
57-
const { difficulty, topics } = useQuestionFilter();
58+
const [difficultyFilter, setDifficultyFilter] = useState<string>(Difficulty.Easy);
59+
const [topicFilter, setTopicFilter] = useState<string[]>(["all"]);
60+
const { difficulties, topicList } = useQuestionFilter();
5861
const { userid } = useUserInfo();
5962
const timeout = useRef<NodeJS.Timeout>();
6063

@@ -69,9 +72,8 @@ const Matchmaking = () => {
6972
const getMatchMakingRequest = (): MatchRequest => {
7073
const matchRequest: MatchRequest = {
7174
userId: userid,
72-
// welp, just bandaid default easy
73-
difficulty: difficulty === Difficulty.All ? Difficulty.Easy : difficulty,
74-
topicTags: topics,
75+
difficulty: difficultyFilter,
76+
topicTags: topicFilter,
7577
requestTime: getMatchRequestTime(),
7678
};
7779

@@ -139,6 +141,19 @@ const Matchmaking = () => {
139141
<PeerprepButton onClick={handleMatch}>
140142
{isMatching ? "Cancel Match" : "Find Match"}
141143
</PeerprepButton>
144+
{!isMatching &&
145+
<PeerprepDropdown label="Difficulty"
146+
value={difficultyFilter}
147+
onChange={e => setDifficultyFilter(e.target.value)}
148+
// truthfully we don't need this difficulties list, but we are temporarily including it
149+
options={difficulties} />
150+
}
151+
{!isMatching &&
152+
<PeerprepDropdown label="Topics"
153+
value={topicFilter[0]}
154+
onChange={e => setTopicFilter(e.target.value === "all" ? topicList : [e.target.value])}
155+
options={topicList} />
156+
}
142157
{isMatching && <ResettingStopwatch isActive={isMatching} />}
143158
</div>
144159
</div>

peerprep/components/questionpage/QuestionList.tsx

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22
import React, { useEffect, useState } from "react";
33
import QuestionCard from "./QuestionCard";
4-
import { Question, StatusBody, Difficulty, isError } from "@/api/structs";
4+
import { Question, Difficulty, isError } from "@/api/structs";
55
import PeerprepDropdown from "../shared/PeerprepDropdown";
66
import PeerprepSearchBar from "../shared/PeerprepSearchBar";
77
import { useQuestionFilter } from "@/contexts/QuestionFilterContext";
@@ -10,9 +10,12 @@ const QuestionList: React.FC = () => {
1010
const [questions, setQuestions] = useState<Question[]>([]);
1111
const [loading, setLoading] = useState(true);
1212
const [searchFilter, setSearchFilter] = useState<string>("");
13-
const [topicsList, setTopicsList] = useState<string[]>(["all"]);
13+
const [difficultyFilter, setDifficultyFilter] = useState<string>(
14+
Difficulty.All
15+
);
16+
const [topicFilter, setTopicFilter] = useState<string>("all");
1417

15-
const { difficulty, setDifficulty, topics, setTopics } = useQuestionFilter();
18+
const { topicList, setTopicList } = useQuestionFilter();
1619

1720
useEffect(() => {
1821
const fetchQuestions = async () => {
@@ -33,19 +36,18 @@ const QuestionList: React.FC = () => {
3336
const uniqueTopics = Array.from(
3437
new Set(data.flatMap((question) => question.topicTags))
3538
).sort();
36-
setTopicsList(["all", ...uniqueTopics]);
37-
setTopics(["all", ...uniqueTopics]);
39+
setTopicList(["all", ...uniqueTopics]);
3840
};
3941

4042
fetchQuestions();
4143
}, []);
4244

4345
const filteredQuestions = questions.filter((question) => {
4446
const matchesDifficulty =
45-
difficulty === Difficulty.All ||
46-
Difficulty[question.difficulty] === difficulty;
47+
difficultyFilter === Difficulty.All ||
48+
Difficulty[question.difficulty] === difficultyFilter;
4749
const matchesTopic =
48-
topics[0] === "all" || (question.topicTags ?? []).includes(topics[0]);
50+
topicFilter === "all" || (question.topicTags ?? []).includes(topicFilter);
4951
const matchesSearch =
5052
searchFilter === "" ||
5153
(question.title ?? "").toLowerCase().includes(searchFilter.toLowerCase());
@@ -57,16 +59,12 @@ const QuestionList: React.FC = () => {
5759

5860
const handleSetDifficulty = (e: React.ChangeEvent<HTMLSelectElement>) => {
5961
const diff = e.target.value;
60-
setDifficulty(diff);
62+
setDifficultyFilter(diff);
6163
};
6264

6365
const handleSetTopics = (e: React.ChangeEvent<HTMLSelectElement>) => {
6466
const topic = e.target.value;
65-
if (topic === "all") {
66-
setTopics(topicsList);
67-
} else {
68-
setTopics([topic]);
69-
}
67+
setTopicFilter(topic);
7068
};
7169

7270
return (
@@ -79,16 +77,15 @@ const QuestionList: React.FC = () => {
7977
/>
8078
<PeerprepDropdown
8179
label="Difficulty"
82-
value={difficulty}
80+
value={difficultyFilter}
8381
onChange={handleSetDifficulty}
8482
options={Object.keys(Difficulty).filter((key) => isNaN(Number(key)))}
8583
/>
8684
<PeerprepDropdown
8785
label="Topics"
88-
// coincidentally "all" is at the top of the list so the display works out...dumb luck!
89-
value={topics[0]}
86+
value={topicFilter}
9087
onChange={handleSetTopics}
91-
options={topicsList}
88+
options={topicList}
9289
/>
9390
</div>
9491

peerprep/contexts/QuestionFilterContext.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import { Difficulty } from "@/api/structs";
33
import React, { createContext, useContext, useState, ReactNode } from "react";
44

55
interface QuestionFilterContextType {
6-
difficulty: string;
7-
setDifficulty: (difficulty: string) => void;
8-
topics: string[];
9-
setTopics: (topics: string[]) => void;
6+
difficulties: string[];
7+
topicList: string[];
8+
setTopicList: (topics: string[]) => void;
109
}
1110

1211
const QuestionFilterContext = createContext<
@@ -16,13 +15,16 @@ const QuestionFilterContext = createContext<
1615
export const QuestionFilterProvider: React.FC<{ children: ReactNode }> = ({
1716
children,
1817
}) => {
19-
const [difficulty, setDifficulty] = useState<string>(Difficulty.All); // default to all
20-
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?
18+
const difficulties = [Difficulty.Easy, Difficulty.Medium, Difficulty.Hard];
19+
const [topicList, setTopicList] = useState<string[]>([]);
20+
// I guess default set this too the whole list of topics from questionlist
21+
// can also consider moving all the uniqu topics here? -- yes, we are doing that now
22+
// TODO: since QuestionFilterProvider now exists to wrap the QuestionList,
23+
// we can move the question fetching 1 layer higher, theoretically, so look into this
2224

2325
return (
2426
<QuestionFilterContext.Provider
25-
value={{ difficulty, setDifficulty, topics, setTopics }}
27+
value={{ difficulties, topicList, setTopicList }}
2628
>
2729
{children}
2830
</QuestionFilterContext.Provider>

0 commit comments

Comments
 (0)