Fix: Quiz fallback to QA data when MC not generated

Vocab units created via /vocabulary/units only have QA items, no
pre-generated MC questions. Quiz now falls back to generating MC
questions client-side from QA items (EN word → 4 DE options).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Benjamin Admin
2026-04-27 07:43:16 +02:00
parent 68d1679294
commit c252556528

View File

@@ -42,10 +42,37 @@ export default function QuizPage() {
const loadMC = async () => {
setIsLoading(true)
try {
const resp = await fetch(`${getApiBase()}/api/learning-units/${unitId}/mc`)
if (!resp.ok) throw new Error(`HTTP ${resp.status}`)
// Try MC endpoint first
let resp = await fetch(`${getApiBase()}/api/learning-units/${unitId}/mc`)
if (resp.ok) {
const data = await resp.json()
setQuestions(data.questions || [])
return
}
// Fallback: Generate MC from QA items (vocab units don't have pre-generated MC)
resp = await fetch(`${getApiBase()}/api/learning-units/${unitId}/qa`)
if (!resp.ok) throw new Error('Keine Daten verfuegbar')
const qaData = await resp.json()
const qaItems = qaData.qa_items || []
if (qaItems.length < 4) throw new Error('Zu wenige Woerter fuer Quiz (min. 4)')
// Build MC questions from QA: question=EN word, options=4 DE answers (1 correct + 3 random)
const generated: MCQuestion[] = qaItems.map((item: any, idx: number) => {
const others = qaItems.filter((_: any, i: number) => i !== idx)
const distractors = [...others].sort(() => Math.random() - 0.5).slice(0, 3)
const options = [
{ id: item.id, text: item.answer },
...distractors.map((d: any) => ({ id: d.id, text: d.answer })),
].sort(() => Math.random() - 0.5)
return {
id: item.id,
question: item.question,
options,
correct_answer: item.id,
}
})
setQuestions(generated)
} catch (err: any) {
setError(err.message)
} finally {