1414 */
1515import { useCallback , useEffect , useState } from "react" ;
1616import {
17- CHEAT_SHEET_ENDPOINT ,
18- getAnswerEndpoint ,
1917 getCheatSheet ,
2018 getQuestionSets ,
2119 getQuestions ,
22- QUESTION_SETS_ENDPOINT ,
23- QUESTIONS_ENDPOINT ,
2420 submitAnswer ,
2521} from "../services/quizService" ;
2622
@@ -180,13 +176,7 @@ const getPerformanceFeedback = (percentage) => {
180176 } ;
181177} ;
182178
183- const getRequestFailureLabels = ( err ) => {
184- const status = err ?. response ?. status ;
185- return {
186- statusLabel : status ? ` (HTTP ${ status } )` : "" ,
187- timeoutLabel : err ?. code === "ECONNABORTED" ? " Request timed out." : "" ,
188- } ;
189- } ;
179+
190180
191181export default function Quiz ( { isLightTheme, selectedCategory = "programming" , onCategoryChange } ) {
192182 const [ questions , setQuestions ] = useState ( [ ] ) ;
@@ -251,18 +241,13 @@ export default function Quiz({ isLightTheme, selectedCategory = "programming", o
251241 setIsLoading ( true ) ;
252242 setError ( "" ) ;
253243
254- const querySuffix = questionSet ? `?question_set=${ encodeURIComponent ( questionSet ) } ` : "" ;
255-
256244 try {
257245 const data = await getQuestions ( questionSet ) ;
258246 setAllQuestions ( data ) ;
259247 setQuestions ( shuffleArray ( data ) ) ;
260248 setIsRetryRound ( false ) ;
261249 } catch ( err ) {
262- const { statusLabel, timeoutLabel } = getRequestFailureLabels ( err ) ;
263- setError (
264- `Unable to load quiz questions from ${ QUESTIONS_ENDPOINT } ${ querySuffix } ${ statusLabel } .${ timeoutLabel } Make sure the API server is running.` ,
265- ) ;
250+ setError ( `Unable to load questions. ${ err ?. message ?? "Please try again." } ` ) ;
266251 } finally {
267252 setIsLoading ( false ) ;
268253 }
@@ -285,10 +270,7 @@ export default function Quiz({ isLightTheme, selectedCategory = "programming", o
285270 setQuestions ( shuffleArray ( data ) ) ;
286271 setIsRetryRound ( false ) ;
287272 } catch ( err ) {
288- const { statusLabel, timeoutLabel } = getRequestFailureLabels ( err ) ;
289- setError (
290- `Unable to load question sets from ${ QUESTION_SETS_ENDPOINT } ${ statusLabel } .${ timeoutLabel } Make sure the API server is running.` ,
291- ) ;
273+ setError ( `Unable to load question sets. ${ err ?. message ?? "Please try again." } ` ) ;
292274 } finally {
293275 setIsLoading ( false ) ;
294276 }
@@ -377,10 +359,7 @@ export default function Quiz({ isLightTheme, selectedCategory = "programming", o
377359 setScore ( ( prev ) => prev + 1 ) ;
378360 }
379361 } catch ( err ) {
380- const { statusLabel, timeoutLabel } = getRequestFailureLabels ( err ) ;
381- setError (
382- `Your answer could not be submitted to ${ getAnswerEndpoint ( question . id ) } ${ statusLabel } .${ timeoutLabel } Please try again.` ,
383- ) ;
362+ setError ( `Unable to submit answer. ${ err ?. message ?? "Please try again." } ` ) ;
384363 } finally {
385364 setIsSubmitting ( false ) ;
386365 }
@@ -448,7 +427,6 @@ export default function Quiz({ isLightTheme, selectedCategory = "programming", o
448427 // Capture the set name before any async gaps so a concurrent set change
449428 // during the fetch doesn't cause the wrong entries to be displayed.
450429 const selectedSet = selectedQuestionSet ;
451- const querySuffix = `?question_set=${ encodeURIComponent ( selectedSet ) } ` ;
452430
453431 setIsQuestionSetPickerOpen ( false ) ;
454432 setIsCheatSheetOpen ( true ) ;
@@ -466,11 +444,8 @@ export default function Quiz({ isLightTheme, selectedCategory = "programming", o
466444 setCheatSheetQuestionSet ( response . question_set ) ;
467445 setCheatSheetEntries ( currentEntry ? [ currentEntry ] : [ ] ) ;
468446 } catch ( err ) {
469- const { statusLabel, timeoutLabel } = getRequestFailureLabels ( err ) ;
470447 setCheatSheetEntries ( [ ] ) ;
471- setCheatSheetError (
472- `Unable to load cheat sheet from ${ CHEAT_SHEET_ENDPOINT } ${ querySuffix } ${ statusLabel } .${ timeoutLabel } Please try again.` ,
473- ) ;
448+ setCheatSheetError ( `Unable to load cheat sheet. ${ err ?. message ?? "Please try again." } ` ) ;
474449 } finally {
475450 setIsCheatSheetLoading ( false ) ;
476451 }
0 commit comments