From 741eedfd59e32f7793813b172d09a706214130f4 Mon Sep 17 00:00:00 2001 From: Aaron McAdam Date: Fri, 2 Aug 2024 14:51:03 +0100 Subject: [PATCH] refactor: separate dialog from the quiz itself --- src/components/quiz-button.tsx | 84 +++-------------------------- src/components/quiz.tsx | 97 ++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 77 deletions(-) create mode 100644 src/components/quiz.tsx diff --git a/src/components/quiz-button.tsx b/src/components/quiz-button.tsx index fc53ea3..c31233e 100644 --- a/src/components/quiz-button.tsx +++ b/src/components/quiz-button.tsx @@ -12,6 +12,8 @@ import { DialogTrigger, } from "@/components/ui/dialog"; import { cn } from "@/lib/utils"; +import { DialogClose } from "@radix-ui/react-dialog"; +import { Quiz } from "./quiz"; type Option = { display: string; @@ -26,33 +28,6 @@ type Question = { }; export function QuizButton() { - const [questions, setQuestions] = React.useState([]); - - // TODO: Perf: we may want to somehow preload the display HTML because - // we're seeing some flicker when navigating between steps - React.useEffect(() => { - fetch("/api/questions") - .then((res) => res.json()) - .then((data) => setQuestions(data)); - }, []); - - const [currentIndex, setCurrentIndex] = React.useState(0); - const currentQuestion = questions[currentIndex]; - - // TODO: we may want to consolidate these state values into - // an object we can use to read the overall quiz state - // make this take account of any amount of questions - // for now, we'll hard code the amount of questions - const [chosenOptions, setChosenOptions] = React.useState< - Record - >({ - 0: null, - 1: null, - 2: null, - }); - - const visitorIsRejected = Object.values(chosenOptions).some((o) => o === -1); - return ( @@ -68,56 +43,11 @@ export function QuizButton() { quick questions. -
- {visitorIsRejected ? ( -

You're not eligible for treatment

- ) : currentIndex === questions.length ? ( -

You're eligible for treatment

- ) : currentQuestion ? ( -
-
- {questions.map((_q, i) => ( - - ))} -
-

{currentQuestion.question}

-
- {currentQuestion.options.map((o, i) => ( - - ))} -
-
- ) : null} -
- - {/* */} + + + + +
diff --git a/src/components/quiz.tsx b/src/components/quiz.tsx new file mode 100644 index 0000000..81dd339 --- /dev/null +++ b/src/components/quiz.tsx @@ -0,0 +1,97 @@ +"use client"; + +import * as React from "react"; +import { cn } from "@/lib/utils"; +import { Button } from "./ui/button"; + +type Option = { + display: string; + value: string; + isRejection: boolean; +}; + +type Question = { + question: string; + type: string; + options: Option[]; +}; + +export function Quiz() { + const [questions, setQuestions] = React.useState([]); + + // TODO: Perf: we may want to somehow preload the display HTML because + // we're seeing some flicker when navigating between steps + React.useEffect(() => { + fetch("/api/questions") + .then((res) => res.json()) + .then((data) => setQuestions(data)); + }, []); + + const [currentIndex, setCurrentIndex] = React.useState(0); + const currentQuestion = questions[currentIndex]; + + // TODO: we may want to consolidate these state values into + // an object we can use to read the overall quiz state + // make this take account of any amount of questions + // for now, we'll hard code the amount of questions + const [chosenOptions, setChosenOptions] = React.useState< + Record + >({ + 0: null, + 1: null, + 2: null, + }); + + const visitorIsRejected = Object.values(chosenOptions).some((o) => o === -1); + + return ( +
+ {visitorIsRejected ? ( +

You're not eligible for treatment

+ ) : currentIndex === questions.length ? ( +

You're eligible for treatment

+ ) : currentQuestion ? ( +
+
+ {questions.map((_q, i) => ( + + ))} +
+

{currentQuestion.question}

+
+ {currentQuestion.options.map((o, i) => ( + + ))} +
+
+ ) : null} +
+ ); +}