Next.js app with Hoeren, Lesen, Schreiben, Sprechen, Grammatik and Wortschatz sections, flashcards with spaced repetition, speaking chat, XP/streaks/badges, daily reminders (PWA) and Docker Compose setup for Coolify.
94 lines
3.5 KiB
TypeScript
94 lines
3.5 KiB
TypeScript
"use client";
|
||
|
||
import { useState } from "react";
|
||
import type { Question } from "@/data/types";
|
||
import { sfx } from "@/lib/sfx";
|
||
import Ar from "./Ar";
|
||
|
||
interface Props {
|
||
questions: Question[];
|
||
onFinish: (correct: number, total: number) => void;
|
||
/** Im Prüfungsmodus wird die Lösung erst am Ende gezeigt. */
|
||
examMode?: boolean;
|
||
}
|
||
|
||
const letters = "abcdefghij";
|
||
|
||
export default function Quiz({ questions, onFinish, examMode = false }: Props) {
|
||
const [answers, setAnswers] = useState<(number | null)[]>(() => questions.map(() => null));
|
||
const [submitted, setSubmitted] = useState(false);
|
||
|
||
const correctOf = (q: Question) => (q.kind === "tf" ? (q.answer ? 0 : 1) : q.answer);
|
||
const allAnswered = answers.every((a) => a !== null);
|
||
const reveal = (i: number) => (examMode ? submitted : answers[i] !== null);
|
||
|
||
const choose = (qi: number, oi: number) => {
|
||
if (answers[qi] !== null && !examMode) return;
|
||
if (submitted) return;
|
||
const next = [...answers];
|
||
next[qi] = oi;
|
||
setAnswers(next);
|
||
if (!examMode) {
|
||
if (oi === correctOf(questions[qi])) sfx.correct();
|
||
else sfx.wrong();
|
||
}
|
||
};
|
||
|
||
const finish = () => {
|
||
setSubmitted(true);
|
||
const correct = questions.reduce((n, q, i) => n + (answers[i] === correctOf(q) ? 1 : 0), 0);
|
||
onFinish(correct, questions.length);
|
||
};
|
||
|
||
return (
|
||
<div>
|
||
{questions.map((q, qi) => {
|
||
const options = q.kind === "tf" ? ["Richtig ✅", "Falsch ❌"] : q.options;
|
||
const chosen = answers[qi];
|
||
const right = correctOf(q);
|
||
const shown = reveal(qi);
|
||
return (
|
||
<div key={qi} className="card fade-in" style={{ marginTop: 12 }}>
|
||
<div className="row" style={{ alignItems: "flex-start" }}>
|
||
<span className="pill">{qi + 1}</span>
|
||
<div style={{ flex: 1 }}>
|
||
<div style={{ fontWeight: 800 }}>{q.q}</div>
|
||
<Ar block>
|
||
<span className="small muted">{q.qAr}</span>
|
||
</Ar>
|
||
</div>
|
||
</div>
|
||
<div className={q.kind === "mc" && q.options.every((o) => o.length <= 2) ? "grid grid-3" : ""}>
|
||
{options.map((o, oi) => {
|
||
let cls = "option";
|
||
if (shown && oi === right) cls += " correct";
|
||
else if (shown && chosen === oi && oi !== right) cls += " wrong";
|
||
else if (!shown && chosen === oi) cls += " selected";
|
||
return (
|
||
<button key={oi} type="button" className={cls} disabled={(!examMode && chosen !== null) || submitted} onClick={() => choose(qi, oi)}>
|
||
<span className="key">{q.kind === "tf" ? (oi === 0 ? "R" : "F") : letters[oi]}</span>
|
||
<span>{o}</span>
|
||
</button>
|
||
);
|
||
})}
|
||
</div>
|
||
{shown && chosen !== null && (
|
||
<div className={`feedback ${chosen === right ? "ok" : "no"}`}>
|
||
{chosen === right ? "Super! Richtig 🎉" : "Leider falsch."}
|
||
{q.why ? <span style={{ fontWeight: 600 }}> – {q.why}</span> : null}
|
||
</div>
|
||
)}
|
||
</div>
|
||
);
|
||
})}
|
||
{!submitted && (
|
||
<div style={{ marginTop: 16 }}>
|
||
<button type="button" className="btn green block" disabled={!allAnswered} onClick={finish}>
|
||
{allAnswered ? "Auswerten ✔" : `Noch ${answers.filter((a) => a === null).length} Fragen offen`}
|
||
</button>
|
||
</div>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|