// Inscription screen — public tournament registration const { useState: useInscState, useEffect: useInscEffect } = React; // ── CPF helpers ─────────────────────────────────────────────── function formatCPF(v) { const d = v.replace(/\D/g, '').slice(0, 11); if (d.length <= 3) return d; if (d.length <= 6) return d.replace(/(\d{3})(\d+)/, '$1.$2'); if (d.length <= 9) return d.replace(/(\d{3})(\d{3})(\d+)/, '$1.$2.$3'); return d.replace(/(\d{3})(\d{3})(\d{3})(\d{1,2})/, '$1.$2.$3-$4'); } function formatPhone(v) { const d = v.replace(/\D/g, '').slice(0, 11); if (d.length <= 2) return d.length ? `(${d}` : d; if (d.length <= 6) return d.replace(/(\d{2})(\d+)/, '($1) $2'); if (d.length <= 10) return d.replace(/(\d{2})(\d{4})(\d+)/, '($1) $2-$3'); return d.replace(/(\d{2})(\d{5})(\d{4})/, '($1) $2-$3'); } function validateCPF(cpf) { const d = cpf.replace(/\D/g, ''); if (d.length !== 11) return false; if (/^(\d)\1{10}$/.test(d)) return false; let sum = 0; for (let i = 0; i < 9; i++) sum += parseInt(d[i]) * (10 - i); let r = (sum * 10) % 11; if (r >= 10) r = 0; if (r !== parseInt(d[9])) return false; sum = 0; for (let i = 0; i < 10; i++) sum += parseInt(d[i]) * (11 - i); r = (sum * 10) % 11; if (r >= 10) r = 0; return r === parseInt(d[10]); } function fmtCurrency(n) { return 'R$ ' + Number(n).toFixed(2).replace('.', ','); } // ── Component ───────────────────────────────────────────────── function InscricaoScreen({ tournamentId }) { const [tournament, setTournament] = useInscState(null); const [loading, setLoading] = useInscState(true); const [phase, setPhase] = useInscState('form'); // 'form' | 'choice' | 'free' | 'pix' const [name, setName] = useInscState(''); const [nickname, setNickname] = useInscState(''); const [cpf, setCpf] = useInscState(''); const [phone, setPhone] = useInscState(''); const [submitting, setSubmitting] = useInscState(false); const [error, setError] = useInscState(''); const [copied, setCopied] = useInscState(false); useInscEffect(() => { fetch(`/api/tournaments/${tournamentId}/info`) .then(r => r.ok ? r.json() : Promise.reject()) .then(data => { setTournament(data); setLoading(false); }) .catch(() => setLoading(false)); }, []); async function handleSubmit(e) { e.preventDefault(); if (!validateCPF(cpf)) { setError('CPF inválido'); return; } if (phone.replace(/\D/g, '').length < 10) { setError('Telefone inválido'); return; } setSubmitting(true); setError(''); try { const res = await fetch(`/api/tournaments/${tournamentId}/inscricao`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name, nickname: nickname.trim() || undefined, cpf: cpf.replace(/\D/g, ''), phone: phone.replace(/\D/g, ''), }), }); const data = await res.json(); if (!res.ok) { setError(data.error || 'Erro ao se inscrever'); return; } if (tournament.freeEntry) { setPhase('pix'); return; } const offerFree = tournament.freeForSubscribers && tournament.entryFee > 0 && tournament.channels?.length > 0; setPhase(offerFree ? 'choice' : 'pix'); } catch { setError('Erro de conexão. Tente novamente.'); } finally { setSubmitting(false); } } function copyPix() { if (!tournament?.pixCode) return; navigator.clipboard.writeText(tournament.pixCode); setCopied(true); setTimeout(() => setCopied(false), 2000); } const cpfDigits = cpf.replace(/\D/g, ''); const phoneDigits = phone.replace(/\D/g, ''); const canSubmit = name.trim() && cpfDigits.length === 11 && phoneDigits.length >= 10 && !submitting; if (loading) { return (
); } if (!tournament) { return (
Campeonato não encontrado.
); } if (tournament.status === 'finished') { return (
{tournament.name}
{tournament.title &&
{tournament.title}
}
🏆
Campeonato encerrado
Este campeonato já foi finalizado. As inscrições estão encerradas.
{tournament.champion && (
Campeão {tournament.champion.nickname || tournament.champion.name}
)} Ver chaveamento →
); } if (tournament.status === 'running') { return (
{tournament.name}
{tournament.title &&
{tournament.title}
}
⚔️
Inscrições encerradas
O campeonato já começou. Não é mais possível se inscrever.
Acompanhar ao vivo →
); } const hasFee = tournament.entryFee > 0; const hasPix = !!tournament.pixCode; const hasContact = !!(tournament.contactPhone || (tournament.channels && tournament.channels.length > 0)); const CHANNEL_META = { twitch: { label: 'Twitch', emoji: '🟣' }, youtube: { label: 'YouTube', emoji: '🔴' }, instagram: { label: 'Instagram', emoji: '📸' }, tiktok: { label: 'TikTok', emoji: '🎵' }, discord: { label: 'Discord', emoji: '💬' }, twitter: { label: 'Twitter/X', emoji: '🐦' }, outro: { label: 'Link', emoji: '🔗' }, }; return (
{/* Header */}
{tournament.name}
{tournament.title &&
{tournament.title}
}
{hasFee && !tournament.freeEntry && (
{fmtCurrency(tournament.entryFee)}
)}
{hasContact && (
{(tournament.channels || []).map((ch, i) => { const meta = CHANNEL_META[ch.type] || CHANNEL_META.outro; const href = ch.url.startsWith('http') ? ch.url : `https://${ch.url}`; return ( {meta.emoji} {meta.label} ); })} {tournament.contactPhone && ( 💬 WhatsApp )}
)}
{phase === 'form' ? (
Inscrição
{tournament.bannerImage && ( Banner do campeonato )} {tournament.freeEntry ? (
Inscrição gratuita — sem cobrança de taxa.
) : hasFee && (
Valor da inscrição: {fmtCurrency(tournament.entryFee)} {hasPix ? ' — pagamento via PIX após o cadastro.' : '.'} {tournament.freeForSubscribers && tournament.channels?.length > 0 && (
Inscritos no canal participam gratuitamente.
)}
)}
setName(e.target.value)} placeholder="Seu nome completo" autoComplete="name" />
setNickname(e.target.value)} placeholder="Como quer ser chamado no chaveamento" />
setCpf(formatCPF(e.target.value))} placeholder="000.000.000-00" inputMode="numeric" />
setPhone(formatPhone(e.target.value))} placeholder="(00) 00000-0000" inputMode="numeric" />
{error &&
{error}
}
) : phase === 'choice' ? (
✓
Inscrição confirmada!
{name}
Como deseja participar?
Inscreva-se no canal e participe de graça, ou pague a taxa de inscrição.
) : phase === 'free' ? (
🎉
Inscrição gratuita!
{name}
Inscreva-se no canal para confirmar sua entrada gratuita. O organizador irá verificar.
{(tournament.channels || []).map((ch, i) => { const meta = CHANNEL_META[ch.type] || CHANNEL_META.outro; const href = ch.url.startsWith('http') ? ch.url : `https://${ch.url}`; return ( {meta.emoji} Inscrever no {meta.label} ); })}
{tournament.rules && ( )}
) : (
✓
Inscrição confirmada!
{name}
{!tournament.freeEntry && (hasFee || hasPix) && (
Pagamento via PIX
{hasFee && (
{fmtCurrency(tournament.entryFee)}
)} {hasPix ? ( <> QR Code PIX
Ou copie o código PIX:
{tournament.pixCode}
) : (
Aguarde as instruções de pagamento do organizador.
)}
)} {tournament.freeForSubscribers && tournament.channels?.length > 0 && ( )} {tournament.rules && ( )}
)}
); } window.InscricaoScreen = InscricaoScreen;