Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions frontend/src/features/search/SearchPage.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<SearchState>) => {
setParams(toSearchParams({ ...state, ...changes }));
const next = { ...shownState, ...changes };
setShownState(next);
setParams(toSearchParams(next));
};

return (
Expand All @@ -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}
Expand Down
12 changes: 9 additions & 3 deletions frontend/src/features/search/api/useFilterOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FilterOptionsData, FilterVariables>(
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),
Expand Down
4 changes: 4 additions & 0 deletions test-results/.last-run.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"status": "failed",
"failedTests": []
}