import { useState } from 'react' interface DeletedChar { guid: number name: string level: number } interface Props { url: string csrfToken: string characters: DeletedChar[] } export function RestoreCharacterForm({ url, csrfToken, characters }: Props) { const [guid, setGuid] = useState('') const [busy, setBusy] = useState(false) const [done, setDone] = useState(false) const [message, setMessage] = useState<{ ok: boolean; html: string } | null>(null) async function handleSubmit(e: React.FormEvent) { e.preventDefault() if (busy || done || !guid) return setBusy(true) setMessage(null) const body = new URLSearchParams() body.set('guid', guid) body.set('csrfmiddlewaretoken', csrfToken) try { const resp = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'X-CSRFToken': csrfToken, Accept: 'application/json', }, credentials: 'same-origin', body: body.toString(), }) if (!resp.ok) throw new Error(`HTTP ${resp.status}`) const data: { success?: boolean; message?: string; redirect?: string } = await resp.json() setMessage({ ok: !!data.success, html: data.message || '' }) if (data.success && data.redirect) { setDone(true) setTimeout(() => { window.location.href = data.redirect as string }, 2000) } else { setBusy(false) } } catch { setMessage({ ok: false, html: 'Error en el servidor. Inténtalo más tarde.' }) setBusy(false) } } return ( <>

{message && }
) }