diff --git a/frontend/src/features/search/SearchPage.tsx b/frontend/src/features/search/SearchPage.tsx index 51f3c45..20cd0cc 100644 --- a/frontend/src/features/search/SearchPage.tsx +++ b/frontend/src/features/search/SearchPage.tsx @@ -1,3 +1,4 @@ +import { useState } from 'react'; import { useSearchParams } from 'react-router'; import { useDocumentTitle } from '../../hooks/useDocumentTitle'; import { parseSearchParams, toSearchParams, type SearchState } from '../../lib/searchParams'; @@ -10,14 +11,29 @@ import { useSearchText } from './useSearchText'; export default function SearchPage() { const [params, setParams] = useSearchParams(); const state = parseSearchParams(params); + const query = params.toString(); + + // React Router changes the URL in a transition, and until it commits, React puts a controlled + // checkbox or select back the way it was. A click looked like it did nothing, and a screen + // reader announced «ikke avkrysset». The controls read `shownState`, which changes at once, + // while the queries follow the URL. Whenever the URL changes, it is the truth again. + const [shownState, setShownState] = useState(state); + const [shownFor, setShownFor] = useState(query); + if (shownFor !== query) { + setShownFor(query); + setShownState(state); + } + const { text, setText, submit } = useSearchText(); const result = useWineSearch(state); - const filters = useFilterOptions(state); + const filters = useFilterOptions(state, shownState); useDocumentTitle(state.q ? `Søk: ${state.q}` : 'Utforsk viner'); // A new history entry for each choice, so the back button undoes the last filter. const changeSearch = (changes: Partial) => { - setParams(toSearchParams({ ...state, ...changes })); + const next = { ...shownState, ...changes }; + setShownState(next); + setParams(toSearchParams(next)); }; return ( @@ -27,7 +43,7 @@ export default function SearchPage() { text={text} onTextChange={setText} onSubmit={submit} - state={state} + state={shownState} filterGroups={filters.groups} filtersError={filters.error} onRetryFilters={filters.refetch} diff --git a/frontend/src/features/search/api/useFilterOptions.ts b/frontend/src/features/search/api/useFilterOptions.ts index ac4cf5f..78a0cb0 100644 --- a/frontend/src/features/search/api/useFilterOptions.ts +++ b/frontend/src/features/search/api/useFilterOptions.ts @@ -10,15 +10,21 @@ export interface FilterOptionsResult { refetch: () => void; } -/** The old counts stay until the new ones arrive, so the panel does not jump between choices. */ -export function useFilterOptions(state: SearchState): FilterOptionsResult { +/** + * The old counts stay until the new ones arrive, so the panel does not jump between choices. + * The counts follow the URL (`state`), while what is ticked follows what the user just chose (`shown`). + */ +export function useFilterOptions( + state: SearchState, + shown: SearchState = state, +): FilterOptionsResult { const { data, previousData, error, refetch } = useQuery( FILTER_OPTIONS_QUERY, { variables: toFilterVariables(state) }, ); return { - groups: toFilterGroups(data ?? previousData, state), + groups: toFilterGroups(data ?? previousData, shown), error, // A failed retry already shows up in `error`, so the rejected promise needs no handling. refetch: () => void refetch().catch(() => undefined), diff --git a/test-results/.last-run.json b/test-results/.last-run.json new file mode 100644 index 0000000..5fca3f8 --- /dev/null +++ b/test-results/.last-run.json @@ -0,0 +1,4 @@ +{ + "status": "failed", + "failedTests": [] +} \ No newline at end of file