Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronmcadam committed Aug 2, 2024
1 parent 53f0273 commit 204c8a4
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions src/components/quiz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};
}),
),
Expand All @@ -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",
Expand Down

0 comments on commit 204c8a4

Please sign in to comment.