import React, { useState, useEffect } from 'react'; import { getNonce, isPro } from '../../Helpers'; import NoticeModal from '../NoticeModal/NoticeModal'; import AddIcon from '@mui/icons-material/Add'; import EditIcon from '@mui/icons-material/Edit'; import DeleteIcon from '@mui/icons-material/Delete'; import PollIcon from '@mui/icons-material/Poll'; import VisibilityIcon from '@mui/icons-material/Visibility'; import BarChartIcon from '@mui/icons-material/BarChart'; import LockIcon from '@mui/icons-material/Lock'; // Declare wp global for WordPress declare const wp: any; interface Poll { id: number; poll_name: string; description: string; featured_image: string; initial_form_id: number | null; answers: any[]; settings: any; created_at: string; updated_at: string; } interface PollListProps { onCreatePoll: () => void; onEditPoll: (poll: Poll) => void; onViewResults: (poll: Poll) => void; } const PollList: React.FC = ({ onCreatePoll, onEditPoll, onViewResults }) => { const [polls, setPolls] = useState([]); const [loading, setLoading] = useState(true); const [deleting, setDeleting] = useState(null); const [isProUser, setIsProUser] = useState(false); const [previewUrl, setPreviewUrl] = useState(null); const [modalConfig, setModalConfig] = useState({ isOpen: false, type: 'confirmation', title: '', message: '', onConfirm: () => { }, confirmText: 'Confirm', declineText: 'Cancel', mode: 'success', position: 'center' as 'center' | 'top-right' }); const closeModal = () => { setModalConfig(prev => ({ ...prev, isOpen: false })); }; useEffect(() => { loadPolls(); checkProStatus(); }, []); const checkProStatus = () => { const isProuser = isPro(); setIsProUser(isProuser); }; const showProModal = () => { setModalConfig({ isOpen: true, type: 'premium', title: '🚀 Upgrade to Pro', message: 'Create unlimited polls with Pro! Free version is limited to 1 poll. Upgrade now to unlock unlimited polls and advanced features!', mode: 'success', onConfirm: () => { window.open('https://wpazleen.com/simple-form-pricing/', '_blank'); closeModal(); }, confirmText: 'Upgrade Now', declineText: 'Maybe Later', position: 'center' }); }; const handleCreatePoll = () => { if (!isProUser && polls.length >= 1) { showProModal(); return; } onCreatePoll(); }; const loadPolls = () => { setLoading(true); (wp as any).ajax.send('simpleform_get_polls', { data: { nonce: getNonce(), }, success(response: any) { if (response.polls) { setPolls(response.polls); } setLoading(false); }, error(error: any) { console.error('Error loading polls:', error); setLoading(false); }, }); }; const handleDelete = (poll: Poll) => { setModalConfig({ isOpen: true, type: 'confirmation', title: 'Delete Poll', message: `Are you sure you want to delete "${poll.poll_name}"? This action cannot be undone.`, mode: 'warning', onConfirm: () => { setDeleting(poll.id); (wp as any).ajax.send('simpleform_delete_poll', { data: { nonce: getNonce(), poll_id: poll.id, }, success(response: any) { setPolls(prev => prev.filter(p => p.id !== poll.id)); setDeleting(null); setModalConfig({ isOpen: true, type: 'toast', title: 'Success', message: `Poll "${poll.poll_name}" deleted successfully`, position: 'top-right', mode: 'success', onConfirm: () => {}, confirmText: 'OK', declineText: 'Cancel', }); setTimeout(closeModal, 3000); }, error(error: any) { console.error('Error deleting poll:', error); setDeleting(null); setModalConfig({ isOpen: true, type: 'toast', title: 'Error', message: 'Failed to delete poll. Please try again.', position: 'top-right', mode: 'error', onConfirm: () => {}, confirmText: 'OK', declineText: 'Cancel', }); setTimeout(closeModal, 3000); }, }); closeModal(); }, confirmText: 'Yes, Delete', declineText: 'Cancel', position: 'center' }); }; const handlePreview = (poll: Poll) => { (wp as any).ajax.send('simpleform_get_poll_preview_url', { data: { nonce: getNonce(), poll_id: poll.id, }, success(response: any) { if (response.preview_url) { setPreviewUrl(response.preview_url); } }, error(error: any) { console.error('Error getting preview URL:', error); setModalConfig({ isOpen: true, type: 'toast', title: 'Error', message: 'Failed to generate preview URL.', position: 'top-right', mode: 'error', onConfirm: () => {}, confirmText: 'OK', declineText: 'Cancel', }); setTimeout(closeModal, 3000); }, }); }; const closePreview = () => { setPreviewUrl(null); }; const formatDate = (dateString: string) => { return new Date(dateString).toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }); }; if (loading) { return (

Loading polls...

); } return (

Polls

Create interactive polls to gather opinions and feedback from your audience

{polls.length === 0 ? (

No polls yet

Create your first poll to start gathering feedback

) : (
{polls.map(poll => (
{poll.featured_image && (
{poll.poll_name}
)}

{poll.poll_name}

{poll.description || 'No description'}

Answers: {poll.answers?.length || 0}
Type: {poll.settings?.allow_multiple ? 'Multiple Choice' : 'Single Choice'}
Created: {formatDate(poll.created_at)}
[simple_poll id="{poll.id}"]
))}
)} {previewUrl && (
e.stopPropagation()}>

Poll Preview