import React, { useState, useEffect } from 'react'; import { useAuth } from 'react-oidc-context'; import { Quiz, Question, AnswerOption } from '../types'; import { v4 as uuidv4 } from 'uuid'; import { Plus, Save, Trash2, CheckCircle, Circle, X, BookOpen } from 'lucide-react'; import { COLORS, SHAPES } from '../constants'; interface QuizCreatorProps { onFinalize: (quiz: Quiz, saveToLibrary: boolean) => void; onCancel: () => void; } export const QuizCreator: React.FC = ({ onFinalize, onCancel }) => { const auth = useAuth(); const [title, setTitle] = useState(''); const [questions, setQuestions] = useState([]); const [qText, setQText] = useState(''); const [options, setOptions] = useState(['', '', '', '']); const [reasons, setReasons] = useState(['', '', '', '']); const [correctIdx, setCorrectIdx] = useState(0); const [saveToLibrary, setSaveToLibrary] = useState(auth.isAuthenticated); const [hasToggledSave, setHasToggledSave] = useState(false); useEffect(() => { if (auth.isAuthenticated && !hasToggledSave) { setSaveToLibrary(true); } }, [auth.isAuthenticated, hasToggledSave]); const handleAddQuestion = () => { if (!qText.trim() || options.some(o => !o.trim())) { alert("Please fill in the question and all 4 options."); return; } const shapes = ['triangle', 'diamond', 'circle', 'square'] as const; const colors = ['red', 'blue', 'yellow', 'green'] as const; const newOptions: AnswerOption[] = options.map((text, idx) => ({ text, isCorrect: idx === correctIdx, shape: shapes[idx], color: colors[idx], reason: reasons[idx].trim() || undefined })); const newQuestion: Question = { id: uuidv4(), text: qText, options: newOptions, timeLimit: 20 }; setQuestions([...questions, newQuestion]); setQText(''); setOptions(['', '', '', '']); setReasons(['', '', '', '']); setCorrectIdx(0); }; const handleRemoveQuestion = (id: string) => { setQuestions(questions.filter(q => q.id !== id)); }; const handleFinalize = () => { if (!title.trim() || questions.length === 0) return; onFinalize({ title, questions }, saveToLibrary); }; return (

Create Quiz

Build your masterpiece

{/* Decorative Circles */}
setTitle(e.target.value)} className="w-full p-4 border-4 border-gray-200 rounded-2xl text-2xl font-bold focus:border-theme-primary outline-none transition-colors" placeholder="e.g., The Ultimate Trivia" />
{questions.map((q, idx) => (
{idx + 1} {q.text}
))}

New Question

setQText(e.target.value)} className="w-full p-4 border-4 border-white shadow-sm rounded-2xl font-bold text-lg focus:border-blue-400 outline-none" placeholder="What is the question?" />
{options.map((opt, idx) => { const isSelected = correctIdx === idx; const borderColor = isSelected ? 'border-green-500' : 'border-white'; const bgClass = COLORS[['red','blue','yellow','green'][idx] as any]; return (
{ const newOpts = [...options]; newOpts[idx] = e.target.value; setOptions(newOpts); }} className="w-full p-2 outline-none font-bold text-gray-700 bg-transparent placeholder:font-normal" placeholder={`Option ${idx + 1}`} />
{ const newReasons = [...reasons]; newReasons[idx] = e.target.value; setReasons(newReasons); }} className="w-full p-2 text-sm outline-none text-gray-500 bg-gray-50 rounded-lg placeholder:text-gray-400" placeholder={isSelected ? "Why is this correct? (optional)" : "Why is this wrong? (optional)"} />
) })}
{auth.isAuthenticated ? ( ) : (
)}
); };