// Registrations management screen — admin
const { useState: useRegState, useEffect: useRegEffect } = React;
const STATUS_OPTS = [
{ value: 'pending', label: 'Pendente' },
{ value: 'approved', label: 'Aprovado' },
{ value: 'paid', label: 'Pago' },
{ value: 'rejected', label: 'Rejeitado' },
];
function fmtCPF(d) { return d.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, '$1.$2.$3-$4'); }
function fmtPhone(d) {
return d.length === 11
? d.replace(/(\d{2})(\d{5})(\d{4})/, '($1) $2-$3')
: d.replace(/(\d{2})(\d{4})(\d{4})/, '($1) $2-$3');
}
function fmtCPFInput(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 fmtPhoneInput(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');
}
// ── Edit Modal ─────────────────────────────────────────────────
function EditModal({ reg, tournamentId, onSave, onClose }) {
const [name, setName] = useRegState(reg?.name || '');
const [nickname, setNickname] = useRegState(reg?.nickname || '');
const [cpf, setCpf] = useRegState(reg ? fmtCPF(reg.cpf) : '');
const [phone, setPhone] = useRegState(reg ? fmtPhone(reg.phone) : '');
const [status, setStatus] = useRegState(reg?.status || 'pending');
const [saving, setSaving] = useRegState(false);
const [error, setError] = useRegState('');
const isNew = !reg;
async function handleSave() {
setSaving(true);
setError('');
try {
const url = isNew
? `/api/tournaments/${tournamentId}/inscricao`
: `/api/tournaments/${tournamentId}/inscricoes/${reg.id}`;
const method = isNew ? 'POST' : 'PUT';
const res = await fetch(url, {
method,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name,
nickname: nickname.trim() || undefined,
cpf: cpf.replace(/\D/g, ''),
phone: phone.replace(/\D/g, ''),
status,
}),
});
const data = await res.json();
if (!res.ok) { setError(data.error || 'Erro ao salvar'); return; }
onSave();
} catch { setError('Erro de conexão'); }
finally { setSaving(false); }
}
return (
e.target === e.currentTarget && onClose()}>
{isNew ? 'Adicionar inscrito' : 'Editar inscrito'}
setName(e.target.value)} placeholder="Nome completo" />
setNickname(e.target.value)} placeholder="Apelido no chaveamento" />
{STATUS_OPTS.map(o => (
))}
{error &&
{error}
}
);
}
// ── Main screen ────────────────────────────────────────────────
function RegistrationsScreen({ tournamentId }) {
const [tournament, setTournament] = useRegState(null);
const [regs, setRegs] = useRegState([]);
const [loading, setLoading] = useRegState(true);
const [filter, setFilter] = useRegState('');
const [editReg, setEditReg] = useRegState(undefined); // undefined = closed, null = new, obj = edit
const [deletingId, setDeletingId] = useRegState(null);
const [confirmDelete, setConfirmDelete] = useRegState(null);
useRegEffect(() => { load(); }, []);
async function load() {
setLoading(true);
try {
const [tRes, rRes] = await Promise.all([
fetch(`/api/tournaments/${tournamentId}`),
fetch(`/api/tournaments/${tournamentId}/inscricoes`),
]);
if (tRes.status === 401 || rRes.status === 401) { window.location.href = '/admin/login'; return; }
const [tData, rData] = await Promise.all([tRes.json(), rRes.json()]);
setTournament(tData);
setRegs(rData);
} catch {} finally { setLoading(false); }
}
async function handleDelete(reg) {
setDeletingId(reg.id);
try {
await fetch(`/api/tournaments/${tournamentId}/inscricoes/${reg.id}`, { method: 'DELETE' });
setRegs(prev => prev.filter(r => r.id !== reg.id));
} catch {} finally {
setDeletingId(null);
setConfirmDelete(null);
}
}
const filtered = regs.filter(r => {
if (!filter) return true;
const q = filter.toLowerCase();
return r.name.toLowerCase().includes(q)
|| (r.nickname || '').toLowerCase().includes(q)
|| r.cpf.includes(filter.replace(/\D/g, ''))
|| r.phone.includes(filter.replace(/\D/g, ''));
});
const counts = STATUS_OPTS.reduce((acc, o) => {
acc[o.value] = regs.filter(r => r.status === o.value).length;
return acc;
}, {});
if (loading) return
;
return (
{/* Topbar */}
{/* Stats */}
{regs.length}
Total
{STATUS_OPTS.map(o => counts[o.value] > 0 && (
{counts[o.value]}
{o.label}
))}
{/* Search */}
setFilter(e.target.value)}
/>
{/* List */}
{filtered.length === 0 && (
{filter ? 'Nenhum resultado para essa busca.' : 'Nenhuma inscrição ainda.'}
)}
{filtered.map((reg, idx) => (
{String(idx + 1).padStart(2, '0')}
{reg.nickname
? <>{reg.nickname}{reg.name}>
: {reg.name}}
{fmtCPF(reg.cpf)}
·
{fmtPhone(reg.phone)}
{STATUS_OPTS.find(o => o.value === reg.status)?.label}
{confirmDelete === reg.id ? (
<>
>
) : (
)}
))}
{/* Edit / Add modal */}
{editReg !== undefined && (
{ setEditReg(undefined); load(); }}
onClose={() => setEditReg(undefined)}
/>
)}
);
}
window.RegistrationsScreen = RegistrationsScreen;