1
1
"use client" ;
2
2
import React , { useEffect , useState } from "react" ;
3
3
import QuestionCard from "./QuestionCard" ;
4
- import { Question , StatusBody , Difficulty , isError } from "@/api/structs" ;
4
+ import { Question , Difficulty , isError } from "@/api/structs" ;
5
5
import PeerprepDropdown from "../shared/PeerprepDropdown" ;
6
6
import PeerprepSearchBar from "../shared/PeerprepSearchBar" ;
7
7
import { useQuestionFilter } from "@/contexts/QuestionFilterContext" ;
@@ -10,9 +10,12 @@ const QuestionList: React.FC = () => {
10
10
const [ questions , setQuestions ] = useState < Question [ ] > ( [ ] ) ;
11
11
const [ loading , setLoading ] = useState ( true ) ;
12
12
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" ) ;
14
17
15
- const { difficulty , setDifficulty , topics , setTopics } = useQuestionFilter ( ) ;
18
+ const { topicList , setTopicList } = useQuestionFilter ( ) ;
16
19
17
20
useEffect ( ( ) => {
18
21
const fetchQuestions = async ( ) => {
@@ -33,19 +36,18 @@ const QuestionList: React.FC = () => {
33
36
const uniqueTopics = Array . from (
34
37
new Set ( data . flatMap ( ( question ) => question . topicTags ) )
35
38
) . sort ( ) ;
36
- setTopicsList ( [ "all" , ...uniqueTopics ] ) ;
37
- setTopics ( [ "all" , ...uniqueTopics ] ) ;
39
+ setTopicList ( [ "all" , ...uniqueTopics ] ) ;
38
40
} ;
39
41
40
42
fetchQuestions ( ) ;
41
43
} , [ ] ) ;
42
44
43
45
const filteredQuestions = questions . filter ( ( question ) => {
44
46
const matchesDifficulty =
45
- difficulty === Difficulty . All ||
46
- Difficulty [ question . difficulty ] === difficulty ;
47
+ difficultyFilter === Difficulty . All ||
48
+ Difficulty [ question . difficulty ] === difficultyFilter ;
47
49
const matchesTopic =
48
- topics [ 0 ] === "all" || ( question . topicTags ?? [ ] ) . includes ( topics [ 0 ] ) ;
50
+ topicFilter === "all" || ( question . topicTags ?? [ ] ) . includes ( topicFilter ) ;
49
51
const matchesSearch =
50
52
searchFilter === "" ||
51
53
( question . title ?? "" ) . toLowerCase ( ) . includes ( searchFilter . toLowerCase ( ) ) ;
@@ -57,16 +59,12 @@ const QuestionList: React.FC = () => {
57
59
58
60
const handleSetDifficulty = ( e : React . ChangeEvent < HTMLSelectElement > ) => {
59
61
const diff = e . target . value ;
60
- setDifficulty ( diff ) ;
62
+ setDifficultyFilter ( diff ) ;
61
63
} ;
62
64
63
65
const handleSetTopics = ( e : React . ChangeEvent < HTMLSelectElement > ) => {
64
66
const topic = e . target . value ;
65
- if ( topic === "all" ) {
66
- setTopics ( topicsList ) ;
67
- } else {
68
- setTopics ( [ topic ] ) ;
69
- }
67
+ setTopicFilter ( topic ) ;
70
68
} ;
71
69
72
70
return (
@@ -79,16 +77,15 @@ const QuestionList: React.FC = () => {
79
77
/>
80
78
< PeerprepDropdown
81
79
label = "Difficulty"
82
- value = { difficulty }
80
+ value = { difficultyFilter }
83
81
onChange = { handleSetDifficulty }
84
82
options = { Object . keys ( Difficulty ) . filter ( ( key ) => isNaN ( Number ( key ) ) ) }
85
83
/>
86
84
< PeerprepDropdown
87
85
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 }
90
87
onChange = { handleSetTopics }
91
- options = { topicsList }
88
+ options = { topicList }
92
89
/>
93
90
</ div >
94
91
0 commit comments