|
1 | 1 | "use client";
|
2 |
| -import React, { useEffect, useState } from "react"; |
| 2 | +import React, { useState } from "react"; |
3 | 3 | import QuestionCard from "./QuestionCard";
|
4 |
| -import { Question, Difficulty, isError } from "@/api/structs"; |
| 4 | +import { Difficulty, Question } from "@/api/structs"; |
5 | 5 | import PeerprepDropdown from "../shared/PeerprepDropdown";
|
6 | 6 | import PeerprepSearchBar from "../shared/PeerprepSearchBar";
|
7 | 7 | import { useQuestionFilter } from "@/contexts/QuestionFilterContext";
|
| 8 | + |
| 9 | +type Props = { |
| 10 | + questions: Question[]; |
| 11 | +}; |
| 12 | + |
8 | 13 | // TODO make multiple select for topics at least
|
9 |
| -const QuestionList: React.FC = () => { |
10 |
| - const [questions, setQuestions] = useState<Question[]>([]); |
11 |
| - const [loading, setLoading] = useState(true); |
| 14 | +const QuestionList = ({ questions }: Props) => { |
12 | 15 | const [searchFilter, setSearchFilter] = useState<string>("");
|
13 | 16 | const [difficultyFilter, setDifficultyFilter] = useState<string>(
|
14 |
| - Difficulty.All |
| 17 | + Difficulty.All, |
15 | 18 | );
|
16 | 19 | const [topicFilter, setTopicFilter] = useState<string>("all");
|
17 |
| - |
18 | 20 | const { topicList, setTopicList } = useQuestionFilter();
|
19 | 21 |
|
20 |
| - useEffect(() => { |
21 |
| - const fetchQuestions = async () => { |
22 |
| - const payload = await fetch(`/api/internal/questions`).then((res) => |
23 |
| - res.json() |
24 |
| - ); |
25 |
| - // uh |
26 |
| - if (isError(payload)) { |
27 |
| - // should also reflect the error |
28 |
| - return; |
29 |
| - } |
30 |
| - const data: Question[] = payload; |
31 |
| - |
32 |
| - setLoading(false); |
33 |
| - setQuestions(data); |
34 |
| - |
35 |
| - // get all present topics in all qns |
36 |
| - const uniqueTopics = Array.from( |
37 |
| - new Set(data.flatMap((question) => question.topicTags)) |
38 |
| - ).sort(); |
39 |
| - setTopicList(["all", ...uniqueTopics]); |
40 |
| - }; |
41 |
| - |
42 |
| - fetchQuestions(); |
43 |
| - }, []); |
| 22 | + const uniqueTopics = Array.from( |
| 23 | + new Set(questions.flatMap((question) => question.topicTags) ?? []), |
| 24 | + ); |
| 25 | + setTopicList(["all", ...uniqueTopics]); |
44 | 26 |
|
45 | 27 | const filteredQuestions = questions.filter((question) => {
|
46 | 28 | const matchesDifficulty =
|
@@ -89,15 +71,11 @@ const QuestionList: React.FC = () => {
|
89 | 71 | />
|
90 | 72 | </div>
|
91 | 73 |
|
92 |
| - {loading ? ( |
93 |
| - <p>Loading questions...</p> |
94 |
| - ) : ( |
95 |
| - <div> |
96 |
| - {sortedQuestions.map((question) => ( |
97 |
| - <QuestionCard key={question.id} question={question} /> |
98 |
| - ))} |
99 |
| - </div> |
100 |
| - )} |
| 74 | + <div> |
| 75 | + {sortedQuestions.map((question) => ( |
| 76 | + <QuestionCard key={question.id} question={question} /> |
| 77 | + ))} |
| 78 | + </div> |
101 | 79 | </div>
|
102 | 80 | );
|
103 | 81 | };
|
|
0 commit comments