Make plant quiz dynamic with fetch from Plantlix API and fallback

This commit is contained in:
Felix Brabetz
2026-05-16 06:44:31 +02:00
parent 14fb2d029c
commit a431237b70
+77 -28
View File
@@ -7,39 +7,88 @@
<h2 class="text-3xl sm:text-4xl font-luxury font-bold text-white mb-6">Das kleine Pflanzen-Quiz</h2> <h2 class="text-3xl sm:text-4xl font-luxury font-bold text-white mb-6">Das kleine Pflanzen-Quiz</h2>
<p class="text-stone-400 mb-12 font-light">Teste dein Wissen! In Kooperation mit den Lern-Algorithmen von Plantlix.de.</p> <p class="text-stone-400 mb-12 font-light">Teste dein Wissen! In Kooperation mit den Lern-Algorithmen von Plantlix.de.</p>
<div class="glass p-8 rounded-2xl border border-stone-800 text-left" x-data="{ answered: false, correct: false }"> <div class="glass p-8 rounded-2xl border border-stone-800 text-left"
<h3 class="text-xl font-bold text-white mb-6">Welcher dieser Bäume ist ein extremer Tiefwurzler und übersteht Trockenheit am besten?</h3> x-data="{
answered: false,
correct: false,
selected: '',
question: 'Lade Quiz-Frage von Plantlix...',
options: [],
correctAnswer: '',
explanation: '',
loading: true,
init() {
// Versuche die Frage von Plantlix zu holen
fetch('https://plantlix.de/api/quiz/random')
.then(res => {
if (!res.ok) throw new Error('API Fehler');
return res.json();
})
.then(data => {
this.question = data.question;
this.options = data.options; // Erwartet Array von Objekten: { key: 'A', text: '...' }
this.correctAnswer = data.correctAnswer; // Erwartet 'A', 'B' oder 'C'
this.explanation = data.explanation;
this.loading = false;
})
.catch(err => {
console.log('Plantlix API nicht erreichbar, nutze Fallback-Frage.');
// Fallback-Daten (die ursprüngliche Frage)
this.question = 'Welcher dieser Bäume ist ein extremer Tiefwurzler und übersteht Trockenheit am besten?';
this.options = [
{ key: 'A', text: 'Die Rotbuche (*Fagus sylvatica*)' },
{ key: 'B', text: 'Die Stieleiche (*Quercus robur*)' },
{ key: 'C', text: 'Die Fichte (*Picea abies*)' }
];
this.correctAnswer = 'B';
this.explanation = 'Die Stieleiche bildet eine tiefgehende Pfahlwurzel und kommt so auch in trockenen Sommern an Wasser. Buchen und Fichten sind Flachwurzler und leiden schneller unter Trockenstress.';
this.loading = false;
});
}
}">
<div class="space-y-4" x-show="!answered"> <!-- Loading State -->
<button @click="answered = true; correct = false" class="w-full text-left p-4 bg-stone-900/50 hover:bg-stone-800 border border-stone-800 rounded-lg text-stone-300 transition-colors flex justify-between items-center"> <div x-show="loading" class="text-center py-6">
<span>A) Die Rotbuche (*Fagus sylvatica*)</span> <div class="inline-block animate-spin rounded-full h-8 w-8 border-t-2 border-b-2 border-emerald-500 mb-4"></div>
<i class="fa-solid fa-chevron-right text-stone-600"></i> <p class="text-stone-500 text-sm font-mono" x-text="question"></p>
</button>
<button @click="answered = true; correct = true" class="w-full text-left p-4 bg-stone-900/50 hover:bg-stone-800 border border-stone-800 rounded-lg text-stone-300 transition-colors flex justify-between items-center">
<span>B) Die Stieleiche (*Quercus robur*)</span>
<i class="fa-solid fa-chevron-right text-stone-600"></i>
</button>
<button @click="answered = true; correct = false" class="w-full text-left p-4 bg-stone-900/50 hover:bg-stone-800 border border-stone-800 rounded-lg text-stone-300 transition-colors flex justify-between items-center">
<span>C) Die Fichte (*Picea abies*)</span>
<i class="fa-solid fa-chevron-right text-stone-600"></i>
</button>
</div> </div>
<!-- Result --> <!-- Quiz Content -->
<div x-show="answered" x-cloak class="text-center py-6" x-transition:enter="transition ease-out duration-300" x-transition:enter-start="opacity-0 scale-95" x-transition:enter-end="opacity-100 scale-100"> <div x-show="!loading" x-cloak>
<div x-show="correct" class="text-emerald-400"> <h3 class="text-xl font-bold text-white mb-6" x-text="question"></h3>
<div class="text-4xl mb-4"><i class="fa-solid fa-circle-check"></i></div>
<h4 class="text-xl font-bold mb-2">Absolut richtig!</h4> <div class="space-y-4" x-show="!answered">
<p class="text-stone-400 text-sm font-light">Die Stieleiche bildet eine tiefgehende Pfahlwurzel und kommt so auch in trockenen Sommern an Wasser. Buchen und Fichten sind Flachwurzler und leiden schneller unter Trockenstress.</p> <template x-for="option in options" :key="option.key">
<button @click="answered = true; selected = option.key; correct = (option.key === correctAnswer)"
class="w-full text-left p-4 bg-stone-900/50 hover:bg-stone-800 border border-stone-800 rounded-lg text-stone-300 transition-colors flex justify-between items-center group">
<span>
<span class="font-mono text-emerald-500 mr-2" x-text="option.key + ')'"></span>
<span x-text="option.text"></span>
</span>
<i class="fa-solid fa-chevron-right text-stone-600 group-hover:text-emerald-500 transition-colors"></i>
</button>
</template>
</div> </div>
<div x-show="!correct" class="text-amber-500">
<div class="text-4xl mb-4"><i class="fa-solid fa-circle-xmark"></i></div> <!-- Result -->
<h4 class="text-xl font-bold mb-2">Leider nicht ganz!</h4> <div x-show="answered" class="text-center py-6" x-transition:enter="transition ease-out duration-300" x-transition:enter-start="opacity-0 scale-95" x-transition:enter-end="opacity-100 scale-100">
<p class="text-stone-400 text-sm font-light">Die richtige Antwort ist **B) Die Stieleiche**. Sie bildet eine tiefe Pfahlwurzel. Rotbuchen und Fichten wurzeln eher flach und sind daher anfälliger für Trockenheit.</p> <div x-show="correct" class="text-emerald-400">
<div class="text-4xl mb-4"><i class="fa-solid fa-circle-check"></i></div>
<h4 class="text-xl font-bold mb-2">Absolut richtig!</h4>
<p class="text-stone-400 text-sm font-light" x-text="explanation"></p>
</div>
<div x-show="!correct" class="text-amber-500">
<div class="text-4xl mb-4"><i class="fa-solid fa-circle-xmark"></i></div>
<h4 class="text-xl font-bold mb-2">Leider nicht ganz!</h4>
<p class="text-stone-400 text-sm font-light">
Die richtige Antwort ist <span class="font-bold" x-text="correctAnswer"></span>.
<span x-text="explanation"></span>
</p>
</div>
<button @click="answered = false; selected = ''; correct = false; init();" class="mt-8 text-xs text-stone-500 hover:text-white underline uppercase tracking-wider transition-colors">
Nächste Frage laden
</button>
</div> </div>
<button @click="answered = false" class="mt-8 text-xs text-stone-500 hover:text-white underline uppercase tracking-wider transition-colors">
Nochmal versuchen
</button>
</div> </div>
</div> </div>
</div> </div>