import React, { useState } from 'react' import { Upload, Plus, Trash2, Edit2, Download } from 'lucide-react' export default function QuestionManager({ onQuestionsImport, currentQuestions }) { const [showImportModal, setShowImportModal] = useState(false) const [showAddModal, setShowAddModal] = useState(false) const [importMethod, setImportMethod] = useState('json') const [jsonInput, setJsonInput] = useState('') const [csvInput, setCsvInput] = useState('') const [newQuestion, setNewQuestion] = useState({ text: '', options: [ { label: 'A', text: '' }, { label: 'B', text: '' }, { label: 'C', text: '' }, { label: 'D', text: '' }, { label: 'E', text: '' } ], image: null }) const [errorMessage, setErrorMessage] = useState('') const [successMessage, setSuccessMessage] = useState('') const handleJsonImport = () => { try { setErrorMessage('') const parsed = JSON.parse(jsonInput) // Validate format if (!Array.isArray(parsed)) { throw new Error('Questions must be an array') } parsed.forEach((q, idx) => { if (!q.text || !Array.isArray(q.options) || q.options.length !== 5) { throw new Error(`Question ${idx + 1}: Must have text and 5 options (A-E)`) } }) onQuestionsImport(parsed) setSuccessMessage(`✅ Successfully imported ${parsed.length} questions!`) setJsonInput('') setShowImportModal(false) setTimeout(() => setSuccessMessage(''), 3000) } catch (err) { setErrorMessage(`❌ Error: ${err.message}`) } } const handleCsvImport = () => { try { setErrorMessage('') const lines = csvInput.trim().split('\n') const questions = [] for (let i = 1; i < lines.length; i++) { const line = lines[i] if (!line.trim()) continue const parts = line.split('|').map(p => p.trim()) if (parts.length < 6) { throw new Error(`Row ${i + 1}: Need Question|A|B|C|D|E format`) } questions.push({ id: i, text: parts[0], options: [ { label: 'A', text: parts[1] }, { label: 'B', text: parts[2] }, { label: 'C', text: parts[3] }, { label: 'D', text: parts[4] }, { label: 'E', text: parts[5] } ], image: parts[6] || null }) } onQuestionsImport(questions) setSuccessMessage(`✅ Successfully imported ${questions.length} questions!`) setCsvInput('') setShowImportModal(false) setTimeout(() => setSuccessMessage(''), 3000) } catch (err) { setErrorMessage(`❌ Error: ${err.message}`) } } const handleAddQuestion = () => { try { setErrorMessage('') if (!newQuestion.text.trim()) { throw new Error('Question text is required') } if (newQuestion.options.some(opt => !opt.text.trim())) { throw new Error('All 5 options must have text') } const questionToAdd = { id: (currentQuestions?.length || 0) + 1, ...newQuestion } onQuestionsImport([...(currentQuestions || []), questionToAdd]) setSuccessMessage('✅ Question added successfully!') setNewQuestion({ text: '', options: [ { label: 'A', text: '' }, { label: 'B', text: '' }, { label: 'C', text: '' }, { label: 'D', text: '' }, { label: 'E', text: '' } ], image: null }) setShowAddModal(false) setTimeout(() => setSuccessMessage(''), 3000) } catch (err) { setErrorMessage(`❌ Error: ${err.message}`) } } const handleDownloadTemplate = () => { const template = [ { id: 1, text: "Example question text here?", options: [ { label: 'A', text: 'Option A text' }, { label: 'B', text: 'Option B text' }, { label: 'C', text: 'Option C text' }, { label: 'D', text: 'Option D text' }, { label: 'E', text: 'Option E text' } ], image: null } ] const element = document.createElement('a') element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(JSON.stringify(template, null, 2))) element.setAttribute('download', 'questions-template.json') element.style.display = 'none' document.body.appendChild(element) element.click() document.body.removeChild(element) } return (
✅ {currentQuestions.length} questions loaded
)}