From eea74b8a431846ac786eac8c56b2f321e6d90cb4 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sun, 20 Aug 2023 12:00:29 +0800 Subject: [PATCH 1/9] add option to use measured or remaining weight --- client/public/locales/en/common.json | 5 + client/src/pages/spools/create.tsx | 155 ++++++++++++++++++++++++++- spoolman/api/v1/spool.py | 10 +- 3 files changed, 161 insertions(+), 9 deletions(-) diff --git a/client/public/locales/en/common.json b/client/public/locales/en/common.json index 3fe6e44..2661389 100644 --- a/client/public/locales/en/common.json +++ b/client/public/locales/en/common.json @@ -133,8 +133,10 @@ "filament_name": "Filament", "filament": "Filament", "material": "Material", + "weight_to_use": "Weight", "used_weight": "Used Weight", "remaining_weight": "Remaining Weight", + "measured_weight": "Measured Weight", "used_length": "Used Length", "remaining_length": "Remaining Length", "location": "Location", @@ -146,7 +148,10 @@ "archived": "Archived" }, "fields_help": { + "weight_to_use": "Select what weight value to enter. Measured weight is only avalable if the spool weight is set for the selected filament", "used_weight": "How much filament has been used from the spool. A new spool should have 0g used.", + "remaining_weight": "How much filament is left on the spool. For a new spool this should match the spool weight.", + "measured_weight": "How much the filament and spool weigh.", "location": "Where the spool is located if you have multiple locations where you store your spools.", "lot_nr": "Manufacturer's lot number. Can be used to ensure a print has consistent color if multiple spools are used." }, diff --git a/client/src/pages/spools/create.tsx b/client/src/pages/spools/create.tsx index 0cbbc3a..ba6299c 100644 --- a/client/src/pages/spools/create.tsx +++ b/client/src/pages/spools/create.tsx @@ -1,7 +1,7 @@ import React from "react"; import { IResourceComponentsProps, useTranslate } from "@refinedev/core"; import { Create, 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"; @@ -15,7 +15,7 @@ interface CreateOrCloneProps { export const SpoolCreate: React.FC = (props) => { const t = useTranslate(); - const { formProps, saveButtonProps, formLoading } = useForm(); + const { form, formProps, saveButtonProps, formLoading } = useForm(); if (props.mode === "clone" && formProps.initialValues) { // Clear out the values that we don't want to clone @@ -49,10 +49,121 @@ export const SpoolCreate: React.FC a.label.localeCompare(b.label, undefined, { sensitivity: "base" })); + const selected_filament = Form.useWatch('filament_id', form); + const weight_to_use = Form.useWatch('weight_to_use', form); + const used_weight = Form.useWatch('used_weight', form); + const remaining_weight = Form.useWatch('remaining_weight', form); + const measured_weight = Form.useWatch('measured_weight', form); + + const handle_weight = () => { + if (selected_filament) + { + let filament_weight = filamentOptions.find(obj => {return obj.value === selected_filament}).weight; + let spool_weight = filamentOptions.find(obj => {return obj.value === selected_filament}).spool_weight || 0; + if (weight_to_use == 1) + { + form.setFieldsValue( + { + remaining_weight: filament_weight - (used_weight || 0), + measured_weight: filament_weight - (used_weight || 0) + spool_weight + }); + return [true, false, false]; + } + else if (weight_to_use == 2) + { + form.setFieldsValue( + { + used_weight: filament_weight - (remaining_weight || 0), + measured_weight: (remaining_weight || 0) + spool_weight + }); + return [false, true, false]; + } + else if (weight_to_use == 3) + { + form.setFieldsValue( + { + used_weight: filament_weight - ((measured_weight || 0) - spool_weight), + remaining_weight: (measured_weight || 0) - spool_weight + }); + return [false, false, true]; + } +// if (weight_to_use == 0) +// { +// if (used_weight > 0) +// { +// weight_to_use = 1; +// form.setFieldsValue( +// { +// remaining_weight: filament_weight-used_weight +// }); +// return [true, false]; +// } +// else if (remaining_weight > 0) +// { +// form.setFieldsValue( +// { +// used_weight: filament_weight-remaining_weight +// }); +// weight_to_use = 2; +// return [false, true]; +// } +// else +// { +// weight_to_use = 0; +// return [true, true]; +// } +// } +// else if (weight_to_use == 1) +// { +// if (used_weight > 0) +// { +// form.setFieldsValue( +// { +// remaining_weight: filament_weight-used_weight +// }); +// return [true, false]; +// } +// else +// { +// weight_to_use = 0; +// return [true, true]; +// } +// } +// else if (weight_to_use == 2) +// { +// if (used_weight > 0) +// { +// form.setFieldsValue( +// { +// used_weight: filament_weight-remaining_weight +// }); +// return [false, true]; +// } +// else +// { +// weight_to_use = 0; +// return [true, true]; +// } +// } + + } + return [false, false, false]; + } + + const has_spool_weight = () => { + if (selected_filament) + { + return filamentOptions.find(obj => {return obj.value === selected_filament}).spool_weight + } + return false; + } + return ( + + + + {t("spool.fields.used_weight")} + {t("spool.fields.remaining_weight")} + {t("spool.fields.measured_weight")} + - + + + + + + + Date: Mon, 21 Aug 2023 02:40:07 +0800 Subject: [PATCH 2/9] change to hidden field and dummy inputs --- client/src/pages/spools/create.tsx | 209 ++++++++++------------------- spoolman/api/v1/spool.py | 10 +- 2 files changed, 73 insertions(+), 146 deletions(-) diff --git a/client/src/pages/spools/create.tsx b/client/src/pages/spools/create.tsx index ba6299c..4a1dd44 100644 --- a/client/src/pages/spools/create.tsx +++ b/client/src/pages/spools/create.tsx @@ -1,12 +1,13 @@ import React from "react"; import { IResourceComponentsProps, useTranslate } from "@refinedev/core"; import { Create, useForm, useSelect } from "@refinedev/antd"; -import { Form, Input, DatePicker, Select, InputNumber, Radio } from "antd"; +import { Form, Input, DatePicker, Select, InputNumber } from "antd"; import dayjs from "dayjs"; import TextArea from "antd/es/input/TextArea"; import { IFilament } from "../filaments/model"; import { ISpool } from "./model"; import { numberFormatter, numberParser } from "../../utils/parsing"; +import { useSavedState } from "../../utils/saveload"; interface CreateOrCloneProps { mode: "create" | "clone"; @@ -49,120 +50,16 @@ export const SpoolCreate: React.FC a.label.localeCompare(b.label, undefined, { sensitivity: "base" })); - const selected_filament = Form.useWatch('filament_id', form); - const weight_to_use = Form.useWatch('weight_to_use', form); - const used_weight = Form.useWatch('used_weight', form); - const remaining_weight = Form.useWatch('remaining_weight', form); - const measured_weight = Form.useWatch('measured_weight', form); + const [used_weight, set_used_weight] = useSavedState("create-used_weight", 0); - const handle_weight = () => { - if (selected_filament) - { - let filament_weight = filamentOptions.find(obj => {return obj.value === selected_filament}).weight; - let spool_weight = filamentOptions.find(obj => {return obj.value === selected_filament}).spool_weight || 0; - if (weight_to_use == 1) - { - form.setFieldsValue( - { - remaining_weight: filament_weight - (used_weight || 0), - measured_weight: filament_weight - (used_weight || 0) + spool_weight - }); - return [true, false, false]; - } - else if (weight_to_use == 2) - { - form.setFieldsValue( - { - used_weight: filament_weight - (remaining_weight || 0), - measured_weight: (remaining_weight || 0) + spool_weight - }); - return [false, true, false]; - } - else if (weight_to_use == 3) - { - form.setFieldsValue( - { - used_weight: filament_weight - ((measured_weight || 0) - spool_weight), - remaining_weight: (measured_weight || 0) - spool_weight - }); - return [false, false, true]; - } -// if (weight_to_use == 0) -// { -// if (used_weight > 0) -// { -// weight_to_use = 1; -// form.setFieldsValue( -// { -// remaining_weight: filament_weight-used_weight -// }); -// return [true, false]; -// } -// else if (remaining_weight > 0) -// { -// form.setFieldsValue( -// { -// used_weight: filament_weight-remaining_weight -// }); -// weight_to_use = 2; -// return [false, true]; -// } -// else -// { -// weight_to_use = 0; -// return [true, true]; -// } -// } -// else if (weight_to_use == 1) -// { -// if (used_weight > 0) -// { -// form.setFieldsValue( -// { -// remaining_weight: filament_weight-used_weight -// }); -// return [true, false]; -// } -// else -// { -// weight_to_use = 0; -// return [true, true]; -// } -// } -// else if (weight_to_use == 2) -// { -// if (used_weight > 0) -// { -// form.setFieldsValue( -// { -// used_weight: filament_weight-remaining_weight -// }); -// return [false, true]; -// } -// else -// { -// weight_to_use = 0; -// return [true, true]; -// } -// } - - } - return [false, false, false]; - } - - const has_spool_weight = () => { - if (selected_filament) - { - return filamentOptions.find(obj => {return obj.value === selected_filament}).spool_weight - } - return false; - } + const selected_filament_id = Form.useWatch('filament_id', form); + const selected_filament = filamentOptions?.find(obj => {return obj.value === selected_filament_id}); return ( - + - + { + set_used_weight(value ?? 0); + form.setFieldsValue( + { + used_weight: value ?? 0 + }); + }} + /> - + { + set_used_weight((selected_filament?.weight || 0) - (value ?? 0)); + form.setFieldsValue( + { + used_weight: (selected_filament?.weight || 0) - (value ?? 0) + }); + }} + /> - + { + set_used_weight((selected_filament?.weight || 0) - ((value ?? 0) - (selected_filament?.spool_weight || 0))); + form.setFieldsValue( + { + used_weight: (selected_filament?.weight || 0) - ((value ?? 0) - (selected_filament?.spool_weight || 0)) + }); + }} + /> Date: Sat, 26 Aug 2023 01:52:35 +0800 Subject: [PATCH 3/9] address PR comments --- client/src/pages/spools/create.tsx | 87 +++++++++++++++--------------- spoolman/api/v1/spool.py | 8 +-- 2 files changed, 47 insertions(+), 48 deletions(-) diff --git a/client/src/pages/spools/create.tsx b/client/src/pages/spools/create.tsx index 4a1dd44..02f36ae 100644 --- a/client/src/pages/spools/create.tsx +++ b/client/src/pages/spools/create.tsx @@ -1,13 +1,12 @@ -import React from "react"; -import { IResourceComponentsProps, useTranslate } from "@refinedev/core"; -import { Create, useForm, useSelect } from "@refinedev/antd"; -import { Form, Input, DatePicker, Select, InputNumber } from "antd"; +import React, {useState} from "react"; +import {IResourceComponentsProps, useTranslate} from "@refinedev/core"; +import {Create, useForm, useSelect} from "@refinedev/antd"; +import {Form, Input, DatePicker, Select, InputNumber} from "antd"; import dayjs from "dayjs"; import TextArea from "antd/es/input/TextArea"; -import { IFilament } from "../filaments/model"; -import { ISpool } from "./model"; -import { numberFormatter, numberParser } from "../../utils/parsing"; -import { useSavedState } from "../../utils/saveload"; +import {IFilament} from "../filaments/model"; +import {ISpool} from "./model"; +import {numberFormatter, numberParser} from "../../utils/parsing"; interface CreateOrCloneProps { mode: "create" | "clone"; @@ -16,7 +15,7 @@ interface CreateOrCloneProps { export const SpoolCreate: React.FC = (props) => { const t = useTranslate(); - const { form, formProps, saveButtonProps, formLoading } = useForm(); + const {form, formProps, saveButtonProps, formLoading} = useForm(); if (props.mode === "clone" && formProps.initialValues) { // Clear out the values that we don't want to clone @@ -28,7 +27,7 @@ export const SpoolCreate: React.FC({ + const {queryResult} = useSelect({ resource: "filament", }); @@ -50,16 +49,28 @@ export const SpoolCreate: React.FC a.label.localeCompare(b.label, undefined, { sensitivity: "base" })); + filamentOptions?.sort((a, b) => a.label.localeCompare(b.label, undefined, {sensitivity: "base"})); - const [used_weight, set_used_weight] = useSavedState("create-used_weight", 0); + const [usedWeight, setUsedWeight] = useState(0); - const selected_filament_id = Form.useWatch('filament_id', form); - const selected_filament = filamentOptions?.find(obj => {return obj.value === selected_filament_id}); + 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 weightChange = (weight: number) => { + setUsedWeight(weight); + form.setFieldsValue( + { + used_weight: weight + }); + } return ( - + - + @@ -135,17 +146,13 @@ export const SpoolCreate: React.FC { - set_used_weight(value ?? 0); - form.setFieldsValue( - { - used_weight: value ?? 0 - }); + weightChange(value ?? 0); }} /> - { - set_used_weight((selected_filament?.weight || 0) - (value ?? 0)); - form.setFieldsValue( - { - used_weight: (selected_filament?.weight || 0) - (value ?? 0) - }); + weightChange(filamentWeight - (value ?? 0)); }} /> - { - set_used_weight((selected_filament?.weight || 0) - ((value ?? 0) - (selected_filament?.spool_weight || 0))); - form.setFieldsValue( - { - used_weight: (selected_filament?.weight || 0) - ((value ?? 0) - (selected_filament?.spool_weight || 0)) - }); + weightChange(filamentWeight - ((value ?? 0) - spoolWeight)); }} /> @@ -201,7 +200,7 @@ export const SpoolCreate: React.FC - + - + -