From fd8480197cfeb9b9d04d3d5acf2aa389241a3507 Mon Sep 17 00:00:00 2001 From: Nils-Johan Gynther Date: Thu, 9 Apr 2026 23:36:36 +0200 Subject: [PATCH] Refactor quantity input parsing to handle additional units and improve conversion logic across inventory forms and recipe pages --- frontend/app/api/products/route.ts | 9 ++----- .../app/inventory/InventoryConsumeForm.tsx | 19 +++++++++++--- frontend/app/inventory/InventoryEditForm.tsx | 19 +++++++++++--- frontend/app/inventory/InventoryForm.tsx | 25 +++++++++++++------ .../app/recipes/create/CreateRecipePage.tsx | 4 +-- frontend/app/recipes/page.tsx | 21 ++++++++++++---- 6 files changed, 68 insertions(+), 29 deletions(-) diff --git a/frontend/app/api/products/route.ts b/frontend/app/api/products/route.ts index 76b0a96e..6b1dc312 100644 --- a/frontend/app/api/products/route.ts +++ b/frontend/app/api/products/route.ts @@ -8,12 +8,7 @@ export async function GET(request: NextRequest) { cache: 'no-store', }); - const text = await res.text(); + const data = await res.json(); - return new NextResponse(text, { - status: res.status, - headers: { - 'Content-Type': 'application/json', - }, - }); + return NextResponse.json(data, { status: res.status }); } diff --git a/frontend/app/inventory/InventoryConsumeForm.tsx b/frontend/app/inventory/InventoryConsumeForm.tsx index b052ac0a..e2eaeb08 100644 --- a/frontend/app/inventory/InventoryConsumeForm.tsx +++ b/frontend/app/inventory/InventoryConsumeForm.tsx @@ -116,8 +116,19 @@ function parseQuantityInput(input: string, defaultUnit: string) { if (!match) return { quantity: NaN, unit: defaultUnit }; let [, num, unit] = match; num = num.replace(',', '.'); - unit = unit || defaultUnit; - if (defaultUnit === 'kg' && (unit === 'g' || unit === 'gram')) return { quantity: parseFloat(num) / 1000, unit: 'kg' }; - if (defaultUnit === 'g' && (unit === 'kg' || unit === 'kilogram')) return { quantity: parseFloat(num) * 1000, unit: 'g' }; - return { quantity: parseFloat(num), unit }; + unit = unit.toLowerCase() || defaultUnit; + const value = parseFloat(num); + // Konvertera alltid till defaultUnit + if (defaultUnit === 'kg') { + if (unit === 'g' || unit === 'gram') return { quantity: value / 1000, unit: 'kg' }; + if (unit === 'hg' || unit === 'hektogram') return { quantity: value / 10, unit: 'kg' }; + if (unit === 'kg' || unit === 'kilogram' || unit === '') return { quantity: value, unit: 'kg' }; + } + if (defaultUnit === 'g') { + if (unit === 'kg' || unit === 'kilogram') return { quantity: value * 1000, unit: 'g' }; + if (unit === 'hg' || unit === 'hektogram') return { quantity: value * 100, unit: 'g' }; + if (unit === 'g' || unit === 'gram' || unit === '') return { quantity: value, unit: 'g' }; + } + // Lägg till fler konverteringar vid behov + return { quantity: value, unit: defaultUnit }; } \ No newline at end of file diff --git a/frontend/app/inventory/InventoryEditForm.tsx b/frontend/app/inventory/InventoryEditForm.tsx index 2c3957e1..eb7c076d 100644 --- a/frontend/app/inventory/InventoryEditForm.tsx +++ b/frontend/app/inventory/InventoryEditForm.tsx @@ -18,10 +18,21 @@ function parseQuantityInput(input: string, defaultUnit: string) { if (!match) return { quantity: NaN, unit: defaultUnit }; let [, num, unit] = match; num = num.replace(',', '.'); - unit = unit || defaultUnit; - if (defaultUnit === 'kg' && (unit === 'g' || unit === 'gram')) return { quantity: parseFloat(num) / 1000, unit: 'kg' }; - if (defaultUnit === 'g' && (unit === 'kg' || unit === 'kilogram')) return { quantity: parseFloat(num) * 1000, unit: 'g' }; - return { quantity: parseFloat(num), unit }; + unit = unit.toLowerCase() || defaultUnit; + const value = parseFloat(num); + // Konvertera alltid till defaultUnit + if (defaultUnit === 'kg') { + if (unit === 'g' || unit === 'gram') return { quantity: value / 1000, unit: 'kg' }; + if (unit === 'hg' || unit === 'hektogram') return { quantity: value / 10, unit: 'kg' }; + if (unit === 'kg' || unit === 'kilogram' || unit === '') return { quantity: value, unit: 'kg' }; + } + if (defaultUnit === 'g') { + if (unit === 'kg' || unit === 'kilogram') return { quantity: value * 1000, unit: 'g' }; + if (unit === 'hg' || unit === 'hektogram') return { quantity: value * 100, unit: 'g' }; + if (unit === 'g' || unit === 'gram' || unit === '') return { quantity: value, unit: 'g' }; + } + // Lägg till fler konverteringar vid behov + return { quantity: value, unit: defaultUnit }; } const UNIT_OPTIONS = [ diff --git a/frontend/app/inventory/InventoryForm.tsx b/frontend/app/inventory/InventoryForm.tsx index a53af0af..9a537aa1 100644 --- a/frontend/app/inventory/InventoryForm.tsx +++ b/frontend/app/inventory/InventoryForm.tsx @@ -35,13 +35,24 @@ export default function InventoryForm({ products }: Props) { function parseQuantityInput(input: string, defaultUnit: string) { const match = input.trim().match(/^([\d.,]+)\s*([a-zA-Z]*)$/); - if (!match) return { quantity: NaN, unit: defaultUnit }; - let [, num, unit] = match; - num = num.replace(',', '.'); - unit = unit || defaultUnit; - if (defaultUnit === 'kg' && (unit === 'g' || unit === 'gram')) return { quantity: parseFloat(num) / 1000, unit: 'kg' }; - if (defaultUnit === 'g' && (unit === 'kg' || unit === 'kilogram')) return { quantity: parseFloat(num) * 1000, unit: 'g' }; - return { quantity: parseFloat(num), unit }; + if (!match) return { quantity: NaN, unit: defaultUnit }; + let [, num, unit] = match; + num = num.replace(',', '.'); + unit = unit.toLowerCase() || defaultUnit; + const value = parseFloat(num); + // Konvertera alltid till defaultUnit + if (defaultUnit === 'kg') { + if (unit === 'g' || unit === 'gram') return { quantity: value / 1000, unit: 'kg' }; + if (unit === 'hg' || unit === 'hektogram') return { quantity: value / 10, unit: 'kg' }; + if (unit === 'kg' || unit === 'kilogram' || unit === '') return { quantity: value, unit: 'kg' }; + } + if (defaultUnit === 'g') { + if (unit === 'kg' || unit === 'kilogram') return { quantity: value * 1000, unit: 'g' }; + if (unit === 'hg' || unit === 'hektogram') return { quantity: value * 100, unit: 'g' }; + if (unit === 'g' || unit === 'gram' || unit === '') return { quantity: value, unit: 'g' }; + } + // Lägg till fler konverteringar vid behov + return { quantity: value, unit: defaultUnit }; } return ( diff --git a/frontend/app/recipes/create/CreateRecipePage.tsx b/frontend/app/recipes/create/CreateRecipePage.tsx index 1ce3bae6..095f9b97 100644 --- a/frontend/app/recipes/create/CreateRecipePage.tsx +++ b/frontend/app/recipes/create/CreateRecipePage.tsx @@ -144,11 +144,11 @@ export default function CreateRecipePage() { style={{ padding: '0.5rem' }} > - {products.map((product) => ( + {products.length > 0 ? products.map((product) => ( - ))} + )) : }