|
| 1 | +import { useState, useEffect, useCallback } from 'react'; |
| 2 | +import { saveQuizProgress, loadQuizProgress, clearQuizProgress } from '../services/storageManager'; |
| 3 | +import { shuffleArray } from '../utils/helpers'; |
| 4 | + |
| 5 | +export const useQuizProgress = (activeQuizSetName, originalQuestions, isShuffleEnabled, isSubmitted) => { |
| 6 | + const [answers, setAnswers] = useState({}); |
| 7 | + const [initialLoadedQuestionOrder, setInitialLoadedQuestionOrder] = useState(null); |
| 8 | + const [processedQuestions, setProcessedQuestions] = useState([]); |
| 9 | + |
| 10 | + // Effect to load progress when the active quiz set name changes |
| 11 | + useEffect(() => { |
| 12 | + if (activeQuizSetName) { |
| 13 | + console.log(`useQuizProgress: Attempting to load progress for ${activeQuizSetName}`); |
| 14 | + const savedProgress = loadQuizProgress(activeQuizSetName); |
| 15 | + if (savedProgress && savedProgress.questionOrder) { |
| 16 | + setAnswers(savedProgress.answers || {}); |
| 17 | + setInitialLoadedQuestionOrder(savedProgress.questionOrder); |
| 18 | + // console.log(`useQuizProgress: Loaded progress for ${activeQuizSetName}. Answers:`, savedProgress.answers, "Order:", savedProgress.questionOrder); |
| 19 | + } else { |
| 20 | + setAnswers({}); // Reset if no progress found or order is missing |
| 21 | + setInitialLoadedQuestionOrder(null); |
| 22 | + // console.log(`useQuizProgress: No valid progress found for ${activeQuizSetName}, resetting answers and order.`); |
| 23 | + } |
| 24 | + } else { |
| 25 | + setAnswers({}); // Reset if no active quiz set name |
| 26 | + setInitialLoadedQuestionOrder(null); |
| 27 | + // console.log("useQuizProgress: No activeQuizSetName, resetting answers and order."); |
| 28 | + } |
| 29 | + }, [activeQuizSetName]); |
| 30 | + |
| 31 | + // Effect to determine and set the order of questions |
| 32 | + useEffect(() => { |
| 33 | + if (!originalQuestions || originalQuestions.length === 0) { |
| 34 | + setProcessedQuestions([]); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + if (initialLoadedQuestionOrder && initialLoadedQuestionOrder.length > 0) { |
| 39 | + // console.log("useQuizProgress: Using initial loaded question order", initialLoadedQuestionOrder); |
| 40 | + setProcessedQuestions(initialLoadedQuestionOrder); |
| 41 | + } else if (isShuffleEnabled) { |
| 42 | + // console.log("useQuizProgress: Shuffling original questions", originalQuestions); |
| 43 | + setProcessedQuestions(shuffleArray(originalQuestions)); |
| 44 | + } else { |
| 45 | + // console.log("useQuizProgress: Using original question order", originalQuestions); |
| 46 | + setProcessedQuestions(originalQuestions); |
| 47 | + } |
| 48 | + }, [originalQuestions, initialLoadedQuestionOrder, isShuffleEnabled]); |
| 49 | + |
| 50 | + |
| 51 | + // Effect to save progress when answers or processedQuestions change, |
| 52 | + // if the quiz is active and not submitted. |
| 53 | + useEffect(() => { |
| 54 | + // Save progress if we have an active set, it's not submitted, and there are processed questions. |
| 55 | + if (activeQuizSetName && !isSubmitted && processedQuestions && processedQuestions.length > 0) { |
| 56 | + // console.log(`useQuizProgress: Saving progress for ${activeQuizSetName}. Answers:`, answers, "Order:", processedQuestions); |
| 57 | + saveQuizProgress(activeQuizSetName, { answers, questionOrder: processedQuestions }); |
| 58 | + } else if (activeQuizSetName && !isSubmitted && (!processedQuestions || processedQuestions.length === 0)) { |
| 59 | + // This case might occur if a quiz is loaded that has no questions. |
| 60 | + // We probably don't want to save progress for an empty quiz or if the order is somehow lost. |
| 61 | + // console.log(`useQuizProgress: Not saving progress for ${activeQuizSetName} due to empty/invalid processed questions.`); |
| 62 | + } |
| 63 | + }, [answers, processedQuestions, activeQuizSetName, isSubmitted]); |
| 64 | + |
| 65 | + const clearCurrentSavedProgress = useCallback(() => { |
| 66 | + if (activeQuizSetName) { |
| 67 | + // console.log(`useQuizProgress: Clearing saved progress for ${activeQuizSetName}`); |
| 68 | + clearQuizProgress(activeQuizSetName); |
| 69 | + setAnswers({}); |
| 70 | + setInitialLoadedQuestionOrder(null); // This will trigger re-evaluation of processedQuestions |
| 71 | + } |
| 72 | + }, [activeQuizSetName]); |
| 73 | + |
| 74 | + // Function to explicitly discard loaded order, e.g., when user toggles shuffle ON |
| 75 | + const discardLoadedOrderAndShuffle = useCallback(() => { |
| 76 | + setInitialLoadedQuestionOrder(null); |
| 77 | + // The useEffect for processedQuestions will then re-shuffle if isShuffleEnabled is true |
| 78 | + }, []); |
| 79 | + |
| 80 | + return { answers, setAnswers, processedQuestions, clearCurrentSavedProgress, discardLoadedOrderAndShuffle }; |
| 81 | +}; |
0 commit comments