change to hidden field and dummy inputs
This commit is contained in:
@@ -1,12 +1,13 @@
|
|||||||
import React from "react";
|
import React 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, Radio } 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";
|
||||||
@@ -49,120 +50,16 @@ 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 selected_filament = Form.useWatch('filament_id', form);
|
const [used_weight, set_used_weight] = useSavedState("create-used_weight", 0);
|
||||||
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 = () => {
|
const selected_filament_id = Form.useWatch('filament_id', form);
|
||||||
if (selected_filament)
|
const selected_filament = filamentOptions?.find(obj => {return obj.value === selected_filament_id});
|
||||||
{
|
|
||||||
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 (
|
return (
|
||||||
<Create
|
<Create
|
||||||
@@ -216,53 +113,83 @@ export const SpoolCreate: React.FC<IResourceComponentsProps & CreateOrCloneProps
|
|||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
<Form.Item
|
<Form.Item
|
||||||
label={t("spool.fields.weight_to_use")}
|
hidden={true}
|
||||||
help={t("spool.fields_help.weight_to_use")}
|
name={["used_weight"]}
|
||||||
name={["weight_to_use"]}
|
initialValue={0}
|
||||||
initialValue={1}
|
|
||||||
>
|
>
|
||||||
<Radio.Group>
|
<InputNumber
|
||||||
<Radio.Button value={1}>{t("spool.fields.used_weight")}</Radio.Button>
|
value={used_weight}
|
||||||
<Radio.Button value={2}>{t("spool.fields.remaining_weight")}</Radio.Button>
|
/>
|
||||||
<Radio.Button value={3} disabled={!has_spool_weight()}>{t("spool.fields.measured_weight")}</Radio.Button>
|
|
||||||
</Radio.Group>
|
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
|
||||||
<Form.Item
|
<Form.Item
|
||||||
label={t("spool.fields.used_weight")}
|
label={t("spool.fields.used_weight")}
|
||||||
help={t("spool.fields_help.used_weight")}
|
help={t("spool.fields_help.used_weight")}
|
||||||
name={["used_weight"]}
|
// name={["used_weight"]}
|
||||||
rules={[
|
initialValue={0}
|
||||||
{
|
|
||||||
required: handle_weight()[0],
|
|
||||||
},
|
|
||||||
]}
|
|
||||||
>
|
>
|
||||||
<InputNumber min={0} addonAfter="g" precision={1} formatter={numberFormatter} parser={numberParser} disabled={!handle_weight()[0]} />
|
<InputNumber
|
||||||
|
min={0}
|
||||||
|
addonAfter="g"
|
||||||
|
precision={1}
|
||||||
|
formatter={numberFormatter}
|
||||||
|
parser={numberParser}
|
||||||
|
value={used_weight}
|
||||||
|
onChange={(value) => {
|
||||||
|
set_used_weight(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"]}
|
||||||
rules={[
|
initialValue={0}
|
||||||
{
|
|
||||||
required: handle_weight()[1],
|
|
||||||
},
|
|
||||||
]}
|
|
||||||
>
|
>
|
||||||
<InputNumber min={0} addonAfter="g" precision={1} formatter={numberFormatter} parser={numberParser} disabled={!handle_weight()[1]} />
|
<InputNumber
|
||||||
|
min={0}
|
||||||
|
addonAfter="g"
|
||||||
|
precision={1}
|
||||||
|
formatter={numberFormatter}
|
||||||
|
parser={numberParser}
|
||||||
|
disabled={!(selected_filament?.weight || 0)}
|
||||||
|
value={(selected_filament?.weight || 0) ? (selected_filament?.weight || 0) - used_weight : 0}
|
||||||
|
onChange={(value) => {
|
||||||
|
set_used_weight((selected_filament?.weight || 0) - (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"]}
|
||||||
rules={[
|
initialValue={0}
|
||||||
{
|
|
||||||
required: handle_weight()[2],
|
|
||||||
},
|
|
||||||
]}
|
|
||||||
>
|
>
|
||||||
<InputNumber min={0} addonAfter="g" precision={1} formatter={numberFormatter} parser={numberParser} disabled={!handle_weight()[2]} />
|
<InputNumber
|
||||||
|
min={0}
|
||||||
|
addonAfter="g"
|
||||||
|
precision={1}
|
||||||
|
formatter={numberFormatter}
|
||||||
|
parser={numberParser}
|
||||||
|
disabled={!((selected_filament?.weight || 0) && (selected_filament?.spool_weight || 0))}
|
||||||
|
value={((selected_filament?.weight || 0) && (selected_filament?.spool_weight || 0)) ? (selected_filament?.weight || 0) - used_weight + (selected_filament?.spool_weight || 0) : 0}
|
||||||
|
onChange={(value) => {
|
||||||
|
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))
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
/>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
<Form.Item
|
<Form.Item
|
||||||
label={t("spool.fields.location")}
|
label={t("spool.fields.location")}
|
||||||
|
|||||||
@@ -158,11 +158,11 @@ async def create( # noqa: ANN201
|
|||||||
db: Annotated[AsyncSession, Depends(get_db_session)],
|
db: Annotated[AsyncSession, Depends(get_db_session)],
|
||||||
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(
|
||||||
|
|||||||
Reference in New Issue
Block a user