From dbc862e558570cce2cf4879f6b9e434d7012b1eb Mon Sep 17 00:00:00 2001 From: Sahil D Shah Date: Thu, 5 Nov 2026 11:48:06 -0500 Subject: [PATCH 1/3] Frontend only implementation of deciding that a library benefit is already added --- .../benefitList/BenefitList.tsx | 4 ++ .../SelectExistingBenefitModal.test.tsx | 68 +++++++++++++++++++ .../modals/SelectExistingBenefitModal.tsx | 37 +++++++++- 3 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.test.tsx diff --git a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/BenefitList.tsx b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/BenefitList.tsx index e7768190..26dd12a3 100644 --- a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/BenefitList.tsx +++ b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/BenefitList.tsx @@ -113,6 +113,10 @@ const BenefitList = ({ setSelectExistingBenefitModal(false)} importBenefit={actions.importBenefit} + // Lets the picker hide library benefits already in this screener. + existingBenefitNames={screenerBenefits().map( + (benefit) => benefit.name, + )} /> )} {benefitIdToRemove() !== null && ( diff --git a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.test.tsx b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.test.tsx new file mode 100644 index 00000000..8c70a977 --- /dev/null +++ b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.test.tsx @@ -0,0 +1,68 @@ +// @vitest-environment jsdom + +import { render } from "solid-js/web"; +import { afterEach, describe, expect, it, vi } from "vitest"; + +import SelectExistingBenefitModal from "./SelectExistingBenefitModal"; +import type { Benefit } from "@/types"; + +const libraryBenefits: Benefit[] = [ + { id: "snap", name: "SNAP", description: "Food assistance", checks: [] }, + { id: "liheap", name: "LIHEAP", description: "Energy help", checks: [] }, +]; + +vi.mock("@/api/benefit", () => ({ + fetchLibraryBenefits: vi.fn(async () => libraryBenefits), +})); + +// Let the library benefits resource resolve and the modal re-render. +const flush = () => new Promise((resolve) => setTimeout(resolve, 0)); + +describe("SelectExistingBenefitModal", () => { + let dispose: (() => void) | undefined; + + afterEach(() => dispose?.()); + + const renderModal = (existingBenefitNames: string[]) => { + const container = document.createElement("div"); + dispose = render( + () => ( + {}} + closeModal={() => {}} + existingBenefitNames={existingBenefitNames} + /> + ), + container, + ); + return container; + }; + + it("shows every library benefit when the screener has none of them", async () => { + const container = renderModal([]); + await flush(); + + expect(container.textContent).toContain("SNAP"); + expect(container.textContent).toContain("LIHEAP"); + }); + + it("hides library benefits already in the screener, ignoring case and spaces", async () => { + const container = renderModal([" snap "]); + await flush(); + + expect(container.textContent).not.toContain("SNAP"); + expect(container.textContent).toContain("LIHEAP"); + }); + + it("says so when every library benefit is already in the screener", async () => { + const container = renderModal(["SNAP", "LIHEAP"]); + await flush(); + + expect(container.textContent).toContain( + "All library benefits have already been added to this screener.", + ); + expect(container.textContent).not.toContain( + "No library benefits are currently available.", + ); + }); +}); diff --git a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.tsx b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.tsx index c1bdc79e..e269b22b 100644 --- a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.tsx +++ b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.tsx @@ -1,13 +1,34 @@ -import { createResource, createSignal, For, Show } from "solid-js"; +import { createMemo, createResource, createSignal, For, Show } from "solid-js"; import { fetchLibraryBenefits } from "@/api/benefit"; import type { Benefit } from "@/types"; +// An imported library benefit is saved as an independent copy with a new ID +// and no link back to the library benefit it came from, so names are the only +// way the frontend can tell what's already in the screener. Ignore case and +// surrounding whitespace so small differences still match. +const normalizeBenefitName = (name: string) => name.trim().toLowerCase(); + const SelectExistingBenefitModal = (props: { importBenefit: (benefitId: string) => Promise; closeModal: () => void; + // Names of benefits already in the screener. Library benefits with a + // matching name are hidden so the same benefit isn't added twice (#571). + existingBenefitNames: string[]; }) => { const [availableBenefits] = createResource(fetchLibraryBenefits); + + // Limitation of matching by name: if an imported benefit is renamed, its + // library version reappears here. That's the same as the picker's behavior + // before this filter existed, so it's an acceptable trade-off. + const benefitsNotInScreener = createMemo(() => { + const existingNames = new Set( + props.existingBenefitNames.map(normalizeBenefitName), + ); + return (availableBenefits() ?? []).filter( + (benefit) => !existingNames.has(normalizeBenefitName(benefit.name)), + ); + }); const [importingId, setImportingId] = createSignal(null); const [error, setError] = createSignal(null); @@ -50,9 +71,21 @@ const SelectExistingBenefitModal = (props: {
No library benefits are currently available.
+ {/* Separate from the message above: the library has benefits, but + this screener already has every one of them. */} + 0 && + benefitsNotInScreener().length === 0 + } + > +
+ All library benefits have already been added to this screener. +
+
- + {(benefit) => (
{benefit.name}
From 8beca127e81a2f728d88b036d3c96855153428ee Mon Sep 17 00:00:00 2001 From: Sahil D Shah Date: Wed, 11 Nov 2026 14:16:08 -0500 Subject: [PATCH 2/3] Guard the library benefit picker against null benefits and names A new screener's benefits list comes back from the API as null normalizeBenefitName also treats a missing name as an empty string --- .../controller/LibraryBenefitResource.java | 1 + .../benefitList/BenefitList.tsx | 5 ++-- .../SelectExistingBenefitModal.test.tsx | 9 +++++++ .../modals/SelectExistingBenefitModal.tsx | 27 ++++++++++--------- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/builder-api/src/main/java/org/codeforphilly/bdt/builder/controller/LibraryBenefitResource.java b/builder-api/src/main/java/org/codeforphilly/bdt/builder/controller/LibraryBenefitResource.java index 01747ebb..de2f5012 100644 --- a/builder-api/src/main/java/org/codeforphilly/bdt/builder/controller/LibraryBenefitResource.java +++ b/builder-api/src/main/java/org/codeforphilly/bdt/builder/controller/LibraryBenefitResource.java @@ -58,6 +58,7 @@ public Response importLibraryBenefit( return Response.status(Response.Status.UNAUTHORIZED).build(); } + // TODO(High): Store the source library benefit id on the copy and reject duplicate imports Optional importedBenefitOpt = libraryApiService.copyBenefitForOwner(request.benefitId(), userId); if (importedBenefitOpt.isEmpty()) { diff --git a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/BenefitList.tsx b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/BenefitList.tsx index 26dd12a3..62e03ba7 100644 --- a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/BenefitList.tsx +++ b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/BenefitList.tsx @@ -113,8 +113,9 @@ const BenefitList = ({ setSelectExistingBenefitModal(false)} importBenefit={actions.importBenefit} - // Lets the picker hide library benefits already in this screener. - existingBenefitNames={screenerBenefits().map( + // Names are the only way the frontend can tell what's already in the screener + // Pass (screenerBenefits() ?? []) for new screeners whose benefits list is null + existingBenefitNames={(screenerBenefits() ?? []).map( (benefit) => benefit.name, )} /> diff --git a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.test.tsx b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.test.tsx index 8c70a977..8ea40306 100644 --- a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.test.tsx +++ b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.test.tsx @@ -23,6 +23,7 @@ describe("SelectExistingBenefitModal", () => { afterEach(() => dispose?.()); + // TODO(Low): Add a test that the list updates when existingBenefitNames changes after an import const renderModal = (existingBenefitNames: string[]) => { const container = document.createElement("div"); dispose = render( @@ -54,6 +55,14 @@ describe("SelectExistingBenefitModal", () => { expect(container.textContent).toContain("LIHEAP"); }); + it("still renders when a screener benefit has no name", async () => { + const container = renderModal([null as unknown as string]); + await flush(); + + expect(container.textContent).toContain("SNAP"); + expect(container.textContent).toContain("LIHEAP"); + }); + it("says so when every library benefit is already in the screener", async () => { const container = renderModal(["SNAP", "LIHEAP"]); await flush(); diff --git a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.tsx b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.tsx index e269b22b..802465c4 100644 --- a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.tsx +++ b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.tsx @@ -3,32 +3,34 @@ import { createMemo, createResource, createSignal, For, Show } from "solid-js"; import { fetchLibraryBenefits } from "@/api/benefit"; import type { Benefit } from "@/types"; -// An imported library benefit is saved as an independent copy with a new ID -// and no link back to the library benefit it came from, so names are the only -// way the frontend can tell what's already in the screener. Ignore case and -// surrounding whitespace so small differences still match. -const normalizeBenefitName = (name: string) => name.trim().toLowerCase(); +// Treat a missing name as an empty string +// TODO(Low): Skip empty names so blank screener names don't hide blank-named library benefits +const normalizeBenefitName = (name: string | null | undefined) => + (name ?? "").trim().toLowerCase(); const SelectExistingBenefitModal = (props: { importBenefit: (benefitId: string) => Promise; closeModal: () => void; - // Names of benefits already in the screener. Library benefits with a - // matching name are hidden so the same benefit isn't added twice (#571). existingBenefitNames: string[]; }) => { const [availableBenefits] = createResource(fetchLibraryBenefits); - // Limitation of matching by name: if an imported benefit is renamed, its - // library version reappears here. That's the same as the picker's behavior - // before this filter existed, so it's an acceptable trade-off. + // Library benefits with a matching name are hidden so the same benefit isn't added twice + // TODO(Medium): Renamed imports aren't detected, so the library benefit reappears and can be re-imported + // TODO(Low): A custom benefit with a library benefit's name hides that library benefit + // TODO(Medium): Check availableBenefits.error before reading, since an errored resource throws const benefitsNotInScreener = createMemo(() => { const existingNames = new Set( + // TODO(Low): Remove this and the duplicate comment below; normalizeBenefitName covers it + // Ignore case and surrounding whitespace so small differences still match props.existingBenefitNames.map(normalizeBenefitName), ); return (availableBenefits() ?? []).filter( + // Ignore case and surrounding whitespace so small differences still match (benefit) => !existingNames.has(normalizeBenefitName(benefit.name)), ); }); + const [importingId, setImportingId] = createSignal(null); const [error, setError] = createSignal(null); @@ -36,6 +38,7 @@ const SelectExistingBenefitModal = (props: { setImportingId(benefitId); setError(null); try { + // TODO(Low): The refetch filters out this benefit before the modal closes, so the list flashes await props.importBenefit(benefitId); props.closeModal(); } catch (cause) { @@ -71,8 +74,8 @@ const SelectExistingBenefitModal = (props: {
No library benefits are currently available.
- {/* Separate from the message above: the library has benefits, but - this screener already has every one of them. */} + {/* Separate from "No library benefits are currently available" */} + {/* TODO(Low): Use one Switch/Match over the states so only one message can show */} 0 && From 029a526f78eca389dee3fe5a9a8bfbda33cd06f9 Mon Sep 17 00:00:00 2001 From: Sahil D Shah Date: Fri, 13 Nov 2026 10:54:40 -0500 Subject: [PATCH 3/3] Handle library benefit load errors and import refetch - Show the load error instead of crashing when the library benefits fail to load - Keep the benefit being added in the list until the modal closes, instead of letting it flash out when the screener refetches - Show only one of the loading, error, empty and all-added messages at a time --- .../controller/LibraryBenefitResource.java | 1 - .../benefitList/BenefitList.tsx | 2 +- .../SelectExistingBenefitModal.test.tsx | 80 ++++++++++++++++--- .../modals/SelectExistingBenefitModal.tsx | 71 ++++++++-------- 4 files changed, 107 insertions(+), 47 deletions(-) diff --git a/builder-api/src/main/java/org/codeforphilly/bdt/builder/controller/LibraryBenefitResource.java b/builder-api/src/main/java/org/codeforphilly/bdt/builder/controller/LibraryBenefitResource.java index de2f5012..01747ebb 100644 --- a/builder-api/src/main/java/org/codeforphilly/bdt/builder/controller/LibraryBenefitResource.java +++ b/builder-api/src/main/java/org/codeforphilly/bdt/builder/controller/LibraryBenefitResource.java @@ -58,7 +58,6 @@ public Response importLibraryBenefit( return Response.status(Response.Status.UNAUTHORIZED).build(); } - // TODO(High): Store the source library benefit id on the copy and reject duplicate imports Optional importedBenefitOpt = libraryApiService.copyBenefitForOwner(request.benefitId(), userId); if (importedBenefitOpt.isEmpty()) { diff --git a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/BenefitList.tsx b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/BenefitList.tsx index 62e03ba7..0e106e48 100644 --- a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/BenefitList.tsx +++ b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/BenefitList.tsx @@ -114,7 +114,7 @@ const BenefitList = ({ closeModal={() => setSelectExistingBenefitModal(false)} importBenefit={actions.importBenefit} // Names are the only way the frontend can tell what's already in the screener - // Pass (screenerBenefits() ?? []) for new screeners whose benefits list is null + // Null for a new screener (Screener.create() doesn't set benefits), despite the type existingBenefitNames={(screenerBenefits() ?? []).map( (benefit) => benefit.name, )} diff --git a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.test.tsx b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.test.tsx index 8ea40306..1198903e 100644 --- a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.test.tsx +++ b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.test.tsx @@ -1,9 +1,11 @@ // @vitest-environment jsdom +import { createSignal } from "solid-js"; import { render } from "solid-js/web"; import { afterEach, describe, expect, it, vi } from "vitest"; import SelectExistingBenefitModal from "./SelectExistingBenefitModal"; +import { fetchLibraryBenefits } from "@/api/benefit"; import type { Benefit } from "@/types"; const libraryBenefits: Benefit[] = [ @@ -21,17 +23,26 @@ const flush = () => new Promise((resolve) => setTimeout(resolve, 0)); describe("SelectExistingBenefitModal", () => { let dispose: (() => void) | undefined; - afterEach(() => dispose?.()); + afterEach(() => { + dispose?.(); + document.body.innerHTML = ""; + }); - // TODO(Low): Add a test that the list updates when existingBenefitNames changes after an import - const renderModal = (existingBenefitNames: string[]) => { + // Takes accessors so tests can change the screener's benefits after rendering + const renderModal = ( + existingBenefitNames: () => string[], + importBenefit: (benefitId: string) => Promise = async () => {}, + closeModal: () => void = () => {}, + ) => { const container = document.createElement("div"); + // Solid handles onClick at the document, so the container must be in it + document.body.appendChild(container); dispose = render( () => ( {}} - closeModal={() => {}} - existingBenefitNames={existingBenefitNames} + importBenefit={importBenefit} + closeModal={closeModal} + existingBenefitNames={existingBenefitNames()} /> ), container, @@ -40,7 +51,7 @@ describe("SelectExistingBenefitModal", () => { }; it("shows every library benefit when the screener has none of them", async () => { - const container = renderModal([]); + const container = renderModal(() => []); await flush(); expect(container.textContent).toContain("SNAP"); @@ -48,7 +59,7 @@ describe("SelectExistingBenefitModal", () => { }); it("hides library benefits already in the screener, ignoring case and spaces", async () => { - const container = renderModal([" snap "]); + const container = renderModal(() => [" snap "]); await flush(); expect(container.textContent).not.toContain("SNAP"); @@ -56,7 +67,7 @@ describe("SelectExistingBenefitModal", () => { }); it("still renders when a screener benefit has no name", async () => { - const container = renderModal([null as unknown as string]); + const container = renderModal(() => [null as unknown as string]); await flush(); expect(container.textContent).toContain("SNAP"); @@ -64,7 +75,7 @@ describe("SelectExistingBenefitModal", () => { }); it("says so when every library benefit is already in the screener", async () => { - const container = renderModal(["SNAP", "LIHEAP"]); + const container = renderModal(() => ["SNAP", "LIHEAP"]); await flush(); expect(container.textContent).toContain( @@ -74,4 +85,53 @@ describe("SelectExistingBenefitModal", () => { "No library benefits are currently available.", ); }); + + it("updates the list when the screener's benefits change", async () => { + const [names, setNames] = createSignal([]); + const container = renderModal(names); + await flush(); + expect(container.textContent).toContain("SNAP"); + + setNames(["SNAP"]); + + expect(container.textContent).not.toContain("SNAP"); + expect(container.textContent).toContain("LIHEAP"); + }); + + it("keeps the benefit being added visible until the import finishes", async () => { + const [names, setNames] = createSignal([]); + let finishImport: () => void = () => {}; + const closeModal = vi.fn(); + const container = renderModal( + names, + () => new Promise((resolve) => (finishImport = resolve)), + closeModal, + ); + await flush(); + + // SNAP is listed first, so its button is the first "Add to screener" + container.querySelector("button")!.click(); + // The screener refetch lands before importBenefit resolves + setNames(["SNAP"]); + + expect(container.textContent).toContain("SNAP"); + expect(container.textContent).toContain("Adding..."); + expect(closeModal).not.toHaveBeenCalled(); + + finishImport(); + await flush(); + + expect(closeModal).toHaveBeenCalledOnce(); + }); + + it("shows the load error without crashing", async () => { + vi.mocked(fetchLibraryBenefits).mockRejectedValueOnce(new Error("boom")); + const container = renderModal(() => []); + await flush(); + + expect(container.textContent).toContain("Error loading benefits: boom"); + expect(container.textContent).not.toContain( + "All library benefits have already been added to this screener.", + ); + }); }); diff --git a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.tsx b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.tsx index 802465c4..9dd8c9c1 100644 --- a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.tsx +++ b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.tsx @@ -1,10 +1,17 @@ -import { createMemo, createResource, createSignal, For, Show } from "solid-js"; +import { + createMemo, + createResource, + createSignal, + For, + Match, + Show, + Switch, +} from "solid-js"; import { fetchLibraryBenefits } from "@/api/benefit"; import type { Benefit } from "@/types"; // Treat a missing name as an empty string -// TODO(Low): Skip empty names so blank screener names don't hide blank-named library benefits const normalizeBenefitName = (name: string | null | undefined) => (name ?? "").trim().toLowerCase(); @@ -14,31 +21,30 @@ const SelectExistingBenefitModal = (props: { existingBenefitNames: string[]; }) => { const [availableBenefits] = createResource(fetchLibraryBenefits); + const [importingId, setImportingId] = createSignal(null); + const [error, setError] = createSignal(null); // Library benefits with a matching name are hidden so the same benefit isn't added twice - // TODO(Medium): Renamed imports aren't detected, so the library benefit reappears and can be re-imported - // TODO(Low): A custom benefit with a library benefit's name hides that library benefit - // TODO(Medium): Check availableBenefits.error before reading, since an errored resource throws const benefitsNotInScreener = createMemo(() => { + // Reading an errored resource throws, so show no benefits instead + if (availableBenefits.error) return []; const existingNames = new Set( - // TODO(Low): Remove this and the duplicate comment below; normalizeBenefitName covers it // Ignore case and surrounding whitespace so small differences still match props.existingBenefitNames.map(normalizeBenefitName), ); return (availableBenefits() ?? []).filter( - // Ignore case and surrounding whitespace so small differences still match - (benefit) => !existingNames.has(normalizeBenefitName(benefit.name)), + // Keep the benefit being added in the list until the modal closes, since the + // screener refetches before importBenefit resolves + (benefit) => + benefit.id === importingId() || + !existingNames.has(normalizeBenefitName(benefit.name)), ); }); - const [importingId, setImportingId] = createSignal(null); - const [error, setError] = createSignal(null); - const importBenefit = async (benefitId: string) => { setImportingId(benefitId); setError(null); try { - // TODO(Low): The refetch filters out this benefit before the modal closes, so the list flashes await props.importBenefit(benefitId); props.closeModal(); } catch (cause) { @@ -58,34 +64,29 @@ const SelectExistingBenefitModal = (props: { description, and eligibility checks.

- -
Loading library benefits...
-
- - - - -
No library benefits are currently available.
-
- {/* Separate from "No library benefits are currently available" */} - {/* TODO(Low): Use one Switch/Match over the states so only one message can show */} - 0 && - benefitsNotInScreener().length === 0 - } - > -
- All library benefits have already been added to this screener. -
-
+ + +
Loading library benefits...
+
+ + + + +
No library benefits are currently available.
+
+ +
+ All library benefits have already been added to this screener. +
+
+