Minor spool edit cleanup, added enum

This commit is contained in:
Donkie
2024-01-30 19:29:17 +01:00
parent 81b7a9b4ec
commit 41a40d9fa3

View File

@@ -20,6 +20,12 @@ We also need to stringify them again before sending them back to the API, which
the form's onFinish method. Form.Item's normalize should do this, but it doesn't seem to work. the form's onFinish method. Form.Item's normalize should do this, but it doesn't seem to work.
*/ */
enum WeightToEnter {
used_weight = 1,
remaining_weight = 2,
measured_weight = 3,
}
export const SpoolEdit: React.FC<IResourceComponentsProps> = () => { export const SpoolEdit: React.FC<IResourceComponentsProps> = () => {
const t = useTranslate(); const t = useTranslate();
const [messageApi, contextHolder] = message.useMessage(); const [messageApi, contextHolder] = message.useMessage();
@@ -94,17 +100,17 @@ export const SpoolEdit: React.FC<IResourceComponentsProps> = () => {
const newSelectedFilament = filamentOptions?.find((obj) => { const newSelectedFilament = filamentOptions?.find((obj) => {
return obj.value === newID; return obj.value === newID;
}); });
const newFilamentWeight = newSelectedFilament?.weight || 0; const filamentHasWeight = newSelectedFilament?.weight || 0;
const newSpoolWeight = newSelectedFilament?.spool_weight || 0; const filamentHasSpoolWeight = newSelectedFilament?.spool_weight || 0;
if (weightToEnter >= 3) { if (weightToEnter == WeightToEnter.measured_weight) {
if (!(newFilamentWeight && newSpoolWeight)) { if (!(filamentHasWeight && filamentHasSpoolWeight)) {
setWeightToEnter(2); setWeightToEnter(WeightToEnter.remaining_weight);
} }
} }
if (weightToEnter >= 2) { if (weightToEnter == WeightToEnter.remaining_weight || weightToEnter == WeightToEnter.measured_weight) {
if (!newFilamentWeight) { if (!filamentHasWeight) {
setWeightToEnter(1); setWeightToEnter(WeightToEnter.used_weight);
} }
} }
}; };
@@ -232,14 +238,14 @@ export const SpoolEdit: React.FC<IResourceComponentsProps> = () => {
onChange={(value) => { onChange={(value) => {
setWeightToEnter(value.target.value); setWeightToEnter(value.target.value);
}} }}
defaultValue={1} defaultValue={WeightToEnter.used_weight}
value={weightToEnter} value={weightToEnter}
> >
<Radio.Button value={1}>{t("spool.fields.used_weight")}</Radio.Button> <Radio.Button value={WeightToEnter.used_weight}>{t("spool.fields.used_weight")}</Radio.Button>
<Radio.Button value={2} disabled={!filamentWeight}> <Radio.Button value={WeightToEnter.remaining_weight} disabled={!filamentWeight}>
{t("spool.fields.remaining_weight")} {t("spool.fields.remaining_weight")}
</Radio.Button> </Radio.Button>
<Radio.Button value={3} disabled={!(filamentWeight && spoolWeight)}> <Radio.Button value={WeightToEnter.measured_weight} disabled={!(filamentWeight && spoolWeight)}>
{t("spool.fields.measured_weight")} {t("spool.fields.measured_weight")}
</Radio.Button> </Radio.Button>
</Radio.Group> </Radio.Group>
@@ -251,7 +257,7 @@ export const SpoolEdit: React.FC<IResourceComponentsProps> = () => {
precision={1} precision={1}
formatter={numberFormatter} formatter={numberFormatter}
parser={numberParser} parser={numberParser}
disabled={weightToEnter != 1} disabled={weightToEnter != WeightToEnter.used_weight}
value={usedWeight} value={usedWeight}
onChange={(value) => { onChange={(value) => {
weightChange(value ?? 0); weightChange(value ?? 0);
@@ -265,7 +271,7 @@ export const SpoolEdit: React.FC<IResourceComponentsProps> = () => {
precision={1} precision={1}
formatter={numberFormatter} formatter={numberFormatter}
parser={numberParser} parser={numberParser}
disabled={weightToEnter != 2} disabled={weightToEnter != WeightToEnter.remaining_weight}
value={filamentWeight ? filamentWeight - usedWeight : 0} value={filamentWeight ? filamentWeight - usedWeight : 0}
onChange={(value) => { onChange={(value) => {
weightChange(filamentWeight - (value ?? 0)); weightChange(filamentWeight - (value ?? 0));
@@ -279,7 +285,7 @@ export const SpoolEdit: React.FC<IResourceComponentsProps> = () => {
precision={1} precision={1}
formatter={numberFormatter} formatter={numberFormatter}
parser={numberParser} parser={numberParser}
disabled={weightToEnter != 3} disabled={weightToEnter != WeightToEnter.measured_weight}
value={filamentWeight && spoolWeight ? filamentWeight - usedWeight + spoolWeight : 0} value={filamentWeight && spoolWeight ? filamentWeight - usedWeight + spoolWeight : 0}
onChange={(value) => { onChange={(value) => {
weightChange(filamentWeight - ((value ?? 0) - spoolWeight)); weightChange(filamentWeight - ((value ?? 0) - spoolWeight));