Skip to content

Commit 6e7a213

Browse files
feat: add question answering
1 parent 93edff3 commit 6e7a213

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

src/components/tutorial-quiz.tsx

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@ const cinzel = Cinzel({ subsets: ["latin"], weight: ["700"] });
55
export default function TutorialQuiz() {
66
const [questionNumber, setQuestionNumber] = useState<number>(0);
77
const currentQuestion = helloWorldQuiz.questions[questionNumber];
8+
const [selectedOption, setSelectedOption] = useState<string | null>(null);
9+
const [isCorrect, setIsCorrect] = useState<boolean | null>(null);
10+
11+
const handleOptionClick = (option: string) => {
12+
// Prevent clicking if already selected
13+
if (selectedOption) return;
14+
15+
setSelectedOption(option);
16+
17+
// Check answer
18+
let correct = null;
19+
if (option === currentQuestion.correctAnswer) {
20+
correct = true;
21+
} else {
22+
correct = false;
23+
}
24+
setIsCorrect(correct);
25+
};
826

927
return (
1028
<div className="mt-12 p-8 border-2 border-amber-800/50 rounded-xl bg-amber-50/50 shadow-inner shadow-amber-900/20">
@@ -22,11 +40,34 @@ export default function TutorialQuiz() {
2240

2341
{/* Choices */}
2442
<div className="grid gap-3">
25-
{currentQuestion.options.map((option) => (
26-
<button key={option} className="w-full text-left bg-white p-4 ">
27-
{option}
28-
</button>
29-
))}
43+
{currentQuestion.options.map((option) => {
44+
// Dynamic Styling Logic ( This is soo cool)
45+
let buttonStyle = "bg-white hover:bg-amber-100 border-amber-300 text-amber-900";
46+
47+
if (selectedOption === option) {
48+
if (isCorrect) {
49+
buttonStyle = "bg-green-200 border-green-500 text-green-900 font-bold";
50+
} else {
51+
buttonStyle = "bg-red-200 border-red-500 text-red-900 font-bold";
52+
}
53+
} else if (selectedOption && option === currentQuestion.correctAnswer) {
54+
// Highlight the correct answer if they got it wrong
55+
buttonStyle = "bg-green-100 border-green-300 text-green-800 opacity-75";
56+
}
57+
58+
return (
59+
<button
60+
key={option}
61+
onClick={() => handleOptionClick(option)}
62+
disabled={selectedOption !== null} // Disables all buttons after selection
63+
className={`w-full text-left p-4 rounded border-2 transition-all hover:cursor-pointer ${buttonStyle} ${
64+
selectedOption === null ? "hover:translate-x-1" : ""
65+
}`}
66+
>
67+
{option}
68+
</button>
69+
);
70+
})}
3071
</div>
3172
</div>
3273
</div>

0 commit comments

Comments
 (0)