From e55d1ce583fc67341565a1902feb386df373838a Mon Sep 17 00:00:00 2001 From: andrewtan2000 Date: Tue, 24 Sep 2024 00:57:01 +0800 Subject: [PATCH] Update Test Page --- app/(main)/test/page.tsx | 58 ++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/app/(main)/test/page.tsx b/app/(main)/test/page.tsx index f6720e5..abccabc 100644 --- a/app/(main)/test/page.tsx +++ b/app/(main)/test/page.tsx @@ -11,13 +11,13 @@ import { Dialog } from 'primereact/dialog'; import { TreeSelect, TreeSelectSelectionKeysType } from 'primereact/treeselect'; import { ProgressBar } from 'primereact/progressbar'; -const QuizPage: React.FC = () => { +const TestPage: React.FC = () => { const [selectedTopicNodes, setSelectedTopicNodes] = useState(); const [topicNodes, setTopicNodes] = useState(null); const [selectedTopics, setSelectedTopics] = useState([]); const [selectedSkills, setSelectedSkills] = useState([]); - const [quiz, setQuiz] = useState(null); - const [isQuizOngoing, setIsQuizOngoing] = useState(false); + const [test, setTest] = useState(null); + const [isTestOngoing, setIsTestOngoing] = useState(false); const [selectedOptions, setSelectedOptions] = useState<{ [key: number]: number | 0 }>({}); const [currentTestQuestionIndex, setCurrentTestQuestionIndex] = useState(0); const [isDisabled, setIsDisabled] = useState(false); @@ -26,7 +26,7 @@ const QuizPage: React.FC = () => { const [showTestScore, setShowTestScore] = useState(false); const [showTestScoreMessage, setShowTestScoreMessage] = useState(''); const [isRadioDisabled, setIsRadioDisabled] = useState(false); - const [quizIdAvailable, setQuizIdAvailable] = useState(false); + const [testIdAvailable, setTestIdAvailable] = useState(false); // Retrieve currentTestQuestionIndex from local storage when the component mounts useEffect(() => { @@ -72,16 +72,16 @@ const QuizPage: React.FC = () => { try { const responseData = await TestService.getTestInProgress(); if (responseData.message === 'Test not found') { - setIsQuizOngoing(false); + setIsTestOngoing(false); return; } - setQuiz(responseData); + setTest(responseData); const initialSelectedOptions: { [key: number]: number | 0 } = {}; responseData.mcqs.forEach((mcq) => { initialSelectedOptions[mcq.id] = 0; }); setSelectedOptions(initialSelectedOptions); - setQuizIdAvailable(true); // Set quizIdAvailable to true once quiz data is fetched + setTestIdAvailable(true); // Set quizIdAvailable to true once quiz data is fetched setExplanationsVisible({}); // Initialize explanationsVisible } catch (error) { console.error('Error fetching data:', error); @@ -91,13 +91,13 @@ const QuizPage: React.FC = () => { fetchData(); }, []); - const currentTestQuestion = quiz?.mcqs?.[currentTestQuestionIndex]; - const currentQuestionLength = quiz?.mcqs?.length ?? 0; + const currentTestQuestion = test?.mcqs?.[currentTestQuestionIndex]; + const currentQuestionLength = test?.mcqs?.length ?? 0; - const submitAttempt = async (quizId: number, mcqId: number, attempt: number | null | undefined) => { + const submitAttempt = async (testId: number, mcqId: number, attempt: number | null | undefined) => { const attemptValue = attempt ?? 0; try { - await TestService.submitAttempt(quizId, mcqId, attemptValue); + await TestService.submitAttempt(testId, mcqId, attemptValue); } catch (error) { console.error(`Error submitting attempt for MCQ`, error); } @@ -245,16 +245,16 @@ const QuizPage: React.FC = () => { }, [showTestScoreMessage]); useEffect(() => { - if (!isQuizOngoing) { + if (!isTestOngoing) { setShowTestScore(false); localStorage.setItem('showTestScore', 'false'); localStorage.setItem('currentTestQuestionIndex', '0'); } - }, [isQuizOngoing]); + }, [isTestOngoing]); - const [isStartingNewQuiz, setIsStartingNewQuiz] = useState(false); + const [isStartingNewTest, setIsStartingNewTest] = useState(false); - const startNewQuiz = async () => { + const startNewTest = async () => { let selectedTopics: number[] = []; let selectedSkills: number[] = []; if (selectedTopicNodes) { @@ -267,13 +267,13 @@ const QuizPage: React.FC = () => { } }); } - setIsStartingNewQuiz(true); // Set isStartingNewQuiz to true to disable the button + setIsStartingNewTest(true); // Set isStartingNewQuiz to true to disable the button try { await TestService.startNewTest(selectedTopics, selectedSkills); } catch (error) { console.error('Error starting new test:', error); } finally { - setIsStartingNewQuiz(false); // Set isStartingNewQuiz to false once the call is complete + setIsStartingNewTest(false); // Set isStartingNewQuiz to false once the call is complete window.location.reload(); } }; @@ -292,8 +292,8 @@ const QuizPage: React.FC = () => {
Class Tests
- {quiz && Array.isArray(quiz.mcqs) && quiz.mcqs.length === 0 &&
No questions generated.
} - {!isQuizOngoing && ( + {test && Array.isArray(test.mcqs) && test.mcqs.length === 0 &&
No questions generated.
} + {!isTestOngoing && (
{/* {selectedQuestionCount ? selectedQuestionCount : generatedQuestionCount} question(s) will be generated. */}
@@ -302,13 +302,13 @@ const QuizPage: React.FC = () => {
)} - {currentTestQuestion && ( + {isTestOngoing && currentTestQuestion && (
- Question {currentTestQuestionIndex + 1} of {quiz?.mcqs?.length || 0} + Question {currentTestQuestionIndex + 1} of {test?.mcqs?.length || 0}
@@ -349,25 +349,25 @@ const QuizPage: React.FC = () => {
))} - {currentTestQuestionIndex < quiz.mcqs.length - 1 ? ( + {currentTestQuestionIndex < test.mcqs.length - 1 ? ( ) : (
- +
)} {showTestScore && showTestScoreMessage &&
{showTestScoreMessage}
} @@ -391,4 +391,4 @@ const QuizPage: React.FC = () => { ); }; -export default QuizPage; +export default TestPage;