From 204c8a4070f0e282d25024137025e398f829964e Mon Sep 17 00:00:00 2001 From: Aaron McAdam Date: Fri, 2 Aug 2024 18:36:30 +0100 Subject: [PATCH] refactoring --- src/components/quiz.tsx | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/components/quiz.tsx b/src/components/quiz.tsx index 5e88589..acdc1ed 100644 --- a/src/components/quiz.tsx +++ b/src/components/quiz.tsx @@ -38,20 +38,11 @@ export function Quiz() { // Map the fetched data to the view model state // Set the first question as "current" and the rest as "upcoming" data.map((q, i) => { - if (i === 0) { - return { - title: q.question, - options: q.options, - response: null, - status: "current", - }; - } - return { title: q.question, options: q.options, response: null, - status: "upcoming", + status: i === 0 ? "current" : "upcoming", }; }), ), @@ -64,17 +55,18 @@ export function Quiz() { const visitorIsRejected = questions.some((q) => q.response?.isRejection); function handleOptionClick(option: Option) { - setQuestions((currentQuestions) => { + setQuestions((prevQuestions) => { // Update the current question with its response and "complete" status // and set the next question as "upcoming" - const updatedQuestions: QuizQuestion[] = currentQuestions.map((q, i) => { + const updatedQuestions: QuizQuestion[] = prevQuestions.map((q, i) => { if (i === currentIndex) { return { ...q, response: option, status: "complete", }; - } else if (i === currentIndex + 1) { + // Mark the next question as "current" only if it's not been completed + } else if (i === currentIndex + 1 && q.status !== "complete") { return { ...q, status: "current",