From 743b9b3f3312972997eb14646880a4e24010111f Mon Sep 17 00:00:00 2001 From: Jessica McInchak Date: Fri, 20 Dec 2024 11:55:32 +0100 Subject: [PATCH] flag to flags --- .../@planx/components/Result/Public/index.tsx | 2 +- .../components/shared/BaseOptionsEditor.tsx | 8 +- .../src/@planx/components/shared/index.ts | 2 +- .../components/Flow/components/Option.tsx | 16 ++-- .../FlowEditor/lib/__tests__/filters.test.ts | 8 +- .../__tests__/preview/collectedFlags.test.ts | 92 +------------------ .../removeNodesDependentOnPassport.test.ts | 4 +- .../removeOrphansFromBreadcrumbs.test.ts | 4 +- .../lib/__tests__/preview/resultData.test.ts | 8 +- .../src/pages/FlowEditor/lib/store/preview.ts | 17 +--- 10 files changed, 31 insertions(+), 130 deletions(-) diff --git a/editor.planx.uk/src/@planx/components/Result/Public/index.tsx b/editor.planx.uk/src/@planx/components/Result/Public/index.tsx index ef90adedd8..70a60f0f61 100644 --- a/editor.planx.uk/src/@planx/components/Result/Public/index.tsx +++ b/editor.planx.uk/src/@planx/components/Result/Public/index.tsx @@ -52,7 +52,7 @@ const Responses = ({ if (isAnsweredByUser) return true; // Retain responses with flags which are auto answered - const hasFlag = response.selections.some((s) => s.data?.flag); + const hasFlag = response.selections.some((s) => s.data?.flags); return hasFlag; }; diff --git a/editor.planx.uk/src/@planx/components/shared/BaseOptionsEditor.tsx b/editor.planx.uk/src/@planx/components/shared/BaseOptionsEditor.tsx index 702c843868..91fbcfc2dd 100644 --- a/editor.planx.uk/src/@planx/components/shared/BaseOptionsEditor.tsx +++ b/editor.planx.uk/src/@planx/components/shared/BaseOptionsEditor.tsx @@ -90,17 +90,13 @@ export const BaseOptionsEditor: React.FC = (props) => ( )} { props.onChange({ ...props.value, data: { ...props.value.data, - flag: ev, + flags: ev, }, }); }} diff --git a/editor.planx.uk/src/@planx/components/shared/index.ts b/editor.planx.uk/src/@planx/components/shared/index.ts index 529522b989..06571cc4a3 100644 --- a/editor.planx.uk/src/@planx/components/shared/index.ts +++ b/editor.planx.uk/src/@planx/components/shared/index.ts @@ -28,7 +28,7 @@ export interface Option { id: string; data: { description?: string; - flag?: Array; + flags?: Array; img?: string; text: string; val?: string; diff --git a/editor.planx.uk/src/pages/FlowEditor/components/Flow/components/Option.tsx b/editor.planx.uk/src/pages/FlowEditor/components/Flow/components/Option.tsx index 753ca1f3d6..ed277874ff 100644 --- a/editor.planx.uk/src/pages/FlowEditor/components/Flow/components/Option.tsx +++ b/editor.planx.uk/src/pages/FlowEditor/components/Flow/components/Option.tsx @@ -17,15 +17,11 @@ const Option: React.FC = (props) => { let flags: Flag[] | undefined; try { - // Question & Checklist Options set zero or many flag values under "data.flag" - if (props.data?.flag) { - if (Array.isArray(props.data?.flag)) { - flags = flatFlags.filter( - ({ value }) => props.data?.flag?.includes(value), - ); - } else { - flags = flatFlags.filter(({ value }) => props.data?.flag === value); - } + // Question & Checklist Options set zero or many flag values under "data.flags" + if (props.data?.flags) { + flags = flatFlags.filter( + ({ value }) => props.data?.flags?.includes(value), + ); } // Filter Options set single flag value under "data.val" (Questions & Checklists use this same field for passport values) @@ -35,7 +31,7 @@ const Option: React.FC = (props) => { flags = flatFlags.filter(({ value }) => props.data.val === value); } } - } catch (e) {} + } catch (e) { } return (
  • { resetPreview(); }); - test("Correctly collects flags when an option's `data.flag` prop is the new array format", () => { - setState({ flow: flowWithNewFlags }); + test("Correctly collects flags based on an option's `data.flags` prop", () => { + setState({ flow }); // Manually proceed through a Question that sets multiple flags on a single option clickContinue("Question", { answers: ["YesOption"], auto: false }); @@ -26,34 +26,8 @@ describe("Collecting flags", () => { }); }); - test("Correctly collects flags when an option's `data.flag` prop is the legacy string format", () => { - setState({ flow: flowWithLegacyFlags }); - - // Manually proceed a Checklist and select every option that sets a single flag - clickContinue("Checklist", { - answers: [ - "PPImmuneOption", - "PPImmune2Option", - "PPPDOption", - "WTTRequiredOption", - "MCUYesOption", - ], - auto: false, - }); - - expect(collectedFlags()).toEqual({ - "Community infrastructure levy": [], - "Demolition in a conservation area": [], - "Listed building consent": [], - "Material change of use": ["Material change of use"], - "Planning permission": ["Immune", "Permitted development"], // Multiple flags of same value have been de-deduplicated - "Planning policy": [], - "Works to trees & hedges": ["Required"], - }); - }); - test("Returns empty arrays for each category if no flags have been collected", () => { - setState({ flow: flowWithNewFlags }); + setState({ flow }); // Manually proceed through a Question option without flags clickContinue("Question", { answers: ["NoOption"], auto: false }); @@ -70,7 +44,7 @@ describe("Collecting flags", () => { }); }); -const flowWithNewFlags: Store.Flow = { +const flow: Store.Flow = { _root: { edges: ["Question"], }, @@ -86,7 +60,7 @@ const flowWithNewFlags: Store.Flow = { type: 200, data: { text: "Yes", - flag: ["PP-NOTICE", "EDGE_CASE", "CO_RELIEF", "PRIOR_APPROVAL"], // `flag` is an array for freshly created/updated Question & Checklist options + flags: ["PP-NOTICE", "EDGE_CASE", "CO_RELIEF", "PRIOR_APPROVAL"], }, }, NoOption: { @@ -96,59 +70,3 @@ const flowWithNewFlags: Store.Flow = { }, }, }; - -const flowWithLegacyFlags: Store.Flow = { - _root: { - edges: ["Checklist"], - }, - Checklist: { - type: 105, - data: { - allRequired: false, - neverAutoAnswer: false, - text: "Pick up flags?", - }, - edges: [ - "PPImmuneOption", - "PPImmune2Option", - "PPPDOption", - "WTTRequiredOption", - "MCUYesOption", - ], - }, - PPImmuneOption: { - data: { - text: "PP Immune", - flag: "IMMUNE", - }, - type: 200, - }, - PPImmune2Option: { - data: { - text: "PP Immune again", - flag: "IMMUNE", - }, - type: 200, - }, - PPPDOption: { - data: { - text: "PP Permitted dev", - flag: "NO_APP_REQUIRED", - }, - type: 200, - }, - WTTRequiredOption: { - data: { - text: "WTT Required", - flag: "TR-REQUIRED", - }, - type: 200, - }, - MCUYesOption: { - data: { - text: "MCU Yes", - flag: "MCOU_TRUE", - }, - type: 200, - }, -}; diff --git a/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/preview/removeNodesDependentOnPassport.test.ts b/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/preview/removeNodesDependentOnPassport.test.ts index fb0e5d0511..4f92f16c53 100644 --- a/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/preview/removeNodesDependentOnPassport.test.ts +++ b/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/preview/removeNodesDependentOnPassport.test.ts @@ -394,7 +394,7 @@ const mockFlowData = { }, "4FRZMfNlXf": { data: { - flag: "PP-NOT_DEVELOPMENT", + flags: ["PP-NOT_DEVELOPMENT"], text: "Not development", }, type: TYPES.Answer, @@ -428,7 +428,7 @@ const mockFlowData = { }, IzT93uCmyF: { data: { - flag: "PRIOR_APPROVAL", + flags: ["PRIOR_APPROVAL"], text: "Prior", }, type: TYPES.Answer, diff --git a/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/preview/removeOrphansFromBreadcrumbs.test.ts b/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/preview/removeOrphansFromBreadcrumbs.test.ts index 1c9ca91f93..3b8e2612a3 100644 --- a/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/preview/removeOrphansFromBreadcrumbs.test.ts +++ b/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/preview/removeOrphansFromBreadcrumbs.test.ts @@ -79,7 +79,7 @@ const mockFlowData = { }, "4FRZMfNlXf": { data: { - flag: "PP-NOT_DEVELOPMENT", + flags: ["PP-NOT_DEVELOPMENT"], text: "Not development", }, type: 200, @@ -113,7 +113,7 @@ const mockFlowData = { }, IzT93uCmyF: { data: { - flag: "PRIOR_APPROVAL", + flags: ["PRIOR_APPROVAL"], text: "Prior", }, type: 200, diff --git a/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/preview/resultData.test.ts b/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/preview/resultData.test.ts index 5cc4e36b03..bfa9fc5806 100644 --- a/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/preview/resultData.test.ts +++ b/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/preview/resultData.test.ts @@ -143,7 +143,7 @@ const flow: Store.Flow = { }, MaterialOptionNo: { data: { - flag: ["MCOU_FALSE", "NO_APP_REQUIRED"], + flags: ["MCOU_FALSE", "NO_APP_REQUIRED"], text: "No", }, type: 200, @@ -156,7 +156,7 @@ const flow: Store.Flow = { }, ListedBuildingOptionNo: { data: { - flag: ["LB-NOT_REQUIRED"], + flags: ["LB-NOT_REQUIRED"], text: "No", }, type: 200, @@ -205,14 +205,14 @@ const flow: Store.Flow = { }, MaterialOptionYes: { data: { - flag: ["MCOU_TRUE", "PLANNING_PERMISSION_REQUIRED"], + flags: ["MCOU_TRUE", "PLANNING_PERMISSION_REQUIRED"], text: "Yes", }, type: 200, }, ListedBuildingOptionYes: { data: { - flag: ["PLANNING_PERMISSION_REQUIRED", "LB-REQUIRED"], + flags: ["PLANNING_PERMISSION_REQUIRED", "LB-REQUIRED"], text: "Yes", }, type: 200, diff --git a/editor.planx.uk/src/pages/FlowEditor/lib/store/preview.ts b/editor.planx.uk/src/pages/FlowEditor/lib/store/preview.ts index add41d41eb..a47d225908 100644 --- a/editor.planx.uk/src/pages/FlowEditor/lib/store/preview.ts +++ b/editor.planx.uk/src/pages/FlowEditor/lib/store/preview.ts @@ -414,11 +414,8 @@ export const previewStore: StateCreator< })); const hidden = !selections.some( (selection) => - selection.data?.flag && - // Account for both new flag values (array) and legacy flag value (string) - ((Array.isArray(selection.data.flag) && - selection.data.flag.includes(flag?.value)) || - selection.data.flag === flag?.value), + selection.data?.flags && + selection.data.flags.includes(flag?.value) ); return { @@ -934,16 +931,10 @@ const collectedFlagValuesByCategory = ( if (breadcrumb.answers) { breadcrumb.answers.forEach((answerId) => { const node = flow[answerId]; - // Account for both new flag values (array) and legacy flag value (string) - if (node.data?.flag && Array.isArray(node.data.flag)) { - node.data.flag.forEach((flag) => { + if (node.data?.flags) { + node.data.flags.forEach((flag: Flag["value"]) => { if (possibleFlagValues.includes(flag)) collectedFlags.push(flag); }); - } else if ( - node.data?.flag && - possibleFlagValues.includes(node.data.flag) - ) { - collectedFlags.push(node.data.flag); } }); }