Skip to content

Commit 4185efe

Browse files
authored
Merge pull request #592 from sahilds1/571-improve-library-benefit-picker
[#571] Improve library benefit picker
2 parents e2b1ba5 + f82e75b commit 4185efe

8 files changed

Lines changed: 212 additions & 16 deletions

File tree

‎builder-frontend/src/components/homeScreen/ImportScreenerForm.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createSignal, Show } from "solid-js";
22
import { useNavigate } from "@solidjs/router";
33
import { Button } from "@/components/shared/Button";
44
import { importScreener, readScreenerFile } from "@/api/screenerTransfer";
5+
import { normalizeForComparison } from "@/utils/normalizeForComparison";
56

67
export default function ImportScreenerForm(props: {
78
existingNames?: string[];
@@ -16,7 +17,7 @@ export default function ImportScreenerForm(props: {
1617
const duplicate = () =>
1718
props.existingNames?.some(
1819
(existing) =>
19-
existing.trim().toLowerCase() === name().trim().toLowerCase(),
20+
normalizeForComparison(existing) === normalizeForComparison(name()),
2021
);
2122
let fileInput!: HTMLInputElement;
2223

‎builder-frontend/src/components/screenerEditor/formJsExtensions/printSummary/PrintSummary.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { isDecided, webLink } from "../nextSteps/content";
77
import { firstVisibleNextSteps } from "../nextSteps/selection";
88
import type { ScreeningResultsService } from "../screeningResults/screeningResults";
99
import { useScreeningResults } from "../screeningResults/useScreeningResults";
10+
import { normalizeForComparison } from "@/utils/normalizeForComparison";
1011
import "./printSummary.css";
1112

1213
export const PRINT_SUMMARY_TYPE = "print_summary";
@@ -35,7 +36,7 @@ function printedDestination(link: HTMLAnchorElement): string | undefined {
3536
const shown =
3637
link.protocol === "tel:"
3738
? (value: string) => value.replace(/[^+\d]/g, "")
38-
: (value: string) => value.trim().toLowerCase();
39+
: normalizeForComparison;
3940
return shown(link.textContent ?? "") === shown(contact) ? undefined : contact;
4041
}
4142

‎builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/BenefitList.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ const BenefitList = ({
114114
<SelectExistingBenefitModal
115115
closeModal={() => setSelectExistingBenefitModal(false)}
116116
importBenefit={actions.importBenefit}
117+
// Names are the only way the frontend can tell what's already in the screener
118+
// Null for a new screener (Screener.create() doesn't set benefits), despite the type
119+
existingBenefitNames={(screenerBenefits() ?? []).map(
120+
(benefit) => benefit.name,
121+
)}
117122
/>
118123
)}
119124
{benefitIdToRemove() !== null && (
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// @vitest-environment jsdom
2+
3+
import { createSignal } from "solid-js";
4+
import { render } from "solid-js/web";
5+
import { afterEach, describe, expect, it, vi } from "vitest";
6+
7+
import SelectExistingBenefitModal from "./SelectExistingBenefitModal";
8+
import { fetchLibraryBenefits } from "@/api/benefit";
9+
import type { Benefit } from "@/types";
10+
11+
const libraryBenefits: Benefit[] = [
12+
{ id: "snap", name: "SNAP", description: "Food assistance", checks: [] },
13+
{ id: "liheap", name: "LIHEAP", description: "Energy help", checks: [] },
14+
];
15+
16+
vi.mock("@/api/benefit", () => ({
17+
fetchLibraryBenefits: vi.fn(async () => libraryBenefits),
18+
}));
19+
20+
// Let the library benefits resource resolve and the modal re-render.
21+
const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
22+
23+
describe("SelectExistingBenefitModal", () => {
24+
let dispose: (() => void) | undefined;
25+
26+
afterEach(() => {
27+
dispose?.();
28+
document.body.innerHTML = "";
29+
});
30+
31+
// Takes accessors so tests can change the screener's benefits after rendering
32+
const renderModal = (
33+
existingBenefitNames: () => string[],
34+
importBenefit: (benefitId: string) => Promise<void> = async () => {},
35+
closeModal: () => void = () => {},
36+
) => {
37+
const container = document.createElement("div");
38+
// Solid handles onClick at the document, so the container must be in it
39+
document.body.appendChild(container);
40+
dispose = render(
41+
() => (
42+
<SelectExistingBenefitModal
43+
importBenefit={importBenefit}
44+
closeModal={closeModal}
45+
existingBenefitNames={existingBenefitNames()}
46+
/>
47+
),
48+
container,
49+
);
50+
return container;
51+
};
52+
53+
it("shows every library benefit when the screener has none of them", async () => {
54+
const container = renderModal(() => []);
55+
await flush();
56+
57+
expect(container.textContent).toContain("SNAP");
58+
expect(container.textContent).toContain("LIHEAP");
59+
});
60+
61+
it("hides library benefits already in the screener, ignoring case and spaces", async () => {
62+
const container = renderModal(() => [" snap "]);
63+
await flush();
64+
65+
expect(container.textContent).not.toContain("SNAP");
66+
expect(container.textContent).toContain("LIHEAP");
67+
});
68+
69+
it("still renders when a screener benefit has no name", async () => {
70+
const container = renderModal(() => [null as unknown as string]);
71+
await flush();
72+
73+
expect(container.textContent).toContain("SNAP");
74+
expect(container.textContent).toContain("LIHEAP");
75+
});
76+
77+
it("says so when every library benefit is already in the screener", async () => {
78+
const container = renderModal(() => ["SNAP", "LIHEAP"]);
79+
await flush();
80+
81+
expect(container.textContent).toContain(
82+
"All library benefits have already been added to this screener.",
83+
);
84+
expect(container.textContent).not.toContain(
85+
"No library benefits are currently available.",
86+
);
87+
});
88+
89+
it("updates the list when the screener's benefits change", async () => {
90+
const [names, setNames] = createSignal<string[]>([]);
91+
const container = renderModal(names);
92+
await flush();
93+
expect(container.textContent).toContain("SNAP");
94+
95+
setNames(["SNAP"]);
96+
97+
expect(container.textContent).not.toContain("SNAP");
98+
expect(container.textContent).toContain("LIHEAP");
99+
});
100+
101+
it("keeps the benefit being added visible until the import finishes", async () => {
102+
const [names, setNames] = createSignal<string[]>([]);
103+
let finishImport: () => void = () => {};
104+
const closeModal = vi.fn();
105+
const container = renderModal(
106+
names,
107+
() => new Promise<void>((resolve) => (finishImport = resolve)),
108+
closeModal,
109+
);
110+
await flush();
111+
112+
// SNAP is listed first, so its button is the first "Add to screener"
113+
container.querySelector("button")!.click();
114+
// The screener refetch lands before importBenefit resolves
115+
setNames(["SNAP"]);
116+
117+
expect(container.textContent).toContain("SNAP");
118+
expect(container.textContent).toContain("Adding...");
119+
expect(closeModal).not.toHaveBeenCalled();
120+
121+
finishImport();
122+
await flush();
123+
124+
expect(closeModal).toHaveBeenCalledOnce();
125+
});
126+
127+
it("shows the load error without crashing", async () => {
128+
vi.mocked(fetchLibraryBenefits).mockRejectedValueOnce(new Error("boom"));
129+
const container = renderModal(() => []);
130+
await flush();
131+
132+
expect(container.textContent).toContain("Error loading benefits: boom");
133+
expect(container.textContent).not.toContain(
134+
"All library benefits have already been added to this screener.",
135+
);
136+
});
137+
});

‎builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.tsx‎

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,44 @@
1-
import { createResource, createSignal, For, Show } from "solid-js";
1+
import {
2+
createMemo,
3+
createResource,
4+
createSignal,
5+
For,
6+
Match,
7+
Show,
8+
Switch,
9+
} from "solid-js";
210

311
import { fetchLibraryBenefits } from "@/api/benefit";
412
import type { Benefit } from "@/types";
513
import LibraryReference from "@/components/shared/libraryReference/LibraryReference";
14+
import { normalizeForComparison } from "@/utils/normalizeForComparison";
615

716
const SelectExistingBenefitModal = (props: {
817
importBenefit: (benefitId: string) => Promise<void>;
918
closeModal: () => void;
19+
existingBenefitNames: string[];
1020
}) => {
1121
const [availableBenefits] = createResource<Benefit[]>(fetchLibraryBenefits);
1222
const [importingId, setImportingId] = createSignal<string | null>(null);
1323
const [error, setError] = createSignal<string | null>(null);
1424

25+
// Library benefits with a matching name are hidden so the same benefit isn't added twice
26+
const benefitsNotInScreener = createMemo(() => {
27+
// Reading an errored resource throws, so show no benefits instead
28+
if (availableBenefits.error) return [];
29+
const existingNames = new Set(
30+
// Ignore case and surrounding whitespace so small differences still match
31+
props.existingBenefitNames.map(normalizeForComparison),
32+
);
33+
return (availableBenefits() ?? []).filter(
34+
// Keep the benefit being added in the list until the modal closes, since the
35+
// screener refetches before importBenefit resolves
36+
(benefit) =>
37+
benefit.id === importingId() ||
38+
!existingNames.has(normalizeForComparison(benefit.name)),
39+
);
40+
});
41+
1542
const importBenefit = async (benefitId: string) => {
1643
setImportingId(benefitId);
1744
setError(null);
@@ -35,25 +62,32 @@ const SelectExistingBenefitModal = (props: {
3562
description, and eligibility checks.
3663
</p>
3764

38-
<Show when={availableBenefits.loading}>
39-
<div>Loading library benefits...</div>
40-
</Show>
41-
<Show when={availableBenefits.error}>
42-
<div class="text-red-600" role="alert">
43-
Error loading benefits: {availableBenefits.error.message}
44-
</div>
45-
</Show>
4665
<Show when={error()}>
4766
<div class="text-red-600 mb-3" role="alert">
4867
{error()}
4968
</div>
5069
</Show>
51-
<Show when={availableBenefits()?.length === 0}>
52-
<div>No library benefits are currently available.</div>
53-
</Show>
70+
<Switch>
71+
<Match when={availableBenefits.loading}>
72+
<div>Loading library benefits...</div>
73+
</Match>
74+
<Match when={availableBenefits.error}>
75+
<div class="text-red-600" role="alert">
76+
Error loading benefits: {availableBenefits.error.message}
77+
</div>
78+
</Match>
79+
<Match when={availableBenefits()?.length === 0}>
80+
<div>No library benefits are currently available.</div>
81+
</Match>
82+
<Match when={benefitsNotInScreener().length === 0}>
83+
<div>
84+
All library benefits have already been added to this screener.
85+
</div>
86+
</Match>
87+
</Switch>
5488

5589
<div class="max-h-96 space-y-3 overflow-y-auto">
56-
<For each={availableBenefits()}>
90+
<For each={benefitsNotInScreener()}>
5791
{(benefit) => (
5892
<div class="border-2 border-gray-200 rounded p-4">
5993
<div class="font-bold text-lg">{benefit.name}</div>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { describe, expect, it } from "vitest";
2+
import { normalizeForComparison } from "./normalizeForComparison";
3+
4+
describe("normalizeForComparison", () => {
5+
it("ignores case and surrounding whitespace", () => {
6+
expect(normalizeForComparison(" SNAP Benefits ")).toBe(
7+
normalizeForComparison("snap benefits"),
8+
);
9+
});
10+
11+
it("treats a missing name as an empty string", () => {
12+
expect(normalizeForComparison(null)).toBe("");
13+
expect(normalizeForComparison(undefined)).toBe("");
14+
});
15+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Ignores case and surrounding whitespace when comparing names or addresses, and treats a missing value as ""
2+
export const normalizeForComparison = (value: string | null | undefined) =>
3+
(value ?? "").trim().toLowerCase();

‎docs/src/content/docs/user/user-guide.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ The **Manage Benefits** tab displays all benefits configured for your screener a
7575
From this view, you can:
7676

7777
- **Create a custom benefit** by selecting **Create custom benefit** and providing a name and description
78-
- **Add a library benefit** by selecting **Add library benefit**, choosing a pre-built benefit, and selecting **Add to screener**. The imported copy can then be edited without changing the library source.
78+
- **Add a library benefit** by selecting **Add library benefit**, choosing a pre-built benefit, and selecting **Add to screener**. The imported copy can then be edited without changing the library source. Library benefits already in your screener aren't listed. A benefit counts as added when your screener has a benefit with the same name, ignoring capitals and extra spaces. So if you rename an added benefit, the library benefit is listed again, and if you create a custom benefit with the same name as a library benefit, that library benefit isn't listed.
7979
- **Edit** a benefit by selecting **Edit** on its card, which opens the **Configure Benefit** page
8080
- **Remove** a benefit by selecting **Remove** on its card
8181

0 commit comments

Comments
 (0)