@@ -20,6 +20,22 @@ const emptyParcel = (): Parcel => ({
|
||||
coordinates: '',
|
||||
geometry_confidence: 'unknown',
|
||||
});
|
||||
const requiredLabels: Record<string, string> = {
|
||||
support_kind: 'Pretende apoio para',
|
||||
request_description: 'Descrição do pedido',
|
||||
applicant_category: 'Categoria do requerente',
|
||||
full_name: 'Nome ou denominação',
|
||||
preferred_contact: 'Canal preferido',
|
||||
area_status: 'Situação da localização',
|
||||
purpose: 'Finalidade pretendida',
|
||||
email: 'Email',
|
||||
contact_person: 'Pessoa de contacto',
|
||||
representative_role: 'Função da representação',
|
||||
truth_declaration: 'Declaração de informação',
|
||||
contact_consent: 'Autorização de contacto',
|
||||
country: 'País do requerente',
|
||||
declared_origin_country_code: 'Origem declarada da solicitação',
|
||||
};
|
||||
const emptyPayload = (user: { name: string; email: string }): Payload => ({
|
||||
support_kind: '',
|
||||
request_description: '',
|
||||
@@ -98,6 +114,49 @@ export default function LandForm({
|
||||
[existing?.id],
|
||||
);
|
||||
const form = useForm({ version: existing?.version ?? 1, payload: initial });
|
||||
const [pendingErrorField, setPendingErrorField] = useState<string | null>(
|
||||
null,
|
||||
);
|
||||
useEffect(() => {
|
||||
const first = Object.keys(form.errors)[0];
|
||||
if (!first) return;
|
||||
const field = first.replace(/^payload\./, '').split('.')[0];
|
||||
const errorStep = [
|
||||
'truth_declaration',
|
||||
'contact_consent',
|
||||
'additional_information',
|
||||
].includes(field)
|
||||
? 3
|
||||
: ['exploration_plan', 'consultation'].includes(field)
|
||||
? 2
|
||||
: [
|
||||
'area_status',
|
||||
'parcels',
|
||||
'purpose',
|
||||
'activity',
|
||||
'known_occupants',
|
||||
'known_improvements',
|
||||
'prior_right_status',
|
||||
'conflict_status',
|
||||
'official_process',
|
||||
].includes(field)
|
||||
? 1
|
||||
: 0;
|
||||
setStep(errorStep);
|
||||
setPendingErrorField(field);
|
||||
}, [form.errors]);
|
||||
useEffect(() => {
|
||||
if (!pendingErrorField) return;
|
||||
const timer = window.setTimeout(() => {
|
||||
const target =
|
||||
document.getElementById(`land-field-${pendingErrorField}`) ??
|
||||
document.getElementById('land-error-summary');
|
||||
target?.focus();
|
||||
target?.scrollIntoView({ block: 'center' });
|
||||
setPendingErrorField(null);
|
||||
}, 0);
|
||||
return () => window.clearTimeout(timer);
|
||||
}, [pendingErrorField, step]);
|
||||
const update = (field: string, value: unknown) =>
|
||||
form.setData('payload', { ...form.data.payload, [field]: value });
|
||||
const updateParcel = (index: number, key: string, value: unknown) => {
|
||||
@@ -118,18 +177,28 @@ export default function LandForm({
|
||||
{required ? ' *' : ''}
|
||||
</span>
|
||||
<input
|
||||
id={`land-field-${key}`}
|
||||
className="w-full rounded border p-2"
|
||||
type={type}
|
||||
required={required}
|
||||
value={form.data.payload[key] ?? ''}
|
||||
onChange={(event) => update(key, event.target.value)}
|
||||
aria-invalid={Boolean(form.errors[`payload.${key}`])}
|
||||
aria-describedby={
|
||||
form.errors[`payload.${key}`]
|
||||
? `land-error-${key}`
|
||||
: undefined
|
||||
}
|
||||
/>
|
||||
{help && (
|
||||
<span className="block text-xs text-slate-600">{help}</span>
|
||||
)}
|
||||
{form.errors[`payload.${key}`] && (
|
||||
<span role="alert" className="text-sm text-red-700">
|
||||
<span
|
||||
id={`land-error-${key}`}
|
||||
role="alert"
|
||||
className="text-sm text-red-700"
|
||||
>
|
||||
{form.errors[`payload.${key}`]}
|
||||
</span>
|
||||
)}
|
||||
@@ -209,6 +278,15 @@ export default function LandForm({
|
||||
Pretende apoio para *
|
||||
</span>
|
||||
<select
|
||||
id="land-field-support_kind"
|
||||
aria-invalid={Boolean(
|
||||
form.errors['payload.support_kind'],
|
||||
)}
|
||||
aria-describedby={
|
||||
form.errors['payload.support_kind']
|
||||
? 'land-error-support_kind'
|
||||
: undefined
|
||||
}
|
||||
className="w-full rounded border p-2"
|
||||
required
|
||||
value={current.support_kind ?? ''}
|
||||
@@ -226,6 +304,15 @@ export default function LandForm({
|
||||
)}
|
||||
</select>
|
||||
</label>
|
||||
{form.errors['payload.support_kind'] && (
|
||||
<p
|
||||
id="land-error-support_kind"
|
||||
role="alert"
|
||||
className="text-red-700"
|
||||
>
|
||||
{form.errors['payload.support_kind']}
|
||||
</p>
|
||||
)}
|
||||
{text('request_description', 'Descrição do pedido', true)}
|
||||
{text(
|
||||
'project_reference',
|
||||
@@ -238,6 +325,15 @@ export default function LandForm({
|
||||
<select
|
||||
className="w-full rounded border p-2"
|
||||
required
|
||||
id="land-field-applicant_category"
|
||||
aria-invalid={Boolean(
|
||||
form.errors['payload.applicant_category'],
|
||||
)}
|
||||
aria-describedby={
|
||||
form.errors['payload.applicant_category']
|
||||
? 'land-error-applicant_category'
|
||||
: undefined
|
||||
}
|
||||
value={current.applicant_category ?? ''}
|
||||
onChange={(event) =>
|
||||
update('applicant_category', event.target.value)
|
||||
@@ -250,6 +346,15 @@ export default function LandForm({
|
||||
<option value="other">Outra</option>
|
||||
</select>
|
||||
</label>
|
||||
{form.errors['payload.applicant_category'] && (
|
||||
<p
|
||||
id="land-error-applicant_category"
|
||||
role="alert"
|
||||
className="text-red-700"
|
||||
>
|
||||
{form.errors['payload.applicant_category']}
|
||||
</p>
|
||||
)}
|
||||
{text('full_name', 'Nome ou denominação', true)}
|
||||
{category === 'community' &&
|
||||
text(
|
||||
@@ -266,6 +371,15 @@ export default function LandForm({
|
||||
className="w-full rounded border p-2"
|
||||
required
|
||||
value={current.preferred_contact ?? ''}
|
||||
id="land-field-preferred_contact"
|
||||
aria-invalid={Boolean(
|
||||
form.errors['payload.preferred_contact'],
|
||||
)}
|
||||
aria-describedby={
|
||||
form.errors['payload.preferred_contact']
|
||||
? 'land-error-preferred_contact'
|
||||
: undefined
|
||||
}
|
||||
onChange={(event) =>
|
||||
update('preferred_contact', event.target.value)
|
||||
}
|
||||
@@ -276,32 +390,46 @@ export default function LandForm({
|
||||
<option value="any">Qualquer um indicado</option>
|
||||
</select>
|
||||
</label>
|
||||
<CountrySelect
|
||||
countries={countries}
|
||||
value={current.country ?? ''}
|
||||
onChange={(value) => update('country', value)}
|
||||
label="País, se aplicável"
|
||||
allowLegacyValue
|
||||
/>
|
||||
<CountrySelect
|
||||
countries={countries}
|
||||
value={current.declared_origin_country_code ?? ''}
|
||||
onChange={(value) =>
|
||||
update('declared_origin_country_code', value)
|
||||
}
|
||||
label="País de origem declarado da solicitação (opcional)"
|
||||
/>
|
||||
{(form.errors['payload.country'] ||
|
||||
form.errors[
|
||||
'payload.declared_origin_country_code'
|
||||
]) && (
|
||||
<p role="alert">
|
||||
{form.errors['payload.country'] ??
|
||||
form.errors[
|
||||
'payload.declared_origin_country_code'
|
||||
]}
|
||||
{form.errors['payload.preferred_contact'] && (
|
||||
<p
|
||||
id="land-error-preferred_contact"
|
||||
role="alert"
|
||||
className="text-red-700"
|
||||
>
|
||||
{form.errors['payload.preferred_contact']}
|
||||
</p>
|
||||
)}
|
||||
<div className="space-y-1">
|
||||
<p className="text-sm font-medium">
|
||||
País do requerente (opcional)
|
||||
</p>
|
||||
<CountrySelect
|
||||
countries={countries}
|
||||
value={current.country ?? ''}
|
||||
onChange={(value) => update('country', value)}
|
||||
label="País do requerente (opcional)"
|
||||
error={form.errors['payload.country']}
|
||||
allowLegacyValue
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
<p className="text-sm font-medium">
|
||||
País de origem declarado da solicitação (opcional)
|
||||
</p>
|
||||
<CountrySelect
|
||||
countries={countries}
|
||||
value={current.declared_origin_country_code ?? ''}
|
||||
onChange={(value) =>
|
||||
update('declared_origin_country_code', value)
|
||||
}
|
||||
label="País de origem declarado da solicitação (opcional)"
|
||||
error={
|
||||
form.errors[
|
||||
'payload.declared_origin_country_code'
|
||||
]
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
{text('nationality', 'Nacionalidade, se aplicável')}
|
||||
{category === 'entity' && (
|
||||
<>
|
||||
@@ -339,6 +467,15 @@ export default function LandForm({
|
||||
className="w-full rounded border p-2"
|
||||
required
|
||||
value={current.area_status ?? ''}
|
||||
id="land-field-area_status"
|
||||
aria-invalid={Boolean(
|
||||
form.errors['payload.area_status'],
|
||||
)}
|
||||
aria-describedby={
|
||||
form.errors['payload.area_status']
|
||||
? 'land-error-area_status'
|
||||
: undefined
|
||||
}
|
||||
onChange={(event) =>
|
||||
update('area_status', event.target.value)
|
||||
}
|
||||
@@ -356,6 +493,15 @@ export default function LandForm({
|
||||
<option value="unknown">Ainda não sei</option>
|
||||
</select>
|
||||
</label>
|
||||
{form.errors['payload.area_status'] && (
|
||||
<p
|
||||
id="land-error-area_status"
|
||||
role="alert"
|
||||
className="text-red-700"
|
||||
>
|
||||
{form.errors['payload.area_status']}
|
||||
</p>
|
||||
)}
|
||||
{current.area_status !== 'seeking' &&
|
||||
current.area_status !== 'unknown' &&
|
||||
(current.parcels ?? []).map(
|
||||
@@ -953,6 +1099,15 @@ export default function LandForm({
|
||||
<label className="flex gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
id="land-field-truth_declaration"
|
||||
aria-invalid={Boolean(
|
||||
form.errors['payload.truth_declaration'],
|
||||
)}
|
||||
aria-describedby={
|
||||
form.errors['payload.truth_declaration']
|
||||
? 'land-error-truth_declaration'
|
||||
: undefined
|
||||
}
|
||||
checked={Boolean(current.truth_declaration)}
|
||||
onChange={(e) =>
|
||||
update('truth_declaration', e.target.checked)
|
||||
@@ -961,9 +1116,27 @@ export default function LandForm({
|
||||
Confirmo que as respostas correspondem à informação de
|
||||
que disponho e assinalei dúvidas conhecidas.
|
||||
</label>
|
||||
{form.errors['payload.truth_declaration'] && (
|
||||
<p
|
||||
id="land-error-truth_declaration"
|
||||
role="alert"
|
||||
className="text-red-700"
|
||||
>
|
||||
{form.errors['payload.truth_declaration']}
|
||||
</p>
|
||||
)}
|
||||
<label className="flex gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
id="land-field-contact_consent"
|
||||
aria-invalid={Boolean(
|
||||
form.errors['payload.contact_consent'],
|
||||
)}
|
||||
aria-describedby={
|
||||
form.errors['payload.contact_consent']
|
||||
? 'land-error-contact_consent'
|
||||
: undefined
|
||||
}
|
||||
checked={Boolean(current.contact_consent)}
|
||||
onChange={(e) =>
|
||||
update('contact_consent', e.target.checked)
|
||||
@@ -972,6 +1145,15 @@ export default function LandForm({
|
||||
Autorizo o contacto para responder a este pedido. Esta
|
||||
autorização não permite publicação dos documentos.
|
||||
</label>
|
||||
{form.errors['payload.contact_consent'] && (
|
||||
<p
|
||||
id="land-error-contact_consent"
|
||||
role="alert"
|
||||
className="text-red-700"
|
||||
>
|
||||
{form.errors['payload.contact_consent']}
|
||||
</p>
|
||||
)}
|
||||
<p className="rounded bg-amber-50 p-3 text-sm">
|
||||
Ainda não carregue actas, documentos de identidade ou
|
||||
informações sensíveis que não sejam necessárias à
|
||||
@@ -1033,11 +1215,21 @@ export default function LandForm({
|
||||
</div>
|
||||
</footer>
|
||||
{Object.keys(form.errors).length > 0 && (
|
||||
<div role="alert" className="rounded border border-red-500 p-3">
|
||||
<div
|
||||
id="land-error-summary"
|
||||
tabIndex={-1}
|
||||
role="alert"
|
||||
className="rounded border border-red-500 p-3"
|
||||
>
|
||||
<p>Revise os campos assinalados antes de continuar.</p>
|
||||
<ul>
|
||||
{Object.entries(form.errors).map(([key, value]) => (
|
||||
<li key={key}>{String(value)}</li>
|
||||
<li key={key}>
|
||||
{requiredLabels[
|
||||
key.replace(/^payload\./, '')
|
||||
] ?? 'Campo assinalado'}
|
||||
: {String(value)}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user