Files
recipe-app/frontend/app/inventory/InventoryForm.tsx
T
Nils-Johan Gynther d5b360fd62 Refactor logging in IcaRecipeParser and QuickImportService to use NestJS Logger
- Updated IcaRecipeParser to replace console.log statements with Logger for better logging practices.
- Enhanced QuickImportService with Logger for consistent logging and error handling.
- Changed quantity validation in CreateRecipeIngredientDto and CreateRecipeDto to allow zero.
- Removed CanonicalNameForm and NameForm components from the frontend.
- Updated EditProductForm to use a select dropdown for categories instead of a text input.
- Added validation for product name, canonical name, and category length in product update action.
- Refactored unit options into a separate file for better reusability across components.
- Removed unused API fetching functions and inventory types to clean up the codebase.
2026-04-16 18:18:27 +02:00

207 lines
5.9 KiB
TypeScript

'use client';
import { useState } from 'react';
import { createInventoryItem } from './actions';
import type { Product } from '../../features/inventory/types';
import { UNIT_OPTIONS } from '../../lib/units';
type Props = {
products: Product[];
};
export default function InventoryForm({ products }: Props) {
const [isPending, setIsPending] = useState(false);
const [error, setError] = useState<string | null>(null);
const [isOpen, setIsOpen] = useState(false);
const LOCATION_OPTIONS = [
{ value: '', label: 'Välj plats' },
{ value: 'Kyl', label: 'Kyl' },
{ value: 'Frys', label: 'Frys' },
{ value: 'Skafferi', label: 'Skafferi' },
{ value: 'Annat', label: 'Annat' },
];
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.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 (
<div style={{ marginBottom: '1.5rem' }}>
<button
type="button"
onClick={() => setIsOpen((v) => !v)}
style={{
padding: '0.6rem 1rem',
border: '1px solid #ddd',
borderRadius: '6px',
background: '#fff',
cursor: 'pointer',
fontWeight: 500,
fontSize: '1rem',
width: '100%',
textAlign: 'left',
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
}}
>
<span>Lägg till hemmavara</span>
<span>{isOpen ? '▲' : '▼'}</span>
</button>
{isOpen && (
<form
onSubmit={async (e) => {
e.preventDefault();
setError(null);
setIsPending(true);
const form = e.currentTarget;
const formData = new FormData(form);
const raw = formData.get('quantity') as string;
const unit = formData.get('unit') as string;
const { quantity, unit: parsedUnit } = parseQuantityInput(raw, unit);
formData.set('quantity', String(quantity));
formData.set('unit', parsedUnit);
try {
await createInventoryItem(formData);
form.reset();
} catch (err) {
setError(err instanceof Error ? err.message : 'Okänt fel');
} finally {
setIsPending(false);
}
}}
style={{
display: 'grid',
gap: '0.75rem',
padding: '1rem',
border: '1px solid #ddd',
borderTop: 'none',
borderRadius: '0 0 8px 8px',
marginBottom: '0',
}}
>
<h2 style={{ margin: 0, display: 'none' }}>Lägg till hemmavara</h2>
<label>
Produkt
<br />
<select name="productId" required style={{ width: '100%', padding: '0.5rem' }}>
<option value="">Välj produkt</option>
{products.map((product) => (
<option key={product.id} value={product.id}>
{product.canonicalName || product.name}
</option>
))}
</select>
</label>
<label>
Mängd
<br />
<input
name="quantity"
type="text"
required
style={{ width: '100%', padding: '0.5rem' }}
/>
</label>
<label>
Enhet
<br />
<select
name="unit"
required
style={{ width: '100%', padding: '0.5rem' }}
>
{UNIT_OPTIONS.map((opt) => (
<option key={opt.value} value={opt.value}>
{opt.label}
</option>
))}
</select>
</label>
<label>
Plats
<br />
<select
name="location"
required
style={{ width: '100%', padding: '0.5rem' }}
>
{LOCATION_OPTIONS.map((opt) => (
<option key={opt.value} value={opt.value}>
{opt.label}
</option>
))}
</select>
</label>
<label>
Varumärke
<br />
<input
name="brand"
type="text"
placeholder="t.ex. Eldorado, Kronfågel, Garant, ICA Basic, Motti"
style={{ width: '100%', padding: '0.5rem' }}
/>
</label>
<label>
Passar till
<br />
<input
name="suitableFor"
type="text"
placeholder="Wok, Gryta..."
style={{ width: '100%', padding: '0.5rem' }}
/>
</label>
<label>
Bäst före
<br />
<input
name="bestBeforeDate"
type="date"
style={{ width: '100%', padding: '0.5rem' }}
/>
</label>
<label style={{ display: 'flex', alignItems: 'center', gap: '0.5rem' }}>
<input name="opened" type="checkbox" />
Öppnad
</label>
<button type="submit" disabled={isPending} style={{ padding: '0.75rem' }}>
{isPending ? 'Sparar...' : 'Lägg till'}
</button>
{error ? <p style={{ color: 'crimson', margin: 0 }}>{error}</p> : null}
</form>
)}
</div>
);
}