@@ -239,8 +239,12 @@ export default function BusinessWizard({
|
||||
const [sectorId, setSectorId] = useState(record.sector_id ?? null);
|
||||
const [sectorSearch, setSectorSearch] = useState('');
|
||||
const [publicTitle, setPublicTitle] = useState(record.public_title ?? '');
|
||||
const [territoryPaths, setTerritoryPaths] = useState(record.territory_paths ?? []);
|
||||
const [resolvedLocationJson, setResolvedLocationJson] = useState(record.payload.locations_json ?? '');
|
||||
const [territoryPaths, setTerritoryPaths] = useState(
|
||||
record.territory_paths ?? [],
|
||||
);
|
||||
const [resolvedLocationJson, setResolvedLocationJson] = useState(
|
||||
record.payload.locations_json ?? '',
|
||||
);
|
||||
const [version, setVersion] = useState(record.version_no);
|
||||
const versionRef = useRef(record.version_no);
|
||||
const saved = useRef(
|
||||
@@ -358,7 +362,9 @@ export default function BusinessWizard({
|
||||
next.locations_json = encodeBusinessLocations(rows);
|
||||
}
|
||||
if (key === 'geography_scope' && value === 'specific') {
|
||||
const first = decodeBusinessLocations(current.locations_json)[0];
|
||||
const first = decodeBusinessLocations(
|
||||
current.locations_json,
|
||||
)[0];
|
||||
if (first?.province_id) {
|
||||
next.province_id = first.province_id;
|
||||
next.district_id = first.district_id;
|
||||
@@ -396,23 +402,33 @@ export default function BusinessWizard({
|
||||
});
|
||||
};
|
||||
const locationRows = decodeBusinessLocations(payload.locations_json);
|
||||
const saveLocationRows = (rows: ReturnType<typeof decodeBusinessLocations>) => {
|
||||
const saveLocationRows = (
|
||||
rows: ReturnType<typeof decodeBusinessLocations>,
|
||||
) => {
|
||||
update('locations_json', encodeBusinessLocations(rows));
|
||||
};
|
||||
const setLocation = (
|
||||
index: number,
|
||||
key: keyof ReturnType<typeof emptyBusinessLocation>,
|
||||
value: string,
|
||||
) => saveLocationRows(updateBusinessLocation(locationRows, index, key, value));
|
||||
) =>
|
||||
saveLocationRows(
|
||||
updateBusinessLocation(locationRows, index, key, value),
|
||||
);
|
||||
const addLocation = () => {
|
||||
if (locationRows.length >= 20) return;
|
||||
saveLocationRows([...locationRows, emptyBusinessLocation()]);
|
||||
};
|
||||
const removeLocation = (index: number) => {
|
||||
const remaining = locationRows.filter((_, rowIndex) => rowIndex !== index);
|
||||
saveLocationRows(remaining.length ? remaining : [emptyBusinessLocation()]);
|
||||
const remaining = locationRows.filter(
|
||||
(_, rowIndex) => rowIndex !== index,
|
||||
);
|
||||
saveLocationRows(
|
||||
remaining.length ? remaining : [emptyBusinessLocation()],
|
||||
);
|
||||
};
|
||||
const duplicateLocationPath = hasDuplicateBusinessLocationPath(locationRows);
|
||||
const duplicateLocationPath =
|
||||
hasDuplicateBusinessLocationPath(locationRows);
|
||||
const fundingNeedsReconciliation = fundingReconciliationWarning(payload);
|
||||
const submit = async () => {
|
||||
if (
|
||||
@@ -635,8 +651,14 @@ export default function BusinessWizard({
|
||||
field.step === step &&
|
||||
key !== 'locations_json' &&
|
||||
!(
|
||||
payload.geography_scope === 'multiple' &&
|
||||
['province_id', 'district_id', 'admin_post_id', 'locality_id'].includes(key)
|
||||
payload.geography_scope ===
|
||||
'multiple' &&
|
||||
[
|
||||
'province_id',
|
||||
'district_id',
|
||||
'admin_post_id',
|
||||
'locality_id',
|
||||
].includes(key)
|
||||
) &&
|
||||
Object.entries(field.when ?? {}).every(
|
||||
([dependency, values]) =>
|
||||
@@ -758,61 +780,146 @@ export default function BusinessWizard({
|
||||
)}
|
||||
</Field>
|
||||
))}
|
||||
{step === 5 && payload.geography_scope === 'multiple' && (
|
||||
<section className="space-y-4" aria-labelledby="multiple-locations-heading">
|
||||
<h3 id="multiple-locations-heading" className="font-medium">
|
||||
{t('Localizações administrativas (várias) *', 'Administrative locations (multiple) *')}
|
||||
</h3>
|
||||
<p className="text-sm">
|
||||
{t('Indique pelo menos duas localizações distintas. Não são recolhidas coordenadas neste formulário.', 'Enter at least two distinct locations. This form does not collect coordinates.')}
|
||||
</p>
|
||||
{locationRows.map((row, index) => (
|
||||
<fieldset key={index} className="space-y-3 rounded border p-4">
|
||||
<legend className="px-1 font-medium">
|
||||
{t('Localização', 'Location')} {index + 1}
|
||||
</legend>
|
||||
{(['province_id', 'district_id', 'admin_post_id', 'locality_id'] as const).map((key) => {
|
||||
const configuration = territoryField[key];
|
||||
const parentId = configuration.parent
|
||||
? row[configuration.parent as keyof typeof row]
|
||||
: '';
|
||||
const name = t(
|
||||
key === 'province_id' ? 'Província ou Cidade de Maputo' : key === 'district_id' ? 'Distrito' : key === 'admin_post_id' ? 'Posto administrativo' : 'Localidade',
|
||||
key === 'province_id' ? 'Province or Maputo City' : key === 'district_id' ? 'District' : key === 'admin_post_id' ? 'Administrative post' : 'Locality',
|
||||
);
|
||||
return (
|
||||
<Field key={key} label={`${name}${key === 'province_id' ? ' *' : ''}`}>
|
||||
<TerritoryField
|
||||
name={`${name}, ${t('localização', 'location')} ${index + 1}`}
|
||||
type={configuration.type}
|
||||
parentId={parentId}
|
||||
value={row[key]}
|
||||
required={key === 'province_id'}
|
||||
onChange={(value) => setLocation(index, key, value)}
|
||||
/>
|
||||
</Field>
|
||||
);
|
||||
})}
|
||||
<button type="button" className="underline" disabled={locationRows.length <= 1} onClick={() => removeLocation(index)}>
|
||||
{t('Remover esta localização', 'Remove this location')}
|
||||
</button>
|
||||
</fieldset>
|
||||
))}
|
||||
<button type="button" className={button} disabled={locationRows.length >= 20} onClick={addLocation}>
|
||||
{t('Adicionar localização', 'Add location')}
|
||||
</button>
|
||||
{locationRows.length < 2 && (
|
||||
<p role="status">
|
||||
{t('Adicione pelo menos mais uma localização antes de submeter.', 'Add at least one more location before submitting.')}
|
||||
{step === 5 &&
|
||||
payload.geography_scope === 'multiple' && (
|
||||
<section
|
||||
className="space-y-4"
|
||||
aria-labelledby="multiple-locations-heading"
|
||||
>
|
||||
<h3
|
||||
id="multiple-locations-heading"
|
||||
className="font-medium"
|
||||
>
|
||||
{t(
|
||||
'Localizações administrativas (várias) *',
|
||||
'Administrative locations (multiple) *',
|
||||
)}
|
||||
</h3>
|
||||
<p className="text-sm">
|
||||
{t(
|
||||
'Indique pelo menos duas localizações distintas. Não são recolhidas coordenadas neste formulário.',
|
||||
'Enter at least two distinct locations. This form does not collect coordinates.',
|
||||
)}
|
||||
</p>
|
||||
)}
|
||||
{duplicateLocationPath && (
|
||||
<p role="alert" className="text-red-700">
|
||||
{t('Remova percursos administrativos repetidos ou sobrepostos.', 'Remove repeated or overlapping administrative paths.')}
|
||||
</p>
|
||||
)}
|
||||
</section>
|
||||
)}
|
||||
{locationRows.map((row, index) => (
|
||||
<fieldset
|
||||
key={index}
|
||||
className="space-y-3 rounded border p-4"
|
||||
>
|
||||
<legend className="px-1 font-medium">
|
||||
{t('Localização', 'Location')}{' '}
|
||||
{index + 1}
|
||||
</legend>
|
||||
{(
|
||||
[
|
||||
'province_id',
|
||||
'district_id',
|
||||
'admin_post_id',
|
||||
'locality_id',
|
||||
] as const
|
||||
).map((key) => {
|
||||
const configuration =
|
||||
territoryField[key];
|
||||
const parentId =
|
||||
configuration.parent
|
||||
? row[
|
||||
configuration.parent as keyof typeof row
|
||||
]
|
||||
: '';
|
||||
const name = t(
|
||||
key === 'province_id'
|
||||
? 'Província ou Cidade de Maputo'
|
||||
: key === 'district_id'
|
||||
? 'Distrito'
|
||||
: key ===
|
||||
'admin_post_id'
|
||||
? 'Posto administrativo'
|
||||
: 'Localidade',
|
||||
key === 'province_id'
|
||||
? 'Province or Maputo City'
|
||||
: key === 'district_id'
|
||||
? 'District'
|
||||
: key ===
|
||||
'admin_post_id'
|
||||
? 'Administrative post'
|
||||
: 'Locality',
|
||||
);
|
||||
return (
|
||||
<Field
|
||||
key={key}
|
||||
label={`${name}${key === 'province_id' ? ' *' : ''}`}
|
||||
>
|
||||
<TerritoryField
|
||||
name={`${name}, ${t('localização', 'location')} ${index + 1}`}
|
||||
type={
|
||||
configuration.type
|
||||
}
|
||||
parentId={parentId}
|
||||
value={row[key]}
|
||||
required={
|
||||
key ===
|
||||
'province_id'
|
||||
}
|
||||
onChange={(value) =>
|
||||
setLocation(
|
||||
index,
|
||||
key,
|
||||
value,
|
||||
)
|
||||
}
|
||||
/>
|
||||
</Field>
|
||||
);
|
||||
})}
|
||||
<button
|
||||
type="button"
|
||||
className="underline"
|
||||
disabled={
|
||||
locationRows.length <= 1
|
||||
}
|
||||
onClick={() =>
|
||||
removeLocation(index)
|
||||
}
|
||||
>
|
||||
{t(
|
||||
'Remover esta localização',
|
||||
'Remove this location',
|
||||
)}
|
||||
</button>
|
||||
</fieldset>
|
||||
))}
|
||||
<button
|
||||
type="button"
|
||||
className={button}
|
||||
disabled={locationRows.length >= 20}
|
||||
onClick={addLocation}
|
||||
>
|
||||
{t(
|
||||
'Adicionar localização',
|
||||
'Add location',
|
||||
)}
|
||||
</button>
|
||||
{locationRows.length < 2 && (
|
||||
<p role="status">
|
||||
{t(
|
||||
'Adicione pelo menos mais uma localização antes de submeter.',
|
||||
'Add at least one more location before submitting.',
|
||||
)}
|
||||
</p>
|
||||
)}
|
||||
{duplicateLocationPath && (
|
||||
<p
|
||||
role="alert"
|
||||
className="text-red-700"
|
||||
>
|
||||
{t(
|
||||
'Remova percursos administrativos repetidos ou sobrepostos.',
|
||||
'Remove repeated or overlapping administrative paths.',
|
||||
)}
|
||||
</p>
|
||||
)}
|
||||
</section>
|
||||
)}
|
||||
{step === 7 && (
|
||||
<p role="note">
|
||||
{t(
|
||||
@@ -868,15 +975,20 @@ export default function BusinessWizard({
|
||||
</dd>
|
||||
</div>
|
||||
{Object.entries(formSchema)
|
||||
.filter(([, field]) =>
|
||||
field.type !== 'location_list' &&
|
||||
Object.entries(
|
||||
field.when ?? {},
|
||||
).every(([dependency, values]) =>
|
||||
values.includes(
|
||||
payload[dependency] ?? '',
|
||||
.filter(
|
||||
([, field]) =>
|
||||
field.type !==
|
||||
'location_list' &&
|
||||
Object.entries(
|
||||
field.when ?? {},
|
||||
).every(
|
||||
([dependency, values]) =>
|
||||
values.includes(
|
||||
payload[
|
||||
dependency
|
||||
] ?? '',
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
.map(([key, field]) => (
|
||||
<div key={key}>
|
||||
@@ -893,10 +1005,17 @@ export default function BusinessWizard({
|
||||
{payload.geography_scope === 'multiple' && (
|
||||
<div>
|
||||
<dt className="font-semibold">
|
||||
{t('Localizações administrativas', 'Administrative locations')}
|
||||
{t(
|
||||
'Localizações administrativas',
|
||||
'Administrative locations',
|
||||
)}
|
||||
</dt>
|
||||
<dd>
|
||||
{locationRows.length} {t('localizações indicadas', 'locations entered')}
|
||||
{locationRows.length}{' '}
|
||||
{t(
|
||||
'localizações indicadas',
|
||||
'locations entered',
|
||||
)}
|
||||
</dd>
|
||||
</div>
|
||||
)}
|
||||
@@ -905,11 +1024,22 @@ export default function BusinessWizard({
|
||||
<ul className="list-disc space-y-1 pl-6">
|
||||
{locationRows.map((_, index) => (
|
||||
<li key={index}>
|
||||
{payload.locations_json === resolvedLocationJson && territoryPaths[index]?.length
|
||||
? territoryPaths[index].join(' → ')
|
||||
: payload.locations_json !== resolvedLocationJson
|
||||
? t('Os nomes serão apresentados depois de guardar este rascunho.', 'Names will appear after this draft is saved.')
|
||||
: t('Percurso territorial indisponível na referência pública actual.', 'Territorial path unavailable in the current public reference data.')}
|
||||
{payload.locations_json ===
|
||||
resolvedLocationJson &&
|
||||
territoryPaths[index]?.length
|
||||
? territoryPaths[
|
||||
index
|
||||
].join(' → ')
|
||||
: payload.locations_json !==
|
||||
resolvedLocationJson
|
||||
? t(
|
||||
'Os nomes serão apresentados depois de guardar este rascunho.',
|
||||
'Names will appear after this draft is saved.',
|
||||
)
|
||||
: t(
|
||||
'Percurso territorial indisponível na referência pública actual.',
|
||||
'Territorial path unavailable in the current public reference data.',
|
||||
)}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
|
||||
Reference in New Issue
Block a user