import { useState } from 'react'; import { button, control } from '@/pages/management/shared'; export default function OpportunityContactForm({ id, etag, organization, investors, disabled, onSaved, }: { id: number; etag: string; organization: string | null; investors: { id: string; legal_name: string }[]; disabled: boolean; onSaved: (contact: { id: string; full_name: string }) => void; }) { const [values, setValues] = useState({ full_name: '', email: '', phone: '', investor_id: '', investor_type: '', }); const [busy, setBusy] = useState(false); const [errors, setErrors] = useState>({}); const [message, setMessage] = useState(''); async function save() { if (busy || disabled) return; setBusy(true); setErrors({}); setMessage(''); try { const xsrf = document.cookie .split('; ') .find((item) => item.startsWith('XSRF-TOKEN=')); const response = await fetch( `/minhas-oportunidades/${id}/contacts`, { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', 'X-XSRF-TOKEN': xsrf ? decodeURIComponent(xsrf.slice(11)) : '', }, body: JSON.stringify({ ...values, investor_id: values.investor_id || null, investor_type: values.investor_type || null, etag, }), }, ); const result = await response.json(); setMessage( result.message ?? 'Não foi possível guardar o contacto. Os valores foram preservados.', ); if (!response.ok) { setErrors( Object.fromEntries( Object.entries(result.errors ?? {}).map( ([key, value]) => [ key, Array.isArray(value) ? value.join(' ') : String(value), ], ), ), ); return; } onSaved(result.contact); } catch { setMessage( 'Resposta indisponível. Recupere a oportunidade antes de repetir; os valores foram preservados.', ); } finally { setBusy(false); requestAnimationFrame(() => document.getElementById('contact-result')?.focus(), ); } } return (
Cadastrar contacto do promotor

Indique os dados de contacto autorizados pelo promotor desta oportunidade. O contacto fica guardado para reutilização e não é publicado.

Dados do contacto {(['full_name', 'email', 'phone'] as const).map((field) => (
setValues({ ...values, [field]: event.target.value, }) } aria-invalid={Boolean(errors[field])} aria-describedby={ errors[field] ? `contact-${field}-error` : undefined } /> {errors[field] && ( )}
))} {organization && investors.length > 1 && (
{errors.investor_id && ( )}
)} {organization && investors.length === 0 && (

A organização já cadastrada será ligada ao cadastro CRM existente, sem criar outra organização ou um pedido fictício.

{errors.investor_type && (

{errors.investor_type}

)}
)}

{message}

); }