From 8c1f685a15fe977444ca03be908d40616a844f2d Mon Sep 17 00:00:00 2001 From: Preston Cabe Date: Thu, 29 Oct 2026 19:53:09 -0400 Subject: [PATCH 01/31] Add plain-language explanations to configured checks --- .../controller/CustomBenefitResource.java | 34 ++++++++ .../builder/controller/DecisionResource.java | 1 + .../bdt/builder/model/domain/Benefit.java | 1 + .../bdt/builder/model/domain/CheckConfig.java | 12 +++ .../UpdateCheckExplanationRequest.java | 5 ++ .../api/validation/CheckExplanationTest.java | 45 ++++++++++ .../controller/CustomBenefitResourceTest.java | 37 +++++++++ .../controller/DecisionResourceTest.java | 2 + builder-frontend/README.md | 14 ++++ builder-frontend/src/api/benefit.ts | 14 ++++ .../src/components/resultsAliases.test.tsx | 53 ++++++++++++ .../screener/EligibilityResults.tsx | 8 ++ .../formJsExtensions/nextSteps/content.ts | 5 +- .../configureBenefit/ConfigureBenefit.tsx | 6 ++ .../SelectedEligibilityCheck.tsx | 31 +++++++ .../configureBenefit/benefitResource.ts | 19 +++++ .../modals/EditExplanationModal.test.tsx | 51 ++++++++++++ .../modals/EditExplanationModal.tsx | 78 ++++++++++++++++++ .../screenerEditor/preview/Results.tsx | 10 +++ .../components/shared/CheckExplanation.tsx | 82 +++++++++++++++++++ .../src/integrations/nextSteps.test.ts | 13 +++ builder-frontend/src/types.ts | 4 + e2e/tests/nextStepsPublic.spec.ts | 32 +++++++- 23 files changed, 554 insertions(+), 3 deletions(-) create mode 100644 builder-api/src/main/java/org/codeforphilly/bdt/builder/model/dto/custombenefit/UpdateCheckExplanationRequest.java create mode 100644 builder-api/src/test/java/org/codeforphilly/bdt/builder/api/validation/CheckExplanationTest.java create mode 100644 builder-frontend/src/components/screenerEditor/manageBenefits/configureBenefit/modals/EditExplanationModal.test.tsx create mode 100644 builder-frontend/src/components/screenerEditor/manageBenefits/configureBenefit/modals/EditExplanationModal.tsx create mode 100644 builder-frontend/src/components/shared/CheckExplanation.tsx diff --git a/builder-api/src/main/java/org/codeforphilly/bdt/builder/controller/CustomBenefitResource.java b/builder-api/src/main/java/org/codeforphilly/bdt/builder/controller/CustomBenefitResource.java index a84a8b3f..0f43615f 100644 --- a/builder-api/src/main/java/org/codeforphilly/bdt/builder/controller/CustomBenefitResource.java +++ b/builder-api/src/main/java/org/codeforphilly/bdt/builder/controller/CustomBenefitResource.java @@ -4,6 +4,7 @@ import io.quarkus.security.identity.SecurityIdentity; import jakarta.inject.Inject; import jakarta.validation.Valid; +import jakarta.validation.constraints.NotNull; import jakarta.ws.rs.*; import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.MediaType; @@ -13,6 +14,7 @@ import org.codeforphilly.bdt.builder.model.dto.custombenefit.AddCheckRequest; import org.codeforphilly.bdt.builder.model.dto.custombenefit.CreateCustomBenefitRequest; import org.codeforphilly.bdt.builder.model.dto.custombenefit.UpdateCheckAliasRequest; +import org.codeforphilly.bdt.builder.model.dto.custombenefit.UpdateCheckExplanationRequest; import org.codeforphilly.bdt.builder.model.dto.custombenefit.UpdateCheckParametersRequest; import org.codeforphilly.bdt.builder.model.dto.custombenefit.UpdateCustomBenefitRequest; import org.codeforphilly.bdt.builder.persistence.EligibilityCheckRepository; @@ -580,6 +582,38 @@ public Response updateCheckAlias( } } + @PATCH + @Path("/screener/{screenerId}/benefit/{benefitId}/check/{checkId}/explanation") + public Response updateCheckExplanation( + @Context SecurityIdentity identity, + @PathParam("screenerId") String screenerId, + @PathParam("benefitId") String benefitId, + @PathParam("checkId") String checkId, + @NotNull @Valid UpdateCheckExplanationRequest request + ) { + if (!isUserAuthorizedForScreener(AuthUtils.getUserId(identity), screenerId)) { + return Response.status(Response.Status.UNAUTHORIZED).build(); + } + try { + Optional benefitOpt = screenerRepository.getCustomBenefit(screenerId, benefitId); + if (benefitOpt.isEmpty()) { + return Response.status(Response.Status.NOT_FOUND).entity(Map.of("error", "Benefit not found")).build(); + } + Benefit benefit = benefitOpt.get(); + Optional check = benefit.getChecks() == null ? Optional.empty() : benefit.getChecks().stream() + .filter(config -> checkId.equals(config.getCheckId())).findFirst(); + if (check.isEmpty()) { + return Response.status(Response.Status.NOT_FOUND).entity(Map.of("error", "Check not found in benefit")).build(); + } + check.get().setExplanation(request.explanation()); + screenerRepository.updateCustomBenefit(screenerId, benefit); + return Response.ok().build(); + } catch (Exception e) { + Log.error(e); + return Response.serverError().entity(Map.of("error", "Could not update check explanation")).build(); + } + } + @POST @Path("/screener/{screenerId}/benefit/{benefitId}/check/{checkId}/alias/generate") public Response generateCheckAlias( diff --git a/builder-api/src/main/java/org/codeforphilly/bdt/builder/controller/DecisionResource.java b/builder-api/src/main/java/org/codeforphilly/bdt/builder/controller/DecisionResource.java index 39bfa917..51f8008a 100644 --- a/builder-api/src/main/java/org/codeforphilly/bdt/builder/controller/DecisionResource.java +++ b/builder-api/src/main/java/org/codeforphilly/bdt/builder/controller/DecisionResource.java @@ -167,6 +167,7 @@ private Map evaluateBenefit(Benefit benefit, Map Map checkResultMap = new HashMap<>(); checkResultMap.put("name", checkConfig.getCheckName()); checkResultMap.put("aliasName", checkConfig.getAliasName()); + checkResultMap.put("explanation", checkConfig.getExplanation()); checkResultMap.put("result", evaluationResult); checkResultMap.put("module", checkConfig.getCheckModule() != null ? checkConfig.getCheckModule() : ""); checkResultMap.put("version", checkConfig.getCheckVersion() != null ? checkConfig.getCheckVersion() : ""); diff --git a/builder-api/src/main/java/org/codeforphilly/bdt/builder/model/domain/Benefit.java b/builder-api/src/main/java/org/codeforphilly/bdt/builder/model/domain/Benefit.java index 93a8408b..c3ff1c7d 100644 --- a/builder-api/src/main/java/org/codeforphilly/bdt/builder/model/domain/Benefit.java +++ b/builder-api/src/main/java/org/codeforphilly/bdt/builder/model/domain/Benefit.java @@ -10,6 +10,7 @@ public class Benefit { private String id; private String description; private String name; + @Valid private List checks; private String ownerId; @Valid diff --git a/builder-api/src/main/java/org/codeforphilly/bdt/builder/model/domain/CheckConfig.java b/builder-api/src/main/java/org/codeforphilly/bdt/builder/model/domain/CheckConfig.java index 49e198a7..423e0633 100644 --- a/builder-api/src/main/java/org/codeforphilly/bdt/builder/model/domain/CheckConfig.java +++ b/builder-api/src/main/java/org/codeforphilly/bdt/builder/model/domain/CheckConfig.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.databind.JsonNode; +import jakarta.validation.constraints.Size; import java.util.List; import java.util.Map; @@ -21,6 +22,9 @@ public class CheckConfig { private List parameterDefinitions; // optional alias name for this check instance private String aliasName; + // Plain-language guidance for the person being screened, per configured check. + @Size(max = 2000) + private String explanation; // true when aliasName came from alias generation and hasn't been hand-edited, // so it can be regenerated when parameters change private boolean aliasGenerated; @@ -139,6 +143,14 @@ public void setAliasName(String aliasName) { this.aliasName = aliasName; } + public String getExplanation() { + return explanation; + } + + public void setExplanation(String explanation) { + this.explanation = explanation == null || explanation.isBlank() ? null : explanation.trim(); + } + public boolean isAliasGenerated() { return aliasGenerated; } diff --git a/builder-api/src/main/java/org/codeforphilly/bdt/builder/model/dto/custombenefit/UpdateCheckExplanationRequest.java b/builder-api/src/main/java/org/codeforphilly/bdt/builder/model/dto/custombenefit/UpdateCheckExplanationRequest.java new file mode 100644 index 00000000..63e585d2 --- /dev/null +++ b/builder-api/src/main/java/org/codeforphilly/bdt/builder/model/dto/custombenefit/UpdateCheckExplanationRequest.java @@ -0,0 +1,5 @@ +package org.codeforphilly.bdt.builder.model.dto.custombenefit; + +import jakarta.validation.constraints.Size; + +public record UpdateCheckExplanationRequest(@Size(max = 2000) String explanation) {} diff --git a/builder-api/src/test/java/org/codeforphilly/bdt/builder/api/validation/CheckExplanationTest.java b/builder-api/src/test/java/org/codeforphilly/bdt/builder/api/validation/CheckExplanationTest.java new file mode 100644 index 00000000..33e71c91 --- /dev/null +++ b/builder-api/src/test/java/org/codeforphilly/bdt/builder/api/validation/CheckExplanationTest.java @@ -0,0 +1,45 @@ +package org.codeforphilly.bdt.builder.api.validation; + +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.validation.Validation; +import org.codeforphilly.bdt.builder.model.domain.Benefit; +import org.codeforphilly.bdt.builder.model.domain.CheckConfig; +import org.codeforphilly.bdt.builder.model.dto.custombenefit.UpdateCheckExplanationRequest; +import org.codeforphilly.bdt.builder.model.dto.screener.ScreenerTransfer; +import org.junit.jupiter.api.Test; +import java.util.List; +import java.util.Map; +import static org.junit.jupiter.api.Assertions.*; + +class CheckExplanationTest { + @Test + void explanationSurvivesBenefitStorageAndOlderBenefitsStillLoad() throws Exception { + var mapper = new ObjectMapper(); + var check = new CheckConfig(); + check.setCheckId("instance"); + check.setAliasName("Income limit"); + check.setExplanation("Your income must be within the program limit."); + var benefit = new Benefit("benefit", "Benefit", "", "owner", List.of(check)); + // Working/published storage and benefit copies use this same Jackson mapping. + var stored = mapper.convertValue(benefit, Map.class); + var restored = mapper.convertValue(stored, Benefit.class); + assertEquals(check.getExplanation(), restored.getChecks().getFirst().getExplanation()); + assertNull(mapper.readValue("{\"checkId\":\"legacy\"}", CheckConfig.class).getExplanation()); + } + + @Test + void validatesOptionalExplanationLength() { + try (var factory = Validation.buildDefaultValidatorFactory()) { + var validator = factory.getValidator(); + assertTrue(validator.validate(new UpdateCheckExplanationRequest(null)).isEmpty()); + assertTrue(validator.validate(new UpdateCheckExplanationRequest("x".repeat(2000))).isEmpty()); + assertEquals(1, validator.validate(new UpdateCheckExplanationRequest("x".repeat(2001))).size()); + var check = new CheckConfig(); + check.setExplanation("x".repeat(2001)); + var benefit = new Benefit("id", "Benefit", "", "owner", List.of(check)); + var transfer = new ScreenerTransfer(ScreenerTransfer.FORMAT, ScreenerTransfer.VERSION, + "Screener", null, List.of(benefit), List.of()); + assertEquals(1, validator.validate(transfer).size()); + } + } +} diff --git a/builder-api/src/test/java/org/codeforphilly/bdt/builder/controller/CustomBenefitResourceTest.java b/builder-api/src/test/java/org/codeforphilly/bdt/builder/controller/CustomBenefitResourceTest.java index 72afb7fe..23fa9739 100644 --- a/builder-api/src/test/java/org/codeforphilly/bdt/builder/controller/CustomBenefitResourceTest.java +++ b/builder-api/src/test/java/org/codeforphilly/bdt/builder/controller/CustomBenefitResourceTest.java @@ -12,6 +12,7 @@ import org.codeforphilly.bdt.builder.model.dto.custombenefit.UpdateCustomBenefitRequest; import org.codeforphilly.bdt.builder.model.dto.custombenefit.AddCheckRequest; import org.codeforphilly.bdt.builder.model.dto.custombenefit.UpdateCheckAliasRequest; +import org.codeforphilly.bdt.builder.model.dto.custombenefit.UpdateCheckExplanationRequest; import org.codeforphilly.bdt.builder.model.dto.custombenefit.UpdateCheckParametersRequest; import org.codeforphilly.bdt.builder.persistence.EligibilityCheckRepository; import org.codeforphilly.bdt.builder.persistence.ScreenerRepository; @@ -307,6 +308,42 @@ void updateCheckAliasRecordsWhetherTheAliasWasGenerated() throws Exception { assertFalse(check.isAliasGenerated()); } + @Test + void explanationIsScopedToTheConfiguredCheckAndCanBeCleared() throws Exception { + CheckConfig check = configuredIncomeCheck("My income check", false); + CheckConfig other = configuredIncomeCheck("Other check", false); + other.setCheckId("other-instance"); + other.setExplanation("Other explanation"); + Benefit benefit = new Benefit(BENEFIT_ID, "Benefit", "", USER_ID, List.of(check, other)); + when(screenerRepository.getCustomBenefit(SCREENER_ID, BENEFIT_ID)).thenReturn(Optional.of(benefit)); + + Response saved = resource.updateCheckExplanation(identity, SCREENER_ID, BENEFIT_ID, "configured-check", + new UpdateCheckExplanationRequest(" Your household income must be below $50,000. ")); + assertEquals(200, saved.getStatus()); + assertEquals("Your household income must be below $50,000.", check.getExplanation()); + assertEquals("Other explanation", other.getExplanation()); + assertEquals("My income check", check.getAliasName()); + assertEquals(Map.of("limit", 50_000), check.getParameters()); + verify(screenerRepository).updateCustomBenefit(SCREENER_ID, benefit); + + assertEquals(200, resource.updateCheckExplanation(identity, SCREENER_ID, BENEFIT_ID, "configured-check", + new UpdateCheckExplanationRequest(" ")).getStatus()); + assertNull(check.getExplanation()); + } + + @Test + void explanationUpdateRequiresOwnershipAndAnExistingCheck() throws Exception { + when(screenerRepository.getCustomBenefit(SCREENER_ID, BENEFIT_ID)).thenReturn(Optional.of( + new Benefit(BENEFIT_ID, "Benefit", "", USER_ID, List.of()))); + assertEquals(404, resource.updateCheckExplanation(identity, SCREENER_ID, BENEFIT_ID, "missing", + new UpdateCheckExplanationRequest("Text")).getStatus()); + assertEquals(404, resource.updateCheckExplanation(identity, SCREENER_ID, "missing", "missing", + new UpdateCheckExplanationRequest("Text")).getStatus()); + assertEquals(401, resource.updateCheckExplanation(identity, "not-owned", BENEFIT_ID, "configured-check", + new UpdateCheckExplanationRequest("Text")).getStatus()); + verify(screenerRepository, never()).updateCustomBenefit(any(), any()); + } + private static CheckConfig configuredIncomeCheck(String aliasName, boolean aliasGenerated) { CheckConfig check = new CheckConfig(); check.setCheckId("configured-check"); diff --git a/builder-api/src/test/java/org/codeforphilly/bdt/builder/controller/DecisionResourceTest.java b/builder-api/src/test/java/org/codeforphilly/bdt/builder/controller/DecisionResourceTest.java index 73599a29..2752efbf 100644 --- a/builder-api/src/test/java/org/codeforphilly/bdt/builder/controller/DecisionResourceTest.java +++ b/builder-api/src/test/java/org/codeforphilly/bdt/builder/controller/DecisionResourceTest.java @@ -39,6 +39,7 @@ void evaluatePublishedScreener_includesEachChecksQuestionVotes() throws Exceptio CheckConfig check = new CheckConfig(); check.setCheckId("income-check"); check.setCheckName("IncomeCheck"); + check.setExplanation("Your income must be within the program limit."); check.setParameters(Map.of()); check.setInputDefinition(new ObjectMapper().readTree(""" { @@ -77,5 +78,6 @@ void evaluatePublishedScreener_includesEachChecksQuestionVotes() throws Exceptio Map checkResults = (Map) benefitResult.get("check_results"); Map checkResult = (Map) checkResults.get("income-check0"); assertEquals(List.of("custom.householdIncome"), checkResult.get("inputPaths")); + assertEquals("Your income must be within the program limit.", checkResult.get("explanation")); } } diff --git a/builder-frontend/README.md b/builder-frontend/README.md index 64c0f3bc..e793cb7b 100644 --- a/builder-frontend/README.md +++ b/builder-frontend/README.md @@ -123,3 +123,17 @@ answers in the results snapshot; answers and technical input paths are never printed by this feature. If an input has no static form label, Next steps keeps its general need-more-information message rather than inventing a question. The CRM `nextSteps` extension continues to contain decided benefits only. + +### Check explanations + +Each configured check on a benefit can have an optional plain-language explanation, +separate from its technical description and alias. In Configure Benefit, choose +**Edit explanation** beside the alias. Explanations are plain text (up to 2,000 +characters); clearing the field removes one. They belong to that check instance, +so the same rule can have different explanations for different benefits. + +Published and preview results show an information button for checks with saved +explanations. Hover, focus with the keyboard, or tap to read it; Escape dismisses +the tooltip. Its text is associated with the button for screen readers. Failed +checks use the explanation in **Requirements not met**, including print summaries +and CRM Next steps. Checks without one keep their alias or original name. diff --git a/builder-frontend/src/api/benefit.ts b/builder-frontend/src/api/benefit.ts index 89dbf164..11629341 100644 --- a/builder-frontend/src/api/benefit.ts +++ b/builder-frontend/src/api/benefit.ts @@ -179,3 +179,17 @@ export const generateCheckAlias = async ( const data = (await response.json()) as { aliasName: string }; return data.aliasName; }; + +export const updateCheckExplanation = async ( + screenerId: string, + benefitId: string, + checkId: string, + explanation: string | null, +): Promise => { + const response = await authPatch( + `${apiUrl}/screener/${screenerId}/benefit/${benefitId}/check/${checkId}/explanation`, + { explanation }, + ); + if (!response.ok) + throw await apiErrorFrom(response, "Update explanation failed"); +}; diff --git a/builder-frontend/src/components/resultsAliases.test.tsx b/builder-frontend/src/components/resultsAliases.test.tsx index bc9c4edb..1c7a3b4b 100644 --- a/builder-frontend/src/components/resultsAliases.test.tsx +++ b/builder-frontend/src/components/resultsAliases.test.tsx @@ -15,6 +15,7 @@ const results: ScreenerResult = { check: { name: "OriginalCheckName", aliasName: "ApplicantAgeRequirement", + explanation: "You must be at least 65. Plain text", result: "TRUE", module: "age", version: "1.0.0", @@ -55,4 +56,56 @@ describe("eligibility result aliases", () => { expect(container.textContent).toContain("Applicant Age Requirement"); }); + it.each(["published", "preview"])( + "provides keyboard and screen-reader explanations in %s results", + (mode) => { + const container = document.createElement("div"); + document.body.appendChild(container); + dispose = render( + () => + mode === "published" ? ( + results} /> + ) : ( + ({})} + results={() => results} + resultsLoading={() => false} + /> + ), + container, + ); + const trigger = container.querySelector( + 'button[aria-label="About Applicant Age Requirement"]', + )!; + const tooltip = container.querySelector('[role="tooltip"]')!; + expect(trigger.getAttribute("aria-describedby")).toBe(tooltip.id); + expect(tooltip.hidden).toBe(true); + trigger.focus(); + expect(tooltip.hidden).toBe(false); + expect(tooltip.textContent).toBe( + "You must be at least 65. Plain text", + ); + expect(tooltip.querySelector("b")).toBeNull(); + trigger.dispatchEvent( + new KeyboardEvent("keydown", { key: "Escape", bubbles: true }), + ); + expect(tooltip.hidden).toBe(true); + expect(document.activeElement).toBe(trigger); + trigger.blur(); + trigger.focus(); + expect(tooltip.hidden).toBe(false); + container.remove(); + }, + ); + + it("omits tooltip buttons for checks without explanations", () => { + const container = document.createElement("div"); + const without = structuredClone(results); + without.benefit.check_results.check.explanation = " "; + dispose = render( + () => without} />, + container, + ); + expect(container.querySelector("button")).toBeNull(); + }); }); diff --git a/builder-frontend/src/components/screener/EligibilityResults.tsx b/builder-frontend/src/components/screener/EligibilityResults.tsx index d1d93f0c..123cbdc7 100644 --- a/builder-frontend/src/components/screener/EligibilityResults.tsx +++ b/builder-frontend/src/components/screener/EligibilityResults.tsx @@ -2,6 +2,8 @@ import { Switch, Match, For, Accessor, Show } from "solid-js"; import type { ScreenerResult, BenefitResult } from "@/types"; +import CheckExplanation from "@/components/shared/CheckExplanation"; + import checkIcon from "@/assets/images/checkIcon.svg"; import questionIcon from "@/assets/images/questionIcon.svg"; import xIcon from "@/assets/images/xIcon.svg"; @@ -63,6 +65,12 @@ function BenefitResult({ benefitResult }: { benefitResult: BenefitResult }) {
{check.aliasName ? titleCase(check.aliasName) : check.name} +
diff --git a/builder-frontend/src/components/screenerEditor/formJsExtensions/nextSteps/content.ts b/builder-frontend/src/components/screenerEditor/formJsExtensions/nextSteps/content.ts index 6efb80f6..57aaa306 100644 --- a/builder-frontend/src/components/screenerEditor/formJsExtensions/nextSteps/content.ts +++ b/builder-frontend/src/components/screenerEditor/formJsExtensions/nextSteps/content.ts @@ -93,7 +93,10 @@ export function benefitNextSteps( body: null, unmetRequirements: Object.values(benefit.check_results ?? {}) .filter((check) => check.result === "FALSE") - .map((check) => check.aliasName?.trim() || check.name) + .map( + (check) => + check.explanation?.trim() || check.aliasName?.trim() || check.name, + ) // An unnamed check has nothing to show. .filter((name): name is string => !!name?.trim()), note: markdown(content?.ineligible?.note), diff --git a/builder-frontend/src/components/screenerEditor/manageBenefits/configureBenefit/ConfigureBenefit.tsx b/builder-frontend/src/components/screenerEditor/manageBenefits/configureBenefit/ConfigureBenefit.tsx index a3430392..94d2416c 100644 --- a/builder-frontend/src/components/screenerEditor/manageBenefits/configureBenefit/ConfigureBenefit.tsx +++ b/builder-frontend/src/components/screenerEditor/manageBenefits/configureBenefit/ConfigureBenefit.tsx @@ -120,6 +120,12 @@ const ConfigureBenefit = ({ aliasGenerated, ); }} + updateCheckConfigExplanation={(explanation) => + actions.updateCheckConfigExplanation( + checkConfig.checkId, + explanation, + ) + } generateCheckConfigAlias={() => actions.generateCheckConfigAlias( checkConfig.checkId, diff --git a/builder-frontend/src/components/screenerEditor/manageBenefits/configureBenefit/SelectedEligibilityCheck.tsx b/builder-frontend/src/components/screenerEditor/manageBenefits/configureBenefit/SelectedEligibilityCheck.tsx index 3b93a686..a5e5b874 100644 --- a/builder-frontend/src/components/screenerEditor/manageBenefits/configureBenefit/SelectedEligibilityCheck.tsx +++ b/builder-frontend/src/components/screenerEditor/manageBenefits/configureBenefit/SelectedEligibilityCheck.tsx @@ -1,6 +1,7 @@ import { Accessor, createSignal, For, Show } from "solid-js"; import ConfigureCheckModal from "./modals/ConfigureCheckModal"; +import EditExplanationModal from "./modals/EditExplanationModal"; import EditAliasModal from "./modals/EditAliasModal"; import { titleCase } from "@/utils/title_case"; @@ -18,6 +19,7 @@ const SelectedEligibilityCheck = ({ updateCheckConfigParams, updateCheckConfigAlias, generateCheckConfigAlias, + updateCheckConfigExplanation, }: { checkConfig: Accessor; updateCheckConfigParams: (newCheckData: ParameterValues) => void; @@ -25,6 +27,7 @@ const SelectedEligibilityCheck = ({ updateCheckConfigAlias: | ((aliasName: string | null, aliasGenerated: boolean) => void) | null; + updateCheckConfigExplanation?: (explanation: string | null) => Promise; generateCheckConfigAlias?: () => Promise; onRemove: () => void | null; }) => { @@ -32,6 +35,8 @@ const SelectedEligibilityCheck = ({ createSignal(false); const [editAliasModalOpen, setEditAliasModalOpen] = createSignal(false); + const [editExplanationOpen, setEditExplanationOpen] = createSignal(false); + const displayName = () => checkConfig().aliasName || checkConfig().checkName; const isBlankParameterValue = (value: unknown) => { @@ -88,7 +93,24 @@ const SelectedEligibilityCheck = ({ + + + + +

+ Explanation: {checkConfig().explanation} +

+
Original: {checkConfig().checkName} @@ -139,6 +161,15 @@ const SelectedEligibilityCheck = ({ )}
+ + {(save) => ( + setEditExplanationOpen(false)} + /> + )} + {configuringCheckModalOpen() && ( void; + updateCheckConfigExplanation: ( + checkId: string, + explanation: string | null, + ) => Promise; generateCheckConfigAlias: (checkId: string) => Promise; updateNextSteps: (nextSteps: NextSteps) => Promise; }; @@ -154,6 +159,19 @@ const createScreenerBenefits = ( setActionInProgress(false); }; + const updateCheckConfigExplanation = async ( + checkId: string, + explanation: string | null, + ) => { + await updateCheckExplanation( + screenerId(), + benefitId(), + checkId, + explanation, + ); + await refetch(); + }; + const generateCheckConfigAlias = (checkId: string) => generateCheckAlias(screenerId(), benefitId(), checkId); @@ -188,6 +206,7 @@ const createScreenerBenefits = ( updateCheckConfigParams, updateCheckConfigAlias, generateCheckConfigAlias, + updateCheckConfigExplanation, updateNextSteps, }, actionInProgress, diff --git a/builder-frontend/src/components/screenerEditor/manageBenefits/configureBenefit/modals/EditExplanationModal.test.tsx b/builder-frontend/src/components/screenerEditor/manageBenefits/configureBenefit/modals/EditExplanationModal.test.tsx new file mode 100644 index 00000000..9a47d4aa --- /dev/null +++ b/builder-frontend/src/components/screenerEditor/manageBenefits/configureBenefit/modals/EditExplanationModal.test.tsx @@ -0,0 +1,51 @@ +// @vitest-environment jsdom +import { render } from "solid-js/web"; +import { afterEach, expect, it, vi } from "vitest"; +import EditExplanationModal from "./EditExplanationModal"; +import type { CheckConfig } from "@/types"; + +let dispose: () => void; +afterEach(() => { + dispose?.(); + document.body.replaceChildren(); +}); + +it("keeps a failed save editable, retries, and supports clearing the explanation", async () => { + const save = vi + .fn() + .mockRejectedValueOnce(new Error("offline")) + .mockResolvedValue(undefined); + const close = vi.fn(); + dispose = render( + () => ( + + ({ checkName: "Income", explanation: "Saved text" }) as CheckConfig + } + save={save} + closeModal={close} + /> + ), + document.body, + ); + const textarea = document.querySelector("textarea")!; + expect(textarea.value).toBe("Saved text"); + textarea.value = " Your income must be below the limit. "; + textarea.dispatchEvent(new InputEvent("input", { bubbles: true })); + const submit = () => + document + .querySelector("form")! + .dispatchEvent(new Event("submit", { bubbles: true, cancelable: true })); + submit(); + await vi.waitFor(() => + expect(document.querySelector('[role="alert"]')).not.toBeNull(), + ); + expect(close).not.toHaveBeenCalled(); + expect(textarea.value).toBe(" Your income must be below the limit. "); + expect(save).toHaveBeenLastCalledWith("Your income must be below the limit."); + textarea.value = " "; + textarea.dispatchEvent(new InputEvent("input", { bubbles: true })); + submit(); + await vi.waitFor(() => expect(close).toHaveBeenCalledOnce()); + expect(save).toHaveBeenLastCalledWith(null); +}); diff --git a/builder-frontend/src/components/screenerEditor/manageBenefits/configureBenefit/modals/EditExplanationModal.tsx b/builder-frontend/src/components/screenerEditor/manageBenefits/configureBenefit/modals/EditExplanationModal.tsx new file mode 100644 index 00000000..38733ae8 --- /dev/null +++ b/builder-frontend/src/components/screenerEditor/manageBenefits/configureBenefit/modals/EditExplanationModal.tsx @@ -0,0 +1,78 @@ +import { Accessor, createSignal, createUniqueId, Show } from "solid-js"; +import { Modal } from "@/components/shared/Modal"; +import type { CheckConfig } from "@/types"; + +export default function EditExplanationModal(props: { + checkConfig: Accessor; + save: (explanation: string | null) => Promise; + closeModal: () => void; +}) { + const id = createUniqueId(); + const [draft, setDraft] = createSignal(props.checkConfig().explanation ?? ""); + const [saving, setSaving] = createSignal(false); + const [error, setError] = createSignal(""); + const save = async (event: SubmitEvent) => { + event.preventDefault(); + if (saving()) return; + setSaving(true); + setError(""); + try { + await props.save(draft().trim() || null); + props.closeModal(); + } catch { + setError( + "Could not save the explanation. Your edits are still here. Please try again.", + ); + } finally { + setSaving(false); + } + }; + return ( + +
+

Edit explanation

+

+ Explain this requirement in plain language for the person being + screened. It appears in the results and in Requirements not met. Leave + empty to remove it. +

+ +