import React, { useState } from 'react'; import DeleteIcon from '@mui/icons-material/Delete'; import AddIcon from '@mui/icons-material/Add'; import ImageIcon from '@mui/icons-material/Image'; import CheckCircleIcon from '@mui/icons-material/CheckCircle'; import RadioButtonUncheckedIcon from '@mui/icons-material/RadioButtonUnchecked'; import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; import ExpandLessIcon from '@mui/icons-material/ExpandLess'; import KeyboardDoubleArrowDownIcon from '@mui/icons-material/KeyboardDoubleArrowDown'; import KeyboardDoubleArrowUpIcon from '@mui/icons-material/KeyboardDoubleArrowUp'; import DragIndicatorIcon from '@mui/icons-material/DragIndicator'; interface Answer { id: string; text: string; image_url?: string; is_correct: boolean; } interface Question { id: string; question: string; description?: string; image_url?: string; answers: Answer[]; } interface QuizQuestionEditorProps { question: Question; questionNumber: number; totalQuestions: number; questionIndex: number; onUpdate: (question: Question) => void; onDelete: () => void; onMove: (direction: 'up' | 'down') => void; canDelete: boolean; setModalConfig: (config: any) => void; closeModal: () => void; } const QuizQuestionEditor: React.FC = ({ question, questionNumber, totalQuestions, questionIndex, onUpdate, onDelete, onMove, canDelete, setModalConfig, closeModal }) => { const [collapsed, setCollapsed] = useState(false); const generateId = () => { return 'id_' + Math.random().toString(36).substr(2, 9); }; const updateQuestion = (field: keyof Question, value: any) => { onUpdate({ ...question, [field]: value, }); }; const addAnswer = () => { const newAnswer: Answer = { id: generateId(), text: '', image_url: '', is_correct: false, }; updateQuestion('answers', [...question.answers, newAnswer]); }; const updateAnswer = (answerId: string, field: keyof Answer, value: any) => { const updatedAnswers = question.answers.map(answer => answer.id === answerId ? { ...answer, [field]: value } : answer ); updateQuestion('answers', updatedAnswers); }; const deleteAnswer = (answerId: string) => { if (question.answers.length <= 2) { setModalConfig({ isOpen: true, type: 'toast', title: 'Error', message: 'A question must have at least 2 answers.', position: 'top-right', mode: 'error', }); setTimeout(closeModal, 3000); return; } const updatedAnswers = question.answers.filter(answer => answer.id !== answerId); updateQuestion('answers', updatedAnswers); }; const setCorrectAnswer = (answerId: string) => { const updatedAnswers = question.answers.map(answer => ({ ...answer, is_correct: answer.id === answerId, })); updateQuestion('answers', updatedAnswers); }; const openMediaUploader = (type: 'question' | 'answer', answerId?: string) => { if (typeof (window as any).wp !== 'undefined' && (window as any).wp.media) { const mediaUploader = (window as any).wp.media({ title: 'Select Image', button: { text: 'Use Image' }, multiple: false, library: { type: 'image' } }); mediaUploader.on('select', () => { const attachment = mediaUploader.state().get('selection').first().toJSON(); const imageUrl = attachment.url; if (type === 'question') { updateQuestion('image_url', imageUrl); } else if (type === 'answer' && answerId) { updateAnswer(answerId, 'image_url', imageUrl); } }); mediaUploader.open(); } else { alert('WordPress media uploader is not available.'); } }; return (

Question {questionNumber}

{canDelete && ( )}
{!collapsed && (