fix: complete InvesteMoz validation and error corrections
This commit is contained in:
@@ -53,10 +53,12 @@ export default function BusinessInterest({
|
||||
privacyConsent: false,
|
||||
shareWithProponent: false,
|
||||
});
|
||||
const {form, setForm} = draft;
|
||||
const { form, setForm } = draft;
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const [fieldErrors, setFieldErrors] = useState<Partial<Record<keyof Form, string>>>({});
|
||||
const [fieldErrors, setFieldErrors] = useState<
|
||||
Partial<Record<keyof Form, string>>
|
||||
>({});
|
||||
const [receipt, setReceipt] = useState('');
|
||||
const key = useRef(draft.key);
|
||||
const update = <K extends keyof Form>(field: K, value: Form[K]) => {
|
||||
@@ -68,13 +70,20 @@ export default function BusinessInterest({
|
||||
id: fieldId(field),
|
||||
name: field,
|
||||
'aria-invalid': !!fieldErrors[field],
|
||||
'aria-describedby': fieldErrors[field] ? `${fieldId(field)}-error` : undefined,
|
||||
'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 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) {
|
||||
@@ -104,19 +113,41 @@ export default function BusinessInterest({
|
||||
existingBusinessId: record.id,
|
||||
}),
|
||||
});
|
||||
if (response.status === 401) { draft.authenticate(); return; }
|
||||
if (response.status === 401) {
|
||||
draft.authenticate();
|
||||
return;
|
||||
}
|
||||
const data = await response.json();
|
||||
if (!response.ok) {
|
||||
const errors: Partial<Record<keyof Form, string>> = {};
|
||||
for (const [field, messages] of Object.entries(data.errors ?? {})) {
|
||||
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(' ');
|
||||
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.');
|
||||
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);
|
||||
window.setTimeout(
|
||||
() =>
|
||||
document
|
||||
.getElementById(
|
||||
first
|
||||
? fieldId(first)
|
||||
: 'business-interest-feedback',
|
||||
)
|
||||
?.focus(),
|
||||
0,
|
||||
);
|
||||
return;
|
||||
}
|
||||
setFieldErrors({});
|
||||
@@ -144,7 +175,13 @@ export default function BusinessInterest({
|
||||
{receipt ? (
|
||||
<section className={card} role="status">
|
||||
<h2 className="text-xl font-bold">Pedido recebido</h2>
|
||||
<p>Referência: {receipt}</p><Link href="/investidor/manifestacoes" className="underline">Acompanhar manifestações</Link>
|
||||
<p>Referência: {receipt}</p>
|
||||
<Link
|
||||
href="/investidor/manifestacoes"
|
||||
className="underline"
|
||||
>
|
||||
Acompanhar manifestações
|
||||
</Link>
|
||||
<p>
|
||||
A equipa fará a triagem antes de qualquer partilha com o
|
||||
proponente.
|
||||
@@ -152,7 +189,8 @@ export default function BusinessInterest({
|
||||
</section>
|
||||
) : (
|
||||
<form className={`${card} space-y-4`} onSubmit={submit}>
|
||||
<div className="space-y-1 text-sm font-medium"><span>País de origem declarado desta solicitação</span>
|
||||
<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')}
|
||||
@@ -222,7 +260,8 @@ export default function BusinessInterest({
|
||||
</select>
|
||||
{fieldError('investorType')}
|
||||
</Field>
|
||||
<div className="space-y-1 text-sm font-medium"><span>País</span>
|
||||
<div className="space-y-1 text-sm font-medium">
|
||||
<span>País</span>
|
||||
<CountrySelect
|
||||
countries={countries}
|
||||
required
|
||||
@@ -322,7 +361,15 @@ export default function BusinessInterest({
|
||||
</span>
|
||||
</label>
|
||||
{fieldError('privacyConsent')}
|
||||
{error && <p id="business-interest-feedback" tabIndex={-1} role="alert">{error}</p>}
|
||||
{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