Skip to content

Commit baec054

Browse files
fix: pass quiz data as props not hardcoded
1 parent ebb9e7b commit baec054

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/app/tutorial-hello-world/page.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import BackToDashBoardLink from "@/components/back-to-dashboard-link";
44
import YouTube from "react-youtube";
5-
import TutorialQuiz from "@/components/tutorial-quiz";
5+
import Quiz from "@/components/tutorial-quiz";
6+
67
import { Cinzel } from "next/font/google";
8+
import { helloWorldQuiz } from "@/data/quizzes/01-hello-world";
79

810
const cinzel = Cinzel({
911
subsets: ["latin"],
@@ -144,7 +146,7 @@ export default function TutorialHelloWorld() {
144146

145147
<YouTube videoId="hp4pYFASTrc"></YouTube>
146148

147-
<TutorialQuiz></TutorialQuiz>
149+
<Quiz quizData={helloWorldQuiz}></Quiz>
148150
</article>
149151
</div>
150152
);

src/components/tutorial-quiz.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import { Cinzel } from "next/font/google";
22
import { useState } from "react";
3-
import { helloWorldQuiz } from "@/data/quizzes/01-hello-world";
3+
import { QuizData } from "@/lib/types/types";
44

55
const cinzel = Cinzel({ subsets: ["latin"], weight: ["700"] });
66

7-
export default function TutorialQuiz() {
7+
type QuizProps = {
8+
quizData: QuizData;
9+
};
10+
11+
export default function Quiz({ quizData }: QuizProps) {
812
const [questionNumber, setQuestionNumber] = useState<number>(0);
913
const [selectedOption, setSelectedOption] = useState<string | null>(null);
1014
const [isCorrect, setIsCorrect] = useState<boolean | null>(null);
1115
const [score, setScore] = useState(0);
1216
const [showResults, setShowResults] = useState(false);
1317

14-
const currentQuestion = helloWorldQuiz.questions[questionNumber];
18+
const currentQuestion = quizData.questions[questionNumber];
1519

1620
const handleOptionClick = (option: string) => {
1721
if (selectedOption) return;
1822
setSelectedOption(option);
1923

24+
// 1. Check Correctness
2025
const correct = option === currentQuestion.correctAnswer;
2126
setIsCorrect(correct);
2227

@@ -29,7 +34,7 @@ export default function TutorialQuiz() {
2934
setTimeout(() => {
3035
const nextIndex = questionNumber + 1;
3136

32-
if (nextIndex < helloWorldQuiz.questions.length) {
37+
if (nextIndex < quizData.questions.length) {
3338
setQuestionNumber(nextIndex);
3439
setSelectedOption(null);
3540
setIsCorrect(null);
@@ -50,16 +55,16 @@ export default function TutorialQuiz() {
5055
return (
5156
<div className="mt-12 p-8 border-2 border-amber-800/50 rounded-xl bg-amber-50/50 shadow-inner shadow-amber-900/20">
5257
<h2 className={`text-3xl font-bold text-center mb-6 text-amber-900 ${cinzel.className}`}>
53-
{helloWorldQuiz.title}
58+
{quizData.title}
5459
</h2>
5560

56-
{/* 5. Conditional Rendering: Results vs Question */}
5761
{showResults ? (
5862
<div className="text-center space-y-6 animate-fade-in">
63+
{/* Results */}
5964
<p className="text-2xl text-amber-950">Quest Complete!</p>
6065
<p className="text-xl">
6166
You scored <span className="font-bold">{score}</span> out of{" "}
62-
<span className="font-bold">{helloWorldQuiz.questions.length}</span>
67+
<span className="font-bold">{quizData.questions.length}</span>
6368
</p>
6469
<button
6570
onClick={resetQuiz}
@@ -73,7 +78,7 @@ export default function TutorialQuiz() {
7378
{/* Score Tracker Display */}
7479
<div className="flex justify-between text-amber-800 font-serif italic">
7580
<span>
76-
Question {questionNumber + 1} of {helloWorldQuiz.questions.length}
81+
Question {questionNumber + 1} of {quizData.questions.length}
7782
</span>
7883
<span>Score: {score}</span>
7984
</div>

0 commit comments

Comments
 (0)