Skip to content

Commit

Permalink
fix: Updates next action to not include rules on hidden elements (#4110)
Browse files Browse the repository at this point in the history
Updates next action to not include rules on hidden elements
  • Loading branch information
thiessenp-cds authored Jul 30, 2024
1 parent db1619a commit 6f61f1c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/formContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ export const getElementIdsAsNumber = (elements: string[]) => {
return elements.map((element) => Number(element));
};

// TODO: rename to filterResponsesByShownElements
export const filterValuesByShownElements = (values: Responses, elementsShown: FormElement[]) => {
if (!values || !Array.isArray(elementsShown)) {
return values;
Expand Down
22 changes: 21 additions & 1 deletion lib/hooks/useGCFormContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
idArraysMatch,
GroupsType,
getNextAction,
filterShownElements,
filterValuesByShownElements,
} from "@lib/formContext";
import { LockedSections } from "@formBuilder/components/shared/right-panel/treeview/types";
import { formHasGroups } from "@lib/utils/form-builder/formHasGroups";
Expand All @@ -15,6 +17,7 @@ import {
pushIdToHistory as _pushIdToHistory,
clearHistoryAfterId as _clearHistoryAfterId,
getPreviousIdFromCurrentId,
getInputHistoryValues,
} from "@lib/utils/form-builder/groupsHistory";
import { getLocalizedProperty } from "@lib/utils";
import { Language } from "@lib/types/form-builder-types";
Expand All @@ -23,6 +26,7 @@ interface GCFormsContextValueType {
updateValues: ({ formValues }: { formValues: FormValues }) => void;
getValues: () => FormValues;
matchedIds: string[];
filteredMatchedIds: string[];
groups?: GroupsType;
currentGroup: string | null;
previousGroup: string | null;
Expand Down Expand Up @@ -56,6 +60,20 @@ export const GCFormsProvider = ({
const [currentGroup, setCurrentGroup] = React.useState<string | null>(initialGroup);
const [previousGroup, setPreviousGroup] = React.useState<string | null>(initialGroup);

const inputHistoryValues = getInputHistoryValues(
(values.current || []) as FormValues,
(history.current || []) as string[],
groups
);
const shownElements = filterShownElements(formRecord.form.elements, matchedIds as string[]);
const filteredResponses = filterValuesByShownElements(inputHistoryValues, shownElements);
const filteredMatchedIds = matchedIds.filter((id) => {
const parentId = id.split(".")[0];
if (filteredResponses[parentId]) {
return id;
}
});

const hasNextAction = (group: string) => {
return groups[group]?.nextAction ? true : false;
};
Expand All @@ -79,7 +97,7 @@ export const GCFormsProvider = ({
if (!currentGroup) return;

if (hasNextAction(currentGroup)) {
const nextAction = getNextAction(groups, currentGroup, matchedIds);
const nextAction = getNextAction(groups, currentGroup, filteredMatchedIds);

// Helpful for navigating to the last group
setPreviousGroup(currentGroup);
Expand Down Expand Up @@ -149,6 +167,7 @@ export const GCFormsProvider = ({
updateValues,
getValues,
matchedIds,
filteredMatchedIds,
groups,
currentGroup,
previousGroup,
Expand Down Expand Up @@ -181,6 +200,7 @@ export const useGCFormsContext = () => {
return;
},
matchedIds: [""],
filteredMatchedIds: [""],
groups: {},
currentGroup: "",
previousGroup: "",
Expand Down

0 comments on commit 6f61f1c

Please sign in to comment.