'use client'; import { useState } from 'react'; import { X, Plus, Trash2, Loader2, ChevronDown } from 'lucide-react'; export default function PollModal({ channel, onClose, onPublished }) { const [question, setQuestion] = useState(''); const [options, setOptions] = useState(['', '']); const [isAnonymous, setAnonymous] = useState(true); const [isMultiple, setMultiple] = useState(false); const [type, setType] = useState('regular'); const [correctId, setCorrectId] = useState(0); const [explanation, setExplanation] = useState(''); const [scheduleAt, setScheduleAt] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); function addOption() { if (options.length < 10) setOptions([...options, '']); } function removeOption(i) { if (options.length > 2) setOptions(options.filter((_, idx) => idx !== i)); } function setOption(i, val) { setOptions(options.map((o, idx) => idx === i ? val : o)); } async function submit() { if (!question.trim()) return setError('Введите вопрос'); if (options.some(o => !o.trim())) return setError('Заполните все варианты ответа'); setLoading(true); setError(''); try { const res = await fetch(`/api/channels/${channel.id}/poll`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ question: question.trim(), options: options.map(o => o.trim()), is_anonymous: isAnonymous, allows_multiple_answers: isMultiple, type, correct_option_id: type === 'quiz' ? correctId : undefined, explanation: type === 'quiz' ? explanation : undefined, schedule_at: scheduleAt || null, }), }).then(r => r.json()); if (res.error) throw new Error(res.error); onPublished?.(res); onClose(); } catch (err) { setError(err.message); } setLoading(false); } return (
{error}
}