Skip to content

Commit

Permalink
refactor: alter error messaging
Browse files Browse the repository at this point in the history
revert: change to sampleResult

refactor: simpler conditional logic

fix: remove console logs
  • Loading branch information
RODO94 committed Oct 15, 2024
1 parent 100dcc9 commit 78f801c
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions editor.planx.uk/src/@planx/components/Calculate/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Typography from "@mui/material/Typography";
import { ComponentType as TYPES } from "@opensystemslab/planx-core/types";
import { EditorProps, ICONS } from "@planx/components/ui";
import { FormikErrors, useFormik } from "formik";
import React, { useEffect, useState } from "react";
import React from "react";
import InputGroup from "ui/editor/InputGroup";
import { ModalFooter } from "ui/editor/ModalFooter";
import ModalSection from "ui/editor/ModalSection";
Expand All @@ -24,8 +24,8 @@ const ConditionLabel = styled("span")(() => ({
textAlign: "center",
}));

const UNKNOWN = "unknown";
export default function Component(props: Props) {
const [sampleResult, setSampleResult] = useState<number>(0);
const formik = useFormik({
initialValues: parseCalculate(props.node?.data),
onSubmit: (newValues) => {
Expand All @@ -46,30 +46,34 @@ export default function Component(props: Props) {
values.defaults,
);

if (typeof result !== "number") {
if (Number.isNaN(Number(result))) {
errors.formula = "Enter a formula which outputs a number";
}
} catch (error: any) {
errors.formula = "Invalid formula: " + error.message;
errors.formula = error.message;
}
return errors;
},

validateOnChange: false,
});

useEffect(() => {
const sampleResult = React.useMemo(() => {
try {
const sampleResult = evaluate(
const result = evaluate(
formik.values.formula,
formik.values.samples,
formik.values.defaults,
);

if (typeof sampleResult === "number") {
setSampleResult(sampleResult);
// Type guard as mathjs evaluates `m` to a "Unit" object for "meter"
if (!Number.isNaN(Number(result))) {
return result;
} else {
return UNKNOWN;
}
} catch (error) {}
} catch (e) {
return UNKNOWN;
}
}, [formik.values.formula, formik.values.defaults, formik.values.samples]);

/**
Expand All @@ -95,8 +99,6 @@ export default function Component(props: Props) {
}
}, [formik.values.formula]);

console.log(sampleResult);

return (
<form onSubmit={formik.handleSubmit} id="modal">
<ModalSection>
Expand Down Expand Up @@ -200,7 +202,7 @@ export default function Component(props: Props) {
)}
<p>
<strong>{formik.values.output || "<output>"}</strong> would be set
to <strong>{sampleResult || 0}</strong>.
to <strong>{sampleResult}</strong>.
</p>
</ModalSectionContent>
</ModalSection>
Expand Down

0 comments on commit 78f801c

Please sign in to comment.