address PR comments

This commit is contained in:
Thomas White
2023-08-26 01:52:35 +08:00
parent 312aad55c0
commit d721e7d827
2 changed files with 47 additions and 48 deletions

View File

@@ -1,13 +1,12 @@
import React from "react"; import React, {useState} from "react";
import { IResourceComponentsProps, useTranslate } from "@refinedev/core"; import {IResourceComponentsProps, useTranslate} from "@refinedev/core";
import { Create, useForm, useSelect } from "@refinedev/antd"; import {Create, useForm, useSelect} from "@refinedev/antd";
import { Form, Input, DatePicker, Select, InputNumber } from "antd"; import {Form, Input, DatePicker, Select, InputNumber} from "antd";
import dayjs from "dayjs"; import dayjs from "dayjs";
import TextArea from "antd/es/input/TextArea"; import TextArea from "antd/es/input/TextArea";
import { IFilament } from "../filaments/model"; import {IFilament} from "../filaments/model";
import { ISpool } from "./model"; import {ISpool} from "./model";
import { numberFormatter, numberParser } from "../../utils/parsing"; import {numberFormatter, numberParser} from "../../utils/parsing";
import { useSavedState } from "../../utils/saveload";
interface CreateOrCloneProps { interface CreateOrCloneProps {
mode: "create" | "clone"; mode: "create" | "clone";
@@ -16,7 +15,7 @@ interface CreateOrCloneProps {
export const SpoolCreate: React.FC<IResourceComponentsProps & CreateOrCloneProps> = (props) => { export const SpoolCreate: React.FC<IResourceComponentsProps & CreateOrCloneProps> = (props) => {
const t = useTranslate(); const t = useTranslate();
const { form, formProps, saveButtonProps, formLoading } = useForm<ISpool>(); const {form, formProps, saveButtonProps, formLoading} = useForm<ISpool>();
if (props.mode === "clone" && formProps.initialValues) { if (props.mode === "clone" && formProps.initialValues) {
// Clear out the values that we don't want to clone // Clear out the values that we don't want to clone
@@ -28,7 +27,7 @@ export const SpoolCreate: React.FC<IResourceComponentsProps & CreateOrCloneProps
formProps.initialValues.filament_id = formProps.initialValues.filament.id; formProps.initialValues.filament_id = formProps.initialValues.filament.id;
} }
const { queryResult } = useSelect<IFilament>({ const {queryResult} = useSelect<IFilament>({
resource: "filament", resource: "filament",
}); });
@@ -50,16 +49,28 @@ export const SpoolCreate: React.FC<IResourceComponentsProps & CreateOrCloneProps
return { return {
label: label, label: label,
value: item.id, value: item.id,
weight: item.weight, weight: item.weight,
spool_weight: item.spool_weight spool_weight: item.spool_weight
}; };
}); });
filamentOptions?.sort((a, b) => 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 selectedFilamentID = Form.useWatch('filament_id', form);
const selected_filament = filamentOptions?.find(obj => {return obj.value === selected_filament_id}); 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 ( return (
<Create <Create
@@ -80,7 +91,7 @@ export const SpoolCreate: React.FC<IResourceComponentsProps & CreateOrCloneProps
value: value ? dayjs(value) : undefined, value: value ? dayjs(value) : undefined,
})} })}
> >
<DatePicker showTime format="YYYY-MM-DD HH:mm:ss" /> <DatePicker showTime format="YYYY-MM-DD HH:mm:ss"/>
</Form.Item> </Form.Item>
<Form.Item <Form.Item
label={t("spool.fields.last_used")} label={t("spool.fields.last_used")}
@@ -94,7 +105,7 @@ export const SpoolCreate: React.FC<IResourceComponentsProps & CreateOrCloneProps
value: value ? dayjs(value) : undefined, value: value ? dayjs(value) : undefined,
})} })}
> >
<DatePicker showTime format="YYYY-MM-DD HH:mm:ss" /> <DatePicker showTime format="YYYY-MM-DD HH:mm:ss"/>
</Form.Item> </Form.Item>
<Form.Item <Form.Item
label={t("spool.fields.filament")} label={t("spool.fields.filament")}
@@ -119,7 +130,7 @@ export const SpoolCreate: React.FC<IResourceComponentsProps & CreateOrCloneProps
initialValue={0} initialValue={0}
> >
<InputNumber <InputNumber
value={used_weight} value={usedWeight}
/> />
</Form.Item> </Form.Item>
@@ -135,17 +146,13 @@ export const SpoolCreate: React.FC<IResourceComponentsProps & CreateOrCloneProps
precision={1} precision={1}
formatter={numberFormatter} formatter={numberFormatter}
parser={numberParser} parser={numberParser}
value={used_weight} value={usedWeight}
onChange={(value) => { onChange={(value) => {
set_used_weight(value ?? 0); weightChange(value ?? 0);
form.setFieldsValue(
{
used_weight: value ?? 0
});
}} }}
/> />
</Form.Item> </Form.Item>
<Form.Item <Form.Item
label={t("spool.fields.remaining_weight")} label={t("spool.fields.remaining_weight")}
help={t("spool.fields_help.remaining_weight")} help={t("spool.fields_help.remaining_weight")}
// name={["remaining_weight"]} // name={["remaining_weight"]}
@@ -157,18 +164,14 @@ export const SpoolCreate: React.FC<IResourceComponentsProps & CreateOrCloneProps
precision={1} precision={1}
formatter={numberFormatter} formatter={numberFormatter}
parser={numberParser} parser={numberParser}
disabled={!(selected_filament?.weight || 0)} disabled={!filamentWeight}
value={(selected_filament?.weight || 0) ? (selected_filament?.weight || 0) - used_weight : 0} value={filamentWeight ? filamentWeight - usedWeight : 0}
onChange={(value) => { onChange={(value) => {
set_used_weight((selected_filament?.weight || 0) - (value ?? 0)); weightChange(filamentWeight - (value ?? 0));
form.setFieldsValue(
{
used_weight: (selected_filament?.weight || 0) - (value ?? 0)
});
}} }}
/> />
</Form.Item> </Form.Item>
<Form.Item <Form.Item
label={t("spool.fields.measured_weight")} label={t("spool.fields.measured_weight")}
help={t("spool.fields_help.measured_weight")} help={t("spool.fields_help.measured_weight")}
// name={["measured_weight"]} // name={["measured_weight"]}
@@ -180,14 +183,10 @@ export const SpoolCreate: React.FC<IResourceComponentsProps & CreateOrCloneProps
precision={1} precision={1}
formatter={numberFormatter} formatter={numberFormatter}
parser={numberParser} parser={numberParser}
disabled={!((selected_filament?.weight || 0) && (selected_filament?.spool_weight || 0))} disabled={!(filamentWeight && spoolWeight)}
value={((selected_filament?.weight || 0) && (selected_filament?.spool_weight || 0)) ? (selected_filament?.weight || 0) - used_weight + (selected_filament?.spool_weight || 0) : 0} value={(filamentWeight && spoolWeight) ? filamentWeight - usedWeight + spoolWeight : 0}
onChange={(value) => { onChange={(value) => {
set_used_weight((selected_filament?.weight || 0) - ((value ?? 0) - (selected_filament?.spool_weight || 0))); weightChange(filamentWeight - ((value ?? 0) - spoolWeight));
form.setFieldsValue(
{
used_weight: (selected_filament?.weight || 0) - ((value ?? 0) - (selected_filament?.spool_weight || 0))
});
}} }}
/> />
</Form.Item> </Form.Item>
@@ -201,7 +200,7 @@ export const SpoolCreate: React.FC<IResourceComponentsProps & CreateOrCloneProps
}, },
]} ]}
> >
<Input maxLength={64} /> <Input maxLength={64}/>
</Form.Item> </Form.Item>
<Form.Item <Form.Item
label={t("spool.fields.lot_nr")} label={t("spool.fields.lot_nr")}
@@ -213,7 +212,7 @@ export const SpoolCreate: React.FC<IResourceComponentsProps & CreateOrCloneProps
}, },
]} ]}
> >
<Input maxLength={64} /> <Input maxLength={64}/>
</Form.Item> </Form.Item>
<Form.Item <Form.Item
label={t("spool.fields.comment")} label={t("spool.fields.comment")}
@@ -224,7 +223,7 @@ export const SpoolCreate: React.FC<IResourceComponentsProps & CreateOrCloneProps
}, },
]} ]}
> >
<TextArea maxLength={1024} /> <TextArea maxLength={1024}/>
</Form.Item> </Form.Item>
</Form> </Form>
</Create> </Create>

View File

@@ -159,10 +159,10 @@ async def create( # noqa: ANN201
body: SpoolParameters, body: SpoolParameters,
): ):
if body.remaining_weight is not None and body.used_weight is not None: if body.remaining_weight is not None and body.used_weight is not None:
return JSONResponse( return JSONResponse(
status_code=400, status_code=400,
content={"message": "Only specify either remaining_weight or used_weight."}, content={"message": "Only specify either remaining_weight or used_weight."},
) )
try: try:
db_item = await spool.create( db_item = await spool.create(