Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make val optional in SetValue component #3333

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions editor.planx.uk/src/@planx/components/SetValue/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,20 @@ function SetValueComponent(props: Props) {
/>
</InputRow>
</ModalSectionContent>
<ModalSectionContent title="Field value">
<InputRow>
<Input
required
format="data"
name="val"
value={formik.values.val}
placeholder="value"
onChange={formik.handleChange}
/>
</InputRow>
</ModalSectionContent>
{formik.values.operation !== "removeAll" &&
<ModalSectionContent title="Field value">
<InputRow>
<Input
required
format="data"
name="val"
value={formik.values.val}
placeholder="value"
onChange={formik.handleChange}
/>
</InputRow>
</ModalSectionContent>
}
<ModalSectionContent title="Operation">
<DescriptionText {...formik.values} />
<FormControl component="fieldset">
Expand Down
14 changes: 12 additions & 2 deletions editor.planx.uk/src/@planx/components/SetValue/model.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { MoreInformation, parseMoreInformation } from "../shared";

export interface SetValue extends MoreInformation {
export interface BaseSetValue extends MoreInformation {
fn: string;
}

interface SetValueWithVal extends BaseSetValue {
val: string;
operation: "replace" | "append" | "removeOne" | "removeAll";
operation: "replace" | "append" | "removeOne";
}

interface SetValueWithoutVal extends BaseSetValue {
val?: string;
operation: "removeAll";
}

export type SetValue = SetValueWithVal | SetValueWithoutVal;

export const parseSetValue = (
data: Record<string, any> | undefined,
): SetValue => ({
Expand Down
15 changes: 6 additions & 9 deletions editor.planx.uk/src/@planx/components/SetValue/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type HandleSetValue = (params: {
* Called by computePassport()
*/
export const handleSetValue: HandleSetValue = ({
nodeData: { operation, fn, val: current },
nodeData,
previousValues,
passport,
}) => {
Expand All @@ -31,31 +31,28 @@ export const handleSetValue: HandleSetValue = ({
const previous = formatPreviousValues(previousValues);

const newValues = calculateNewValues({
operation,
nodeData,
previous,
current,
});

if (newValues) {
passport.data![fn] = newValues;
passport.data![nodeData.fn] = newValues;

// Operation has cleared passport value
if (!newValues.length) delete passport.data![fn];
if (!newValues.length) delete passport.data![nodeData.fn];
}

return passport;
};

type CalculateNewValues = (params: {
operation: SetValue["operation"];
nodeData: SetValue;
previous: string[];
current: string;
}) => string | string[] | undefined;

const calculateNewValues: CalculateNewValues = ({
operation,
nodeData: { operation, val: current },
previous,
current,
}) => {
switch (operation) {
case "replace":
Expand Down
Loading