import { useState } from 'react' import './QuestionsModal.css' const QuestionsModal = ({ isOpen, onClose, questions, onUpdateQuestions }) => { const [editingQuestion, setEditingQuestion] = useState(null) const [questionText, setQuestionText] = useState('') const [answers, setAnswers] = useState([ { text: '', points: 100 }, { text: '', points: 80 }, { text: '', points: 60 }, { text: '', points: 40 }, { text: '', points: 20 }, { text: '', points: 10 }, ]) const [jsonError, setJsonError] = useState('') if (!isOpen) return null const resetForm = () => { setEditingQuestion(null) setQuestionText('') setAnswers([ { text: '', points: 100 }, { text: '', points: 80 }, { text: '', points: 60 }, { text: '', points: 40 }, { text: '', points: 20 }, { text: '', points: 10 }, ]) setJsonError('') } const handleBackdropClick = (e) => { if (e.target === e.currentTarget) { onClose() resetForm() } } const handleClose = () => { onClose() resetForm() } const handleEdit = (question) => { setEditingQuestion(question) setQuestionText(question.text) setAnswers([...question.answers]) setJsonError('') } const handleCancelEdit = () => { resetForm() } const handleAnswerChange = (index, field, value) => { const updatedAnswers = [...answers] if (field === 'text') { updatedAnswers[index].text = value } else if (field === 'points') { updatedAnswers[index].points = parseInt(value) || 0 } setAnswers(updatedAnswers) } const handleAddAnswer = () => { const minPoints = Math.min(...answers.map(a => a.points)) setAnswers([...answers, { text: '', points: Math.max(0, minPoints - 10) }]) } const handleRemoveAnswer = (index) => { if (answers.length > 1) { setAnswers(answers.filter((_, i) => i !== index)) } } const validateForm = () => { if (!questionText.trim()) { setJsonError('Введите текст вопроса') return false } if (answers.length === 0) { setJsonError('Добавьте хотя бы один ответ') return false } const hasEmptyAnswers = answers.some(a => !a.text.trim()) if (hasEmptyAnswers) { setJsonError('Заполните все ответы') return false } return true } const handleSave = () => { if (!validateForm()) return const questionData = { id: editingQuestion ? editingQuestion.id : Date.now(), text: questionText.trim(), answers: answers .filter(a => a.text.trim()) .map(a => ({ text: a.text.trim(), points: a.points, })), } let updatedQuestions if (editingQuestion) { updatedQuestions = questions.map(q => q.id === editingQuestion.id ? questionData : q ) } else { updatedQuestions = [...questions, questionData] } onUpdateQuestions(updatedQuestions) resetForm() } const handleDelete = (questionId) => { if (window.confirm('Вы уверены, что хотите удалить этот вопрос?')) { const updatedQuestions = questions.filter(q => q.id !== questionId) onUpdateQuestions(updatedQuestions) if (editingQuestion && editingQuestion.id === questionId) { resetForm() } } } const handleExportJson = () => { try { const jsonString = JSON.stringify(questions, null, 2) const blob = new Blob([jsonString], { type: 'application/json' }) const url = URL.createObjectURL(blob) const link = document.createElement('a') link.href = url link.download = 'questions.json' document.body.appendChild(link) link.click() document.body.removeChild(link) URL.revokeObjectURL(url) setJsonError('') } catch (error) { setJsonError('Ошибка при экспорте: ' + error.message) } } const handleImportJson = () => { const input = document.createElement('input') input.type = 'file' input.accept = '.json' input.onchange = (e) => { const file = e.target.files[0] if (!file) return const reader = new FileReader() reader.onload = (event) => { try { const jsonContent = JSON.parse(event.target.result) if (!Array.isArray(jsonContent)) { setJsonError('JSON должен содержать массив вопросов') return } // Валидация структуры const isValid = jsonContent.every(q => q.id && typeof q.text === 'string' && Array.isArray(q.answers) && q.answers.every(a => a.text && typeof a.points === 'number') ) if (!isValid) { setJsonError('Неверный формат JSON. Ожидается массив объектов с полями: id, text, answers') return } onUpdateQuestions(jsonContent) setJsonError('') alert(`Успешно импортировано ${jsonContent.length} вопросов`) } catch (error) { setJsonError('Ошибка при импорте: ' + error.message) } } reader.readAsText(file) } input.click() } return (
Нет вопросов. Добавьте вопросы для игры.
) : (