Skip to content

Commit

Permalink
refactor: Tidy up types, use switch
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed May 2, 2024
1 parent 3574d88 commit ff7f22e
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions editor.planx.uk/src/pages/FlowEditor/lib/store/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { SetValue } from "@planx/components/SetValue/model";
import { sortIdsDepthFirst } from "@planx/graph";
import { logger } from "airbrake";
import { objectWithoutNullishValues } from "lib/objectHelpers";
import { castArray } from "lodash";
import difference from "lodash/difference";
import flatten from "lodash/flatten";
import isEqual from "lodash/isEqual";
Expand Down Expand Up @@ -843,22 +844,36 @@ const handleSetValue = (
previousValues = formatPreviousValues(previousValues);
const currentValue = responseData?.[fn] || [];

if (operation === "removeOne") {
const removeCurrentValue = (val: string | number | boolean) =>
val !== currentValue[0];
const filtered = previousValues.filter(removeCurrentValue);
switch (operation) {
case "replace":
// Default behaviour when assigning passport variables
// No custom logic needed
break;

passport.data![fn] = filtered.length ? filtered : undefined;
}
case "removeOne": {
if (previousValues === currentValue) {
delete passport.data![fn];
}

if (operation === "removeAll") {
delete passport.data![fn];
}
if (Array.isArray(previousValues)) {
const removeCurrentValue = (val: string | number | boolean) =>
val !== currentValue[0];
const filtered = previousValues.filter(removeCurrentValue);
passport.data![fn] = filtered.length ? filtered : undefined;
}

if (operation === "append") {
const combined = [...previousValues, ...currentValue];
break;
}

case "removeAll":
delete passport.data![fn];
break;

passport.data![fn] = combined;
case "append": {
const combined = [...previousValues, ...currentValue];
passport.data![fn] = combined;
break;
}
}

return passport;
Expand Down Expand Up @@ -942,9 +957,9 @@ export const removeNodesDependentOnPassport = (
return { removedNodeIds, breadcrumbsWithoutPassportData: newBreadcrumbs };
};

const formatPreviousValues = (
value: string | number | boolean,
): Array<string | number | boolean> => {
const formatPreviousValues = <T extends string | number | boolean>(
value: T | T[],
): T[] => {
if (!value) return [];
if (Array.isArray(value)) return value;
return [value];
Expand Down

0 comments on commit ff7f22e

Please sign in to comment.