|
| 1 | +import axios from "axios"; |
| 2 | +import Link from "next/link"; |
| 3 | +import { FormEvent, useState } from "react"; |
| 4 | +import { |
| 5 | + CheckAnswerPayload, |
| 6 | + CheckAnswerResponse, |
| 7 | +} from "../pages/api/check-answer"; |
| 8 | +import PrimaryButton from "./primary-button"; |
| 9 | +import invariant from "tiny-invariant"; |
| 10 | + |
| 11 | +type Props = { |
| 12 | + questionIndex: number; |
| 13 | + questionText: string; |
| 14 | + image?: string; |
| 15 | + answers: string[]; |
| 16 | + nextQuestionFunction: () => void; |
| 17 | +}; |
| 18 | + |
| 19 | +type AnswerResult = "correct" | "incorrect"; |
| 20 | + |
| 21 | +export default function QuizQuestion({ |
| 22 | + questionIndex, |
| 23 | + questionText, |
| 24 | + image, |
| 25 | + answers, |
| 26 | + nextQuestionFunction, |
| 27 | +}: Props) { |
| 28 | + const [answerIndex, setAnswerIndex] = useState<number | undefined>(undefined); |
| 29 | + const [submitting, setSubmitting] = useState(false); |
| 30 | + const [error, setError] = useState<string | undefined>(undefined); |
| 31 | + const [answerResult, setAnswerResult] = useState<AnswerResult | undefined>( |
| 32 | + undefined |
| 33 | + ); |
| 34 | + const [correctAnswerWas, setCorrectAnswerWas] = useState<number | undefined>( |
| 35 | + undefined |
| 36 | + ); |
| 37 | + |
| 38 | + const handleSubmit = async (e: FormEvent) => { |
| 39 | + e.preventDefault(); |
| 40 | + setSubmitting(true); |
| 41 | + |
| 42 | + try { |
| 43 | + invariant( |
| 44 | + answerIndex !== undefined, |
| 45 | + "Answer index is required to submit" |
| 46 | + ); |
| 47 | + |
| 48 | + const payload: CheckAnswerPayload = { |
| 49 | + questionIndex, |
| 50 | + answerIndex, |
| 51 | + }; |
| 52 | + |
| 53 | + const checkResponse = await axios.post("/api/check-answer", payload); |
| 54 | + const result = checkResponse.data as CheckAnswerResponse; |
| 55 | + |
| 56 | + if (result.kind === "error") { |
| 57 | + setError(result.error); |
| 58 | + } |
| 59 | + |
| 60 | + if (result.kind === "correct") { |
| 61 | + setAnswerResult("correct"); |
| 62 | + setCorrectAnswerWas(answerIndex); |
| 63 | + } |
| 64 | + |
| 65 | + if (result.kind === "incorrect") { |
| 66 | + setAnswerResult("incorrect"); |
| 67 | + setCorrectAnswerWas(result.correctAnswerIndex); |
| 68 | + } |
| 69 | + } finally { |
| 70 | + setSubmitting(false); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + const renderResult = () => { |
| 75 | + if (submitting) { |
| 76 | + return <PrimaryButton disabled={true}>Checking Answer...</PrimaryButton>; |
| 77 | + } |
| 78 | + |
| 79 | + if (answerResult === "correct") { |
| 80 | + return ( |
| 81 | + <> |
| 82 | + <p className="text-green-800"> |
| 83 | + Congratulations! That was the right answer! |
| 84 | + </p> |
| 85 | + <p> |
| 86 | + A pack will be sent to you shortly. You'll be able to check it out |
| 87 | + and open it in the{" "} |
| 88 | + <Link href="/lounge"> |
| 89 | + <a className="underline hover:no-underline">lounge</a> |
| 90 | + </Link> |
| 91 | + ! |
| 92 | + </p> |
| 93 | + </> |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + if (answerResult === "incorrect") { |
| 98 | + return <p className="text-red-800">Sorry, that was incorrect!</p>; |
| 99 | + } |
| 100 | + |
| 101 | + return ( |
| 102 | + <> |
| 103 | + <PrimaryButton |
| 104 | + type="submit" |
| 105 | + onClick={handleSubmit} |
| 106 | + disabled={answerIndex === undefined} |
| 107 | + > |
| 108 | + Check Answer |
| 109 | + </PrimaryButton> |
| 110 | + {error ? <p className="text-red-500">{error}</p> : null} |
| 111 | + </> |
| 112 | + ); |
| 113 | + }; |
| 114 | + |
| 115 | + return ( |
| 116 | + <form> |
| 117 | + <div className="flex flex-col gap-4"> |
| 118 | + <div> |
| 119 | + <div className="flex flex-col gap-2"> |
| 120 | + <label className="font-medium text-lg text-gray-900"> |
| 121 | + {questionText} |
| 122 | + </label> |
| 123 | + {image ? ( |
| 124 | + <img src={image} className="object-cover h-48 w-96" alt="" /> |
| 125 | + ) : null} |
| 126 | + </div> |
| 127 | + <fieldset className="mt-4"> |
| 128 | + <div className="space-y-4"> |
| 129 | + {answers.map((answerText, i) => ( |
| 130 | + <div key={i} className="flex items-center"> |
| 131 | + <input |
| 132 | + id={i.toString()} |
| 133 | + name="quiz-answer" |
| 134 | + type="radio" |
| 135 | + className="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 peer disabled:cursor-not-allowed" |
| 136 | + value={i} |
| 137 | + checked={answerIndex === i} |
| 138 | + onChange={(e) => setAnswerIndex(Number(e.target.value))} |
| 139 | + disabled={answerResult !== undefined} |
| 140 | + /> |
| 141 | + <label |
| 142 | + htmlFor={i.toString()} |
| 143 | + className="ml-3 block text-sm font-medium text-gray-700 peer-disabled:text-gray-500 peer-disabled:cursor-not-allowed" |
| 144 | + > |
| 145 | + {answerText} |
| 146 | + {i === correctAnswerWas ? <span> ✅</span> : null} |
| 147 | + </label> |
| 148 | + </div> |
| 149 | + ))} |
| 150 | + </div> |
| 151 | + </fieldset> |
| 152 | + </div> |
| 153 | + |
| 154 | + {renderResult()} |
| 155 | + |
| 156 | + {answerResult !== undefined ? ( |
| 157 | + <PrimaryButton onClick={nextQuestionFunction}> |
| 158 | + Next Question |
| 159 | + </PrimaryButton> |
| 160 | + ) : null} |
| 161 | + </div> |
| 162 | + </form> |
| 163 | + ); |
| 164 | +} |
0 commit comments