@@ -25,14 +25,15 @@ import Card from '../components/Card.jsx';
2525export default function Trivia ( ) {
2626 const [ questions , setQuestions ] = useState ( [ ] ) ;
2727 const [ category , setCategory ] = useState ( '18' ) ; // Science: Computers
28+ const [ difficulty , setDifficulty ] = useState ( 'easy' ) ; // Default to easy
2829 const [ loading , setLoading ] = useState ( false ) ;
2930 const [ error , setError ] = useState ( null ) ;
3031 const [ score , setScore ] = useState ( 0 ) ;
3132 const [ showReview , setShowReview ] = useState ( false ) ;
3233
3334 useEffect ( ( ) => {
3435 fetchQuestions ( ) ;
35- } , [ category ] ) ;
36+ } , [ category , difficulty ] ) ;
3637
3738 async function fetchQuestions ( ) {
3839 try {
@@ -42,7 +43,7 @@ export default function Trivia() {
4243 setShowReview ( false ) ;
4344
4445 const res = await fetch (
45- `https://opentdb.com/api.php?amount=5&category=${ category } &type=multiple`
46+ `https://opentdb.com/api.php?amount=5&category=${ category } &difficulty= ${ difficulty } & type=multiple`
4647 ) ;
4748 if ( ! res . ok ) throw new Error ( 'Failed to fetch' ) ;
4849 const json = await res . json ( ) ;
@@ -88,21 +89,47 @@ export default function Trivia() {
8889
8990 return (
9091 < div style = { { padding : '20px' } } >
91- < h2 > Trivia Quiz</ h2 >
92+ < div style = { { display : 'flex' , alignItems : 'center' , gap : '10px' , marginBottom : '20px' } } >
93+ < h2 style = { { margin : 0 } } > Trivia Quiz</ h2 >
94+ < span style = { {
95+ backgroundColor : difficulty === 'easy' ? '#4caf50' : difficulty === 'medium' ? '#ff9800' : '#f44336' ,
96+ color : 'white' ,
97+ padding : '4px 8px' ,
98+ borderRadius : '4px' ,
99+ fontSize : '0.9em'
100+ } } >
101+ { difficulty . charAt ( 0 ) . toUpperCase ( ) + difficulty . slice ( 1 ) }
102+ </ span >
103+ </ div >
92104
93105 { /* Category Selector */ }
94- < label >
95- Category:{ ' ' }
96- < select
97- value = { category }
98- onChange = { ( e ) => setCategory ( e . target . value ) }
99- disabled = { showReview }
100- >
101- < option value = "18" > Science: Computers</ option >
102- < option value = "21" > Sports</ option >
103- < option value = "23" > History</ option >
104- </ select >
105- </ label >
106+ < div style = { { display : 'flex' , gap : '20px' , marginBottom : '20px' } } >
107+ < label >
108+ Category:{ ' ' }
109+ < select
110+ value = { category }
111+ onChange = { ( e ) => setCategory ( e . target . value ) }
112+ disabled = { showReview }
113+ >
114+ < option value = "18" > Science: Computers</ option >
115+ < option value = "21" > Sports</ option >
116+ < option value = "23" > History</ option >
117+ </ select >
118+ </ label >
119+ { /* Difficulty Selector */ }
120+ < label >
121+ Difficulty:{ ' ' }
122+ < select
123+ value = { difficulty }
124+ onChange = { ( e ) => setDifficulty ( e . target . value ) }
125+ disabled = { showReview }
126+ >
127+ < option value = "easy" > Easy</ option >
128+ < option value = "medium" > Medium</ option >
129+ < option value = "hard" > Hard</ option >
130+ </ select >
131+ </ label >
132+ </ div >
106133
107134 { /* Loading / Error */ }
108135 { loading && < Loading /> }
0 commit comments