Skip to content

fix(ui): drive the location card's checked state from the form state - #574

Merged
jsenecal merged 1 commit into
mainfrom
fix/549-location-card-state
Nov 21, 2026
Merged

jsenecal merged 1 commit into
mainfrom
fix/549-location-card-state

Conversation

@jsenecal

Copy link
Copy Markdown
Collaborator

Problem

LocationCard kept its checked state in useState(defaultChecked). defaultChecked is read only on mount, so the card didn't follow form.queryLocation once the form was mounted:

  • After a reset from the header title, the form stays mounted and the store's locations are cleared, but a selected card still looked selected. The next click flipped the card's local state to unchecked and asked to remove a location the store no longer had, so nothing happened and a second click was needed.
  • A location set in the store without a click (the URL prefill in feat(ui): fill in the query form from URL parameters #567, and later history and shared results) was selected in the form but its card didn't look selected, and the first click on it did nothing.

The location select (dropdown mode) doesn't have this problem: it is already controlled by selections.queryLocation.

Change

  • location-card.tsx: the card is controlled. defaultChecked becomes isChecked, the local state is removed, and a click calls onChange('remove' | 'add', option) once, from the isChecked it was given. The card renders data-checked, so tests can read its state (it had no other way to show it than the border colour).
  • query-location.tsx: passes isChecked={form.queryLocation.includes(opt.value)}. handleCardChange decides whether the location is selected from form.queryLocation, the list the cards show, instead of selections.queryLocation, and, when adding, drops any existing selection with that value before appending it. Removing works as before. Otherwise a location in form.queryLocation but not in selections would show as selected and ignore the click.

Tests

New in components/looking-glass-form.test.tsx (LookingGlassForm location cards):

  • selects a card with one click after the form is reset: select Router One, reset from the store as the title does, click once; the card and both form.queryLocation and selections.queryLocation are selected (the test from A location card stays selected after a reset and needs two clicks #549);
  • shows the locations selected in the store without a click: form.queryLocation is set directly; the cards follow it, and one click removes a location;
  • shows the locations of a prefilled selection and lets a click remove one: form and selections set together, as prefillForm in feat(ui): fill in the query form from URL parameters #567 does;
  • shows the store in the location select too (dropdown mode): passes before and after the fix.

New components/location-card.test.tsx: shows the isChecked it is given; a click calls onChange exactly once with add or remove, and the card doesn't change by itself.

Changed: "does not show the errors from before a reset on the next query" chose Router Three after the reset, with the comment "a card keeps its own checked state across a reset". It now chooses Router One again.

Without the fix (origin/main's two components, with only data-checked added so the tests can read the card), four form tests fail on the behaviour: the two below, the prefill-style test and the changed reset test (excerpt):

     × selects a card with one click after the form is reset
     × shows the locations selected in the store without a click
 FAIL  ... > selects a card with one click after the form is reset
Error: expect(element).toHaveAttribute("data-checked", "false")
 FAIL  ... > shows the locations selected in the store without a click
Error: expect(element).toHaveAttribute("data-checked", "true")

Mutation runs on the final tests (components/looking-glass-form.test.tsx and components/location-card.test.tsx):

Reverted Result
both components (only data-checked kept) Tests 7 failed | 33 passed (40) (the 3 card unit tests, the 3 new form tests, the changed reset test)
only handleCardChange deciding from selections.queryLocation Tests 1 failed | 39 passed (40) (shows the locations selected in the store without a click)

With the fix: biome format, biome lint and tsc --noEmit pass; Test Files 32 passed (32), Tests 254 passed (254).

Changelog

Notes

Closes #549

LocationCard kept its checked state in useState(defaultChecked), which is
read only on mount. A reset from the header title, or a location set in the
store without a click (as a prefilled link does), left the card showing the
old state, and the next click was ignored because the card and the store
disagreed.

The card is now controlled: QueryLocation passes isChecked from
form.queryLocation and the card only reports 'add' or 'remove'. The card
click handler decides from form.queryLocation too, the list the cards show,
and updates the selections by value.
@jsenecal
jsenecal requested a review from WilhelmZA November 21, 2026 15:47
@jsenecal jsenecal self-assigned this Nov 21, 2026
@github-actions github-actions Bot added the frontend Next.js UI and the runtime UI build label Nov 21, 2026
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 86.219% (+0.03%) from 86.194% — fix/549-location-card-state into main

@WilhelmZA WilhelmZA left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving at bd5fd0c2.

LocationCard no longer keeps its own state. It renders isChecked from form.queryLocation, and a click calls onChange once with remove or add. handleCardChange now decides from form.queryLocation, which is what the cards show, and adds or removes the selection by value. The card has one caller and both call sites are updated; the select mode is unchanged. The branch contains current main, CI gate passes and the commit is verified. Closes #549 fits.

#567 carries this commit, so this PR should merge first.

Nits:

  • components/looking-glass-form.test.tsx: "shows the store in the location select too" also passes on main, so it guards the select mode but not this change. A word in the test name or the body would make that clear.
  • The body has a Changelog entry but CHANGELOG.md is not edited. Please add it under [Unreleased], here or in the release PR.

@WilhelmZA WilhelmZA added the reviewing Issue is being reviewed by maintainers label Nov 21, 2026
@jsenecal
jsenecal merged commit 31ee302 into main Nov 21, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

frontend Next.js UI and the runtime UI build reviewing Issue is being reviewed by maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A location card stays selected after a reset and needs two clicks

3 participants