Mapa de erros 4.4: o grupo escolhido nas zonas mantém-se após a pesquisa
A lista de grupos voltava a «Todas» depois de cada pesquisa, embora o endereço guardasse o grupo. Os grupos sem registos para a selecção passam a dizê-lo e não podem ser escolhidos, salvo o que já está aplicado. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MzJmxFy3izprXr5dSq91jM
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Reset a search form to the filters the server applied.
|
||||
*
|
||||
* React only writes a select's defaultValue into the DOM on the first render,
|
||||
* so a plain form.reset() after a search returns every select to the value the
|
||||
* page opened with (for example "Todas"), while the URL keeps the chosen group.
|
||||
* Selects that declare data-applied are aligned first, so the reset keeps the
|
||||
* applied choice and the focus stays where the investor left it.
|
||||
*/
|
||||
export function resetToAppliedFilters(form: HTMLFormElement | null): void {
|
||||
if (!form) return;
|
||||
for (const select of Array.from(form.querySelectorAll("select"))) {
|
||||
const applied = select.getAttribute("data-applied");
|
||||
if (applied === null) continue;
|
||||
for (const option of Array.from(select.options))
|
||||
option.defaultSelected = option.value === applied;
|
||||
}
|
||||
form.reset();
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import { Link, router, usePage } from '@inertiajs/react';
|
||||
import { searchTerritoryHref } from '@/lib/search-territory';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { territorialSearchParams } from '@/lib/territorial-search-params';
|
||||
import { resetToAppliedFilters } from '@/lib/applied-form-reset';
|
||||
import CatalogueFilterContext from '@/components/catalogue-filter-context';
|
||||
import { catalogueDetailHref } from '@/lib/catalogue-context';
|
||||
import {
|
||||
@@ -116,7 +117,7 @@ export default function Zones({
|
||||
const [searchError, setSearchError] = useState('');
|
||||
const appliedKey = JSON.stringify([filters, territorySelection]);
|
||||
useEffect(() => {
|
||||
searchForm.current?.reset();
|
||||
resetToAppliedFilters(searchForm.current);
|
||||
}, [appliedKey]);
|
||||
useEffect(
|
||||
() => () => {
|
||||
@@ -301,23 +302,34 @@ export default function Zones({
|
||||
className={input}
|
||||
name="category"
|
||||
defaultValue={filters.category ?? ''}
|
||||
data-applied={filters.category ?? ''}
|
||||
>
|
||||
<option value="">Todas</option>
|
||||
<option value="zee">
|
||||
ZEE
|
||||
{groupAvailability.zee != null &&
|
||||
` — ${groupAvailability.zee} registos`}
|
||||
</option>
|
||||
<option value="zfi">
|
||||
ZFI
|
||||
{groupAvailability.zfi != null &&
|
||||
` — ${groupAvailability.zfi} registos`}
|
||||
</option>
|
||||
<option value="industrial_park">
|
||||
Parque industrial
|
||||
{groupAvailability.industrial_park != null &&
|
||||
` — ${groupAvailability.industrial_park} registos`}
|
||||
</option>
|
||||
{(
|
||||
[
|
||||
['zee', 'ZEE'],
|
||||
['zfi', 'ZFI'],
|
||||
['industrial_park', 'Parque industrial'],
|
||||
] as const
|
||||
).map(([value, label]) => {
|
||||
const count = groupAvailability[value];
|
||||
const empty =
|
||||
count === 0 && filters.category !== value;
|
||||
return (
|
||||
<option
|
||||
key={value}
|
||||
value={value}
|
||||
disabled={empty}
|
||||
>
|
||||
{label}
|
||||
{count == null
|
||||
? ''
|
||||
: count === 0
|
||||
? ' — sem registos para esta selecção'
|
||||
: ` — ${count} ${count === 1 ? 'registo' : 'registos'}`}
|
||||
</option>
|
||||
);
|
||||
})}
|
||||
</select>
|
||||
</Field>
|
||||
<details className="w-full rounded border p-3">
|
||||
@@ -330,6 +342,7 @@ export default function Zones({
|
||||
className={input}
|
||||
name="physical_category"
|
||||
defaultValue={filters.physical_category ?? ''}
|
||||
data-applied={filters.physical_category ?? ''}
|
||||
>
|
||||
<option value="">Todas</option>
|
||||
<option value="industrial_park">
|
||||
@@ -364,6 +377,7 @@ export default function Zones({
|
||||
className={input}
|
||||
name="legal_regime"
|
||||
defaultValue={filters.legal_regime ?? ''}
|
||||
data-applied={filters.legal_regime ?? ''}
|
||||
>
|
||||
<option value="">Todos</option>
|
||||
<option value="zee">ZEE</option>
|
||||
@@ -380,6 +394,7 @@ export default function Zones({
|
||||
defaultValue={
|
||||
filters.development_status ?? ''
|
||||
}
|
||||
data-applied={filters.development_status ?? ''}
|
||||
>
|
||||
<option value="">Todos</option>
|
||||
<option value="announced">Anunciado</option>
|
||||
@@ -407,6 +422,7 @@ export default function Zones({
|
||||
defaultValue={
|
||||
filters.availability_status ?? ''
|
||||
}
|
||||
data-applied={filters.availability_status ?? ''}
|
||||
>
|
||||
<option value="">Todas</option>
|
||||
<option value="confirmed">
|
||||
|
||||
Reference in New Issue
Block a user