Refactor quantity input parsing to handle additional units and improve conversion logic across inventory forms and recipe pages
This commit is contained in:
@@ -8,12 +8,7 @@ export async function GET(request: NextRequest) {
|
|||||||
cache: 'no-store',
|
cache: 'no-store',
|
||||||
});
|
});
|
||||||
|
|
||||||
const text = await res.text();
|
const data = await res.json();
|
||||||
|
|
||||||
return new NextResponse(text, {
|
return NextResponse.json(data, { status: res.status });
|
||||||
status: res.status,
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,8 +116,19 @@ function parseQuantityInput(input: string, defaultUnit: string) {
|
|||||||
if (!match) return { quantity: NaN, unit: defaultUnit };
|
if (!match) return { quantity: NaN, unit: defaultUnit };
|
||||||
let [, num, unit] = match;
|
let [, num, unit] = match;
|
||||||
num = num.replace(',', '.');
|
num = num.replace(',', '.');
|
||||||
unit = unit || defaultUnit;
|
unit = unit.toLowerCase() || defaultUnit;
|
||||||
if (defaultUnit === 'kg' && (unit === 'g' || unit === 'gram')) return { quantity: parseFloat(num) / 1000, unit: 'kg' };
|
const value = parseFloat(num);
|
||||||
if (defaultUnit === 'g' && (unit === 'kg' || unit === 'kilogram')) return { quantity: parseFloat(num) * 1000, unit: 'g' };
|
// Konvertera alltid till defaultUnit
|
||||||
return { quantity: parseFloat(num), unit };
|
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 };
|
||||||
}
|
}
|
||||||
@@ -18,10 +18,21 @@ function parseQuantityInput(input: string, defaultUnit: string) {
|
|||||||
if (!match) return { quantity: NaN, unit: defaultUnit };
|
if (!match) return { quantity: NaN, unit: defaultUnit };
|
||||||
let [, num, unit] = match;
|
let [, num, unit] = match;
|
||||||
num = num.replace(',', '.');
|
num = num.replace(',', '.');
|
||||||
unit = unit || defaultUnit;
|
unit = unit.toLowerCase() || defaultUnit;
|
||||||
if (defaultUnit === 'kg' && (unit === 'g' || unit === 'gram')) return { quantity: parseFloat(num) / 1000, unit: 'kg' };
|
const value = parseFloat(num);
|
||||||
if (defaultUnit === 'g' && (unit === 'kg' || unit === 'kilogram')) return { quantity: parseFloat(num) * 1000, unit: 'g' };
|
// Konvertera alltid till defaultUnit
|
||||||
return { quantity: parseFloat(num), unit };
|
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 = [
|
const UNIT_OPTIONS = [
|
||||||
|
|||||||
@@ -38,10 +38,21 @@ export default function InventoryForm({ products }: Props) {
|
|||||||
if (!match) return { quantity: NaN, unit: defaultUnit };
|
if (!match) return { quantity: NaN, unit: defaultUnit };
|
||||||
let [, num, unit] = match;
|
let [, num, unit] = match;
|
||||||
num = num.replace(',', '.');
|
num = num.replace(',', '.');
|
||||||
unit = unit || defaultUnit;
|
unit = unit.toLowerCase() || defaultUnit;
|
||||||
if (defaultUnit === 'kg' && (unit === 'g' || unit === 'gram')) return { quantity: parseFloat(num) / 1000, unit: 'kg' };
|
const value = parseFloat(num);
|
||||||
if (defaultUnit === 'g' && (unit === 'kg' || unit === 'kilogram')) return { quantity: parseFloat(num) * 1000, unit: 'g' };
|
// Konvertera alltid till defaultUnit
|
||||||
return { quantity: parseFloat(num), unit };
|
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 (
|
return (
|
||||||
|
|||||||
@@ -144,11 +144,11 @@ export default function CreateRecipePage() {
|
|||||||
style={{ padding: '0.5rem' }}
|
style={{ padding: '0.5rem' }}
|
||||||
>
|
>
|
||||||
<option value={0}>Välj produkt</option>
|
<option value={0}>Välj produkt</option>
|
||||||
{products.map((product) => (
|
{products.length > 0 ? products.map((product) => (
|
||||||
<option key={product.id} value={product.id}>
|
<option key={product.id} value={product.id}>
|
||||||
{product.name}
|
{product.name}
|
||||||
</option>
|
</option>
|
||||||
))}
|
)) : <option disabled>Kunde inte ladda produkter</option>}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<input
|
<input
|
||||||
|
|||||||
@@ -37,8 +37,19 @@ function parseQuantityInput(input: string, defaultUnit: string) {
|
|||||||
if (!match) return { quantity: NaN, unit: defaultUnit };
|
if (!match) return { quantity: NaN, unit: defaultUnit };
|
||||||
let [, num, unit] = match;
|
let [, num, unit] = match;
|
||||||
num = num.replace(',', '.');
|
num = num.replace(',', '.');
|
||||||
unit = unit || defaultUnit;
|
unit = unit.toLowerCase() || defaultUnit;
|
||||||
if (defaultUnit === 'kg' && (unit === 'g' || unit === 'gram')) return { quantity: parseFloat(num) / 1000, unit: 'kg' };
|
const value = parseFloat(num);
|
||||||
if (defaultUnit === 'g' && (unit === 'kg' || unit === 'kilogram')) return { quantity: parseFloat(num) * 1000, unit: 'g' };
|
// Konvertera alltid till defaultUnit
|
||||||
return { quantity: parseFloat(num), unit };
|
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 };
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user