import React, { useState } from 'react'; import confetti from 'canvas-confetti'; import { CheckCircle2, Sparkles, RefreshCw, Zap } from 'lucide-react'; import { CANDIDATOS_DEFAULT } from '../types'; interface VotingStepProps { voterName: string; existingVoteFor: string | null; onCastVote: (candidate: string) => Promise; isLoading: boolean; } export const VotingStep: React.FC = ({ voterName, existingVoteFor, onCastVote, isLoading, }) => { const [selectedCandidate, setSelectedCandidate] = useState(null); const [submitting, setSubmitting] = useState(false); const normalizedVoter = voterName.trim().toUpperCase(); // Función anti-autovoto const isSelfVote = (candidateName: string): boolean => { if (!normalizedVoter) return false; const candUpper = candidateName.toUpperCase(); if (normalizedVoter === candUpper) return true; const voterWords = normalizedVoter.split(/\s+/); if (voterWords.includes(candUpper)) return true; return false; }; const handleSelect = (candidate: string) => { if (isSelfVote(candidate) || isLoading || submitting) return; setSelectedCandidate(candidate); }; const handleConfirmVote = async () => { const candidateToVote = selectedCandidate || existingVoteFor; if (!candidateToVote || isSelfVote(candidateToVote) || submitting || isLoading) return; setSubmitting(true); try { await onCastVote(candidateToVote); // Confetti celebratory animation try { confetti({ particleCount: 75, spread: 70, origin: { y: 0.6 }, colors: ['#00B7D4', '#00e676', '#ffffff', '#38bdf8'], }); } catch { // ignore } } catch (err) { console.error('Error al emitir voto:', err); } finally { setSubmitting(false); } }; return (
9 Candidatos
{/* Candidatos Pills Grid (Design Theme) */}
{CANDIDATOS_DEFAULT.map((cand, index) => { const isSelf = isSelfVote(cand); const isVoted = existingVoteFor === cand; const isSelected = selectedCandidate === cand || (!selectedCandidate && isVoted); const isLast = index === CANDIDATOS_DEFAULT.length - 1 && CANDIDATOS_DEFAULT.length % 2 !== 0; return ( ); })}
{/* Confirmation and Atomic Action Button */}
{existingVoteFor && (
Voto emitido para:{' '} {existingVoteFor}
Atómico
)}
); };