'use client' import { useState } from 'react' import { useTranslations } from 'next-intl' const ERROR_KEYS = ['noCharacter', 'notOfflineOrOwned', 'cooldown', 'soapError'] as const interface Props { characters: string[] endpoint: string actionLabel: string } /** Formulario reutilizable para acciones de personaje por SOAP (revive, unstuck...). */ export function CharacterActionForm({ characters, endpoint, actionLabel }: Props) { const t = useTranslations('Services') const [character, setCharacter] = useState('') const [busy, setBusy] = useState(false) const [message, setMessage] = useState<{ ok: boolean; text: string } | null>(null) async function handleSubmit(e: React.FormEvent) { e.preventDefault() if (busy || !character) return setBusy(true) setMessage(null) try { const res = await fetch(endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'same-origin', body: JSON.stringify({ character }), }) const data: { success?: boolean; error?: string } = await res.json() if (data.success) setMessage({ ok: true, text: t('success') }) else { const key = (ERROR_KEYS as readonly string[]).includes(data.error ?? '') ? data.error! : 'genericError' setMessage({ ok: false, text: t(key) }) } } catch { setMessage({ ok: false, text: t('genericError') }) } finally { setBusy(false) } } if (characters.length === 0) { return
{t('noCharactersYet')}
} return ({message.text}
}