From 4e10f1019d749a89551f498de74fcf39c3998d26 Mon Sep 17 00:00:00 2001 From: Matt Gerega Date: Thu, 20 Mar 2025 16:52:14 -0400 Subject: [PATCH] Add Measured Weight option to the filament adjustment modal #560 --- client/src/pages/spools/functions.tsx | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/client/src/pages/spools/functions.tsx b/client/src/pages/spools/functions.tsx index 3388277..3fe30ff 100644 --- a/client/src/pages/spools/functions.tsx +++ b/client/src/pages/spools/functions.tsx @@ -44,6 +44,26 @@ export async function useSpoolFilament(spool: ISpool, length?: number, weight?: await fetch(request, init); } +/** + * Adjust usage based on the spool's current gross weight + * @param spool The spool + * @param weight The weight of the spool, in g + */ +export async function useSpoolFilamentMeasure(spool: ISpool, weight: number) { + const init: RequestInit = { + method: "PUT", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + weight: weight, + }), + }; + const request = new Request(`${getAPIURL()}/spool/${spool.id}/measure`); + await fetch(request, init); +} + + /** * Returns an array of queries using the useQueries hook from @tanstack/react-query. * Each query fetches a spool by its ID from the server. @@ -173,7 +193,7 @@ export function useGetFilamentSelectOptions() { }; } -type MeasurementType = "length" | "weight"; +type MeasurementType = "length" | "weight" | "measured_weight"; export function useSpoolAdjustModal() { const t = useTranslate(); @@ -207,8 +227,10 @@ export function useSpoolAdjustModal() { if (measurementType === "length") { await useSpoolFilament(curSpool, value, undefined); - } else { + } else if (measurementType === "weight") { await useSpoolFilament(curSpool, undefined, value); + } else { + await useSpoolFilamentMeasure(curSpool, value); } setCurSpool(null); @@ -225,6 +247,7 @@ export function useSpoolAdjustModal() { > {t("spool.form.measurement_type.length")} {t("spool.form.measurement_type.weight")} + {t("spool.fields.measured_weight")}