import React, { useState } from 'react'; import AddIcon from '@mui/icons-material/Add'; import DeleteIcon from '@mui/icons-material/Delete'; import DragIndicatorIcon from '@mui/icons-material/DragIndicator'; import NoticeModal from './../NoticeModal/NoticeModal'; interface Answer { id: string; text: string; image_url?: string; } interface Question { id: string; text: string; description?: string; answers: Answer[]; required: boolean; allow_multiple: boolean; } interface QuestionEditorProps { questions: Question[]; onUpdate: (questions: Question[]) => void; } const QuestionEditor: React.FC = ({ questions, onUpdate }) => { const [modalConfig, setModalConfig] = useState({ isOpen: false, type: 'toast' as 'success' | 'error' | 'confirmation' | 'premium' | 'toast', title: '', message: '', position: 'top-right' as 'top-right' | 'center', mode: 'success' as 'success' | 'error', onConfirm: () => { }, onClose: () => { }, confirmText: 'OK', declineText: 'Cancel' }); const closeModal = () => { setModalConfig(prev => ({ ...prev, isOpen: false })); }; const showErrorModal = (message: string) => { setModalConfig({ isOpen: true, type: 'toast', title: 'Error', message: message, position: 'top-right', mode: 'error', onConfirm: closeModal, onClose: closeModal, confirmText: 'OK', declineText: 'Cancel' }); }; const generateId = () => `id_${Math.random().toString(36).substring(2, 11)}`; const addQuestion = () => { const newQuestion: Question = { id: generateId(), text: '', description: '', answers: [ { id: generateId(), text: '', image_url: '' }, { id: generateId(), text: '', image_url: '' } ], required: true, allow_multiple: false }; onUpdate([...questions, newQuestion]); }; const updateQuestion = (questionId: string, field: keyof Question, value: any) => { const updatedQuestions = questions.map(q => q.id === questionId ? { ...q, [field]: value } : q ); onUpdate(updatedQuestions); }; const deleteQuestion = (questionId: string) => { if (questions.length <= 1) { showErrorModal('You must have at least one question.'); return; } const updatedQuestions = questions.filter(q => q.id !== questionId); onUpdate(updatedQuestions); }; const addAnswer = (questionId: string) => { const updatedQuestions = questions.map(q => { if (q.id === questionId) { const newAnswer: Answer = { id: generateId(), text: '', image_url: '' }; return { ...q, answers: [...q.answers, newAnswer] }; } return q; }); onUpdate(updatedQuestions); }; const updateAnswer = (questionId: string, answerId: string, field: keyof Answer, value: string) => { const updatedQuestions = questions.map(q => { if (q.id === questionId) { const updatedAnswers = q.answers.map(a => a.id === answerId ? { ...a, [field]: value } : a ); return { ...q, answers: updatedAnswers }; } return q; }); onUpdate(updatedQuestions); }; const deleteAnswer = (questionId: string, answerId: string) => { const question = questions.find(q => q.id === questionId); if (question && question.answers.length <= 2) { showErrorModal('Each question must have at least 2 answers.'); return; } const updatedQuestions = questions.map(q => { if (q.id === questionId) { const updatedAnswers = q.answers.filter(a => a.id !== answerId); return { ...q, answers: updatedAnswers }; } return q; }); onUpdate(updatedQuestions); }; return (

Survey Structure: Poll Title → Question 1 → Question 2 → etc.

Each question below will be presented separately to users after they see your poll title and description.

{questions.map((question, questionIndex) => (

Question {questionIndex + 1}

updateQuestion(question.id, 'text', e.target.value)} placeholder="Enter your question..." />