Skip to content

Commit

Permalink
add toggle to optionally format calculate output for automations
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicamcinchak committed Nov 21, 2023
1 parent a0284fe commit 812b88e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
12 changes: 12 additions & 0 deletions editor.planx.uk/src/@planx/components/Calculate/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import InputGroup from "ui/InputGroup";
import InputRow from "ui/InputRow";
import ModalSection from "ui/ModalSection";
import ModalSectionContent from "ui/ModalSectionContent";
import OptionButton from "ui/OptionButton";

import type { Calculate } from "./model";
import { evaluate, getVariables, parseCalculate } from "./model";
Expand Down Expand Up @@ -85,6 +86,17 @@ export default function Component(props: Props) {
onChange={formik.handleChange}
/>
</InputRow>
<OptionButton
selected={formik.values.formatOutputForAutomations}
onClick={() => {
formik.setFieldValue(
"formatOutputForAutomations",
!formik.values.formatOutputForAutomations,
);
}}
>
Format the output to automate a future Question or Checklist only
</OptionButton>
</ModalSectionContent>
<ModalSectionContent title="Formula">
<InputRow>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ export default function Component(props: Props) {
const passport = useStore((state) => state.computePassport().data);

useEffect(() => {
const evaluatedResult = evaluate(props.formula, passport, props.defaults);
props.handleSubmit?.({
...makeData(
props,
evaluate(props.formula, passport, props.defaults),
props.formatOutputForAutomations ? [evaluatedResult.toString()] : evaluatedResult,
props.output,
),
// don't show this component to the user, auto=true required
// for back button to skip past this component when going back
auto: true,
});
}, []);
}, [props, passport]);

return null;
}
4 changes: 3 additions & 1 deletion editor.planx.uk/src/@planx/components/Calculate/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface Calculate extends MoreInformation {
defaults: Record<string, number>;
formula: string;
samples: Record<string, number>;
formatOutputForAutomations?: boolean;
}

export interface Input {
Expand All @@ -22,6 +23,7 @@ export const parseCalculate = (
defaults: data?.defaults || {},
formula: data?.formula || "",
samples: data?.samples || {},
formatOutputForAutomations: data?.formatOutputForAutomations || false,
});

export function getVariables(input: string): Set<string> {
Expand Down Expand Up @@ -82,7 +84,7 @@ export function evaluate(input: string, scope = {}, defaults = {}): number {
}
}

// Serialization is only necessary internally. v
// Serialization is only necessary internally.
// Mathjs can't handle keys with dots in their names e.g. `property.number`
// This complexity should never be exposed to this component's consumers.
function serialize(x: string) {
Expand Down

0 comments on commit 812b88e

Please sign in to comment.