@@ -54,10 +54,25 @@ export default function BusinessInterest({
|
||||
});
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const [fieldErrors, setFieldErrors] = useState<Partial<Record<keyof Form, string>>>({});
|
||||
const [receipt, setReceipt] = useState('');
|
||||
const key = useRef(newSubmissionKey());
|
||||
const update = <K extends keyof Form>(field: K, value: Form[K]) =>
|
||||
const update = <K extends keyof Form>(field: K, value: Form[K]) => {
|
||||
setForm((current) => ({ ...current, [field]: value }));
|
||||
setFieldErrors((current) => ({ ...current, [field]: undefined }));
|
||||
};
|
||||
const fieldId = (field: keyof Form) => `business-interest-${field}`;
|
||||
const feedback = (field: keyof Form) => ({
|
||||
id: fieldId(field),
|
||||
name: field,
|
||||
'aria-invalid': !!fieldErrors[field],
|
||||
'aria-describedby': fieldErrors[field] ? `${fieldId(field)}-error` : undefined,
|
||||
});
|
||||
const fieldError = (field: keyof Form) => fieldErrors[field] ? (
|
||||
<p id={`${fieldId(field)}-error`} role="alert" className="text-sm text-red-700">
|
||||
{fieldErrors[field]}
|
||||
</p>
|
||||
) : null;
|
||||
const submit = async (event: FormEvent<HTMLFormElement>) => {
|
||||
event.preventDefault();
|
||||
if (!form.privacyConsent) {
|
||||
@@ -88,10 +103,20 @@ export default function BusinessInterest({
|
||||
}),
|
||||
});
|
||||
const data = await response.json();
|
||||
if (!response.ok)
|
||||
throw new Error(
|
||||
data.message ?? 'Não foi possível enviar a manifestação.',
|
||||
);
|
||||
if (!response.ok) {
|
||||
const errors: Partial<Record<keyof Form, string>> = {};
|
||||
for (const [field, messages] of Object.entries(data.errors ?? {})) {
|
||||
if (field in form && Array.isArray(messages)) {
|
||||
errors[field as keyof Form] = messages.filter((message): message is string => typeof message === 'string').join(' ');
|
||||
}
|
||||
}
|
||||
setFieldErrors(errors);
|
||||
setError(data.message ?? 'Não foi possível enviar a manifestação.');
|
||||
const first = Object.keys(errors)[0] as keyof Form | undefined;
|
||||
window.setTimeout(() => document.getElementById(first ? fieldId(first) : 'business-interest-feedback')?.focus(), 0);
|
||||
return;
|
||||
}
|
||||
setFieldErrors({});
|
||||
setReceipt(data.data.reference);
|
||||
key.current = newSubmissionKey();
|
||||
} catch (cause) {
|
||||
@@ -123,9 +148,11 @@ export default function BusinessInterest({
|
||||
</section>
|
||||
) : (
|
||||
<form className={`${card} space-y-4`} onSubmit={submit}>
|
||||
<Field label="País de origem declarado desta solicitação">
|
||||
<div className="space-y-1 text-sm font-medium"><span>País de origem declarado desta solicitação</span>
|
||||
<CountrySelect
|
||||
countries={countries}
|
||||
id={fieldId('declared_origin_country_code')}
|
||||
error={fieldErrors.declared_origin_country_code}
|
||||
value={form.declared_origin_country_code}
|
||||
label="País de origem declarado desta solicitação"
|
||||
onChange={(value) =>
|
||||
@@ -136,16 +163,18 @@ export default function BusinessInterest({
|
||||
Origem opcional e independente do país do
|
||||
investidor.
|
||||
</p>
|
||||
</Field>
|
||||
</div>
|
||||
<Field label="Nome ou denominação">
|
||||
<input
|
||||
className={input}
|
||||
required
|
||||
minLength={3}
|
||||
maxLength={150}
|
||||
{...feedback('fullName')}
|
||||
value={form.fullName}
|
||||
onChange={(e) => update('fullName', e.target.value)}
|
||||
/>
|
||||
{fieldError('fullName')}
|
||||
</Field>
|
||||
<Field label="Email">
|
||||
<input
|
||||
@@ -153,22 +182,27 @@ export default function BusinessInterest({
|
||||
type="email"
|
||||
required
|
||||
maxLength={150}
|
||||
{...feedback('email')}
|
||||
value={form.email}
|
||||
onChange={(e) => update('email', e.target.value)}
|
||||
/>
|
||||
{fieldError('email')}
|
||||
</Field>
|
||||
<Field label="Telefone">
|
||||
<input
|
||||
className={input}
|
||||
type="tel"
|
||||
maxLength={30}
|
||||
{...feedback('phone')}
|
||||
value={form.phone}
|
||||
onChange={(e) => update('phone', e.target.value)}
|
||||
/>
|
||||
{fieldError('phone')}
|
||||
</Field>
|
||||
<Field label="Tipo de investidor">
|
||||
<select
|
||||
className={input}
|
||||
{...feedback('investorType')}
|
||||
value={form.investorType}
|
||||
onChange={(e) =>
|
||||
update('investorType', e.target.value)
|
||||
@@ -182,32 +216,38 @@ export default function BusinessInterest({
|
||||
</option>
|
||||
<option value="outro">Outro</option>
|
||||
</select>
|
||||
{fieldError('investorType')}
|
||||
</Field>
|
||||
<Field label="País">
|
||||
<div className="space-y-1 text-sm font-medium"><span>País</span>
|
||||
<CountrySelect
|
||||
countries={countries}
|
||||
required
|
||||
label="País do requerente"
|
||||
allowLegacyValue
|
||||
id={fieldId('country')}
|
||||
error={fieldErrors.country}
|
||||
value={form.country}
|
||||
onChange={(value) => update('country', value)}
|
||||
/>
|
||||
</Field>
|
||||
</div>
|
||||
<Field label="Sector de interesse">
|
||||
<input
|
||||
className={input}
|
||||
required
|
||||
maxLength={150}
|
||||
{...feedback('sectorInterest')}
|
||||
value={form.sectorInterest}
|
||||
onChange={(e) =>
|
||||
update('sectorInterest', e.target.value)
|
||||
}
|
||||
/>
|
||||
{fieldError('sectorInterest')}
|
||||
</Field>
|
||||
<Field label="Modalidade pretendida">
|
||||
<select
|
||||
className={input}
|
||||
required
|
||||
{...feedback('modality')}
|
||||
value={form.modality}
|
||||
onChange={(e) => update('modality', e.target.value)}
|
||||
>
|
||||
@@ -217,16 +257,19 @@ export default function BusinessInterest({
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
{fieldError('modality')}
|
||||
</Field>
|
||||
<Field label="Intervalo de investimento">
|
||||
<input
|
||||
className={input}
|
||||
maxLength={80}
|
||||
{...feedback('investmentRange')}
|
||||
value={form.investmentRange}
|
||||
onChange={(e) =>
|
||||
update('investmentRange', e.target.value)
|
||||
}
|
||||
/>
|
||||
{fieldError('investmentRange')}
|
||||
</Field>
|
||||
<Field label="Motivação e pedido">
|
||||
<textarea
|
||||
@@ -235,14 +278,17 @@ export default function BusinessInterest({
|
||||
minLength={20}
|
||||
maxLength={2000}
|
||||
rows={5}
|
||||
{...feedback('message')}
|
||||
value={form.message}
|
||||
onChange={(e) => update('message', e.target.value)}
|
||||
/>
|
||||
{fieldError('message')}
|
||||
</Field>
|
||||
<label className="flex items-start gap-3">
|
||||
<input
|
||||
type="checkbox"
|
||||
required
|
||||
{...feedback('privacyConsent')}
|
||||
checked={form.privacyConsent}
|
||||
onChange={(e) =>
|
||||
update('privacyConsent', e.target.checked)
|
||||
@@ -271,7 +317,8 @@ export default function BusinessInterest({
|
||||
equipa.
|
||||
</span>
|
||||
</label>
|
||||
{error && <p role="alert">{error}</p>}
|
||||
{fieldError('privacyConsent')}
|
||||
{error && <p id="business-interest-feedback" tabIndex={-1} role="alert">{error}</p>}
|
||||
<button className={button} disabled={busy}>
|
||||
{busy ? 'A enviar…' : 'Enviar manifestação'}
|
||||
</button>
|
||||
|
||||
Reference in New Issue
Block a user