From f6750830cce53f1251e927ce3d676c4b5a1a5d67 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Thu, 7 Sep 2023 18:37:41 +0800 Subject: [PATCH] add new weight options to edit screen --- client/src/pages/spools/edit.tsx | 139 ++++++++++++++++++++++++++++--- 1 file changed, 129 insertions(+), 10 deletions(-) diff --git a/client/src/pages/spools/edit.tsx b/client/src/pages/spools/edit.tsx index d889a6356..917550601 100644 --- a/client/src/pages/spools/edit.tsx +++ b/client/src/pages/spools/edit.tsx @@ -1,7 +1,7 @@ -import React from "react"; +import React, { useEffect, useState } from "react"; import { IResourceComponentsProps, useTranslate } from "@refinedev/core"; import { Edit, useForm, useSelect } from "@refinedev/antd"; -import { Form, Input, DatePicker, Select, InputNumber } from "antd"; +import { Form, Input, DatePicker, Select, InputNumber, Radio } from "antd"; import dayjs from "dayjs"; import TextArea from "antd/es/input/TextArea"; import { IFilament } from "../filaments/model"; @@ -11,7 +11,7 @@ import { numberFormatter, numberParser } from "../../utils/parsing"; export const SpoolEdit: React.FC = () => { const t = useTranslate(); - const { formProps, saveButtonProps } = useForm(); + const { form, formProps, saveButtonProps } = useForm(); const { queryResult } = useSelect({ resource: "filament", @@ -35,14 +35,60 @@ export const SpoolEdit: React.FC = () => { return { label: label, value: item.id, + weight: item.weight, + spool_weight: item.spool_weight, }; }); filamentOptions?.sort((a, b) => a.label.localeCompare(b.label, undefined, { sensitivity: "base" })); + const [weightToEnter, setWeightToEnter] = useState(1); + const [usedWeight, setUsedWeight] = useState(0); + + const selectedFilamentID = Form.useWatch("filament_id", form); + const selectedFilament = filamentOptions?.find((obj) => { + return obj.value === selectedFilamentID; + }); + const filamentWeight = selectedFilament?.weight || 0; + const spoolWeight = selectedFilament?.spool_weight || 0; + + const filamentChange = (newID: number) => { + const newSelectedFilament = filamentOptions?.find((obj) => { + return obj.value === newID; + }); + const newFilamentWeight = newSelectedFilament?.weight || 0; + const newSpoolWeight = newSelectedFilament?.spool_weight || 0; + + if (weightToEnter >= 3) { + if (!(newFilamentWeight && newSpoolWeight)) { + setWeightToEnter(2); + } + } + if (weightToEnter >= 2) { + if (!newFilamentWeight) { + setWeightToEnter(1); + } + } + }; + + const weightChange = (weight: number) => { + setUsedWeight(weight); + form.setFieldsValue({ + used_weight: weight, + }); + }; + if (formProps.initialValues) { formProps.initialValues["filament_id"] = formProps.initialValues["filament"].id; } + useEffect(() => { + if (formProps.initialValues && usedWeight != formProps.initialValues["used_weight"]) { + setUsedWeight(formProps.initialValues["used_weight"]) + } + }, []); + + console.log(usedWeight) + return (
@@ -114,25 +160,98 @@ export const SpoolEdit: React.FC = () => { filterOption={(input, option) => typeof option?.label === "string" && option?.label.toLowerCase().includes(input.toLowerCase()) } + onChange={(value) => { + filamentChange(value); + }} /> + + + + { + setWeightToEnter(value.target.value); + }} + defaultValue={1} + value={weightToEnter} + > + + {t("spool.fields.used_weight")} + + + {t("spool.fields.remaining_weight")} + + + {t("spool.fields.measured_weight")} + + + + + { + weightChange(value ?? 0); + }} + /> + + + { + weightChange(filamentWeight - (value ?? 0)); + }} + /> + + { + weightChange(filamentWeight - ((value ?? 0) - spoolWeight)); + }} />