Skip to content

Commit

Permalink
Refactor changeCheckbox function
Browse files Browse the repository at this point in the history
  • Loading branch information
jamdelion committed Dec 17, 2024
1 parent 3d3c9c7 commit 9585660
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
35 changes: 21 additions & 14 deletions editor.planx.uk/src/@planx/components/Checklist/Public/Public.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import { Props } from "../types";
import { AutoAnsweredChecklist } from "./AutoAnsweredChecklist";
import {
getInitialExpandedGroups,
toggleCheckbox,
toggleInArray,
toggleNonExclusiveCheckbox,
} from "./helpers";

export enum ChecklistLayout {
Expand Down Expand Up @@ -85,6 +85,11 @@ const VisibleChecklist: React.FC<Props> = (props) => {
}),
});

const setCheckedFieldValue = (optionIds: string[]) => {
const sortedCheckedIds = sortCheckedIds(optionIds);
formik.setFieldValue("checked", sortedCheckedIds);
};

const initialExpandedGroups = getInitialExpandedGroups(
groupedOptions,
previouslySubmittedData
Expand Down Expand Up @@ -112,25 +117,27 @@ const VisibleChecklist: React.FC<Props> = (props) => {
const exclusiveOptionIsChecked =
exclusiveOrOption && formik.values.checked.includes(exclusiveOrOption.id);

const toggleExclusiveCheckbox = (checkboxId: string) => {
return exclusiveOptionIsChecked ? [] : [checkboxId];
};

const changeCheckbox = (id: string) => () => {
const currentIds = formik.values.checked;
let newCheckedIds;
const currentCheckedIds = formik.values.checked;

const currentCheckboxIsExclusiveOption =
exclusiveOrOption && id === exclusiveOrOption.id;

if (currentCheckboxIsExclusiveOption) {
newCheckedIds = exclusiveOptionIsChecked ? [] : [id];
} else if (exclusiveOrOption) {
newCheckedIds = toggleCheckbox(id, currentIds);
const onlyNonExclusiveOptions = newCheckedIds.filter(
(id: string) => exclusiveOrOption && id !== exclusiveOrOption.id
);
newCheckedIds = onlyNonExclusiveOptions;
} else {
newCheckedIds = toggleCheckbox(id, currentIds);
const newCheckedIds = toggleExclusiveCheckbox(id);
setCheckedFieldValue(newCheckedIds);
return;
}
const sortedCheckedIds = sortCheckedIds(newCheckedIds);
formik.setFieldValue("checked", sortedCheckedIds);
const newCheckedIds = toggleNonExclusiveCheckbox(
id,
currentCheckedIds,
exclusiveOrOption
);
setCheckedFieldValue(newCheckedIds);
};

return (
Expand Down
24 changes: 19 additions & 5 deletions editor.planx.uk/src/@planx/components/Checklist/Public/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@ export function toggleInArray<T>(value: T, arr: Array<T>): Array<T> {

export function groupHasOptionSelected(
group: Group<Option>,
answers: string[],
answers: string[]
) {
return group.children.some((child) => answers.some((id) => child.id === id));
}

export function getInitialExpandedGroups(
groupedOptions?: Array<Group<Option>>,
previouslySubmittedData?: Store.UserData,
previouslySubmittedData?: Store.UserData
) {
return (groupedOptions ?? ([] as Group<Option>[])).reduce(
(acc, group, index) =>
groupHasOptionSelected(group, previouslySubmittedData?.answers ?? [])
? [...acc, index]
: acc,
[] as number[],
[] as number[]
);
}

export const toggleCheckbox = (
thisCheckboxId: string,
currentlyCheckedOptionIds: string[],
currentlyCheckedOptionIds: string[]
) => {
const toggleOff = currentlyCheckedOptionIds.filter(
(id) => id !== thisCheckboxId,
(id) => id !== thisCheckboxId
);

const toggleOn = [...currentlyCheckedOptionIds, thisCheckboxId];
Expand All @@ -43,3 +43,17 @@ export const toggleCheckbox = (
? toggleOff
: toggleOn;
};

export const toggleNonExclusiveCheckbox = (
thisCheckboxId: string,
currentlyCheckedOptionIds: string[],
exclusiveOrOption: Option
) => {
const newCheckedOptionIds = toggleCheckbox(
thisCheckboxId,
currentlyCheckedOptionIds
);
return newCheckedOptionIds.filter(
(id: string) => exclusiveOrOption && id !== exclusiveOrOption.id
);
};

0 comments on commit 9585660

Please sign in to comment.