233 lines
6.4 KiB
TypeScript
233 lines
6.4 KiB
TypeScript
'use client';
|
|
|
|
import { useState, useTransition } from 'react';
|
|
import { updateInventoryItem } from './actions';
|
|
import type { InventoryItem } from '../../features/inventory/types';
|
|
|
|
type Props = {
|
|
item: InventoryItem;
|
|
};
|
|
|
|
function toDateInputValue(value: string | null) {
|
|
if (!value) return '';
|
|
return value.slice(0, 10);
|
|
}
|
|
|
|
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 };
|
|
}
|
|
|
|
const UNIT_OPTIONS = [
|
|
{ value: '', label: 'Välj enhet' },
|
|
{ value: 'g', label: 'g (gram)' },
|
|
{ value: 'kg', label: 'kg (kilogram)' },
|
|
{ value: 'hg', label: 'hg (hektogram)' },
|
|
{ value: 'ml', label: 'ml (milliliter)' },
|
|
{ value: 'dl', label: 'dl (deciliter)' },
|
|
{ value: 'l', label: 'l (liter)' },
|
|
{ value: 'st', label: 'st (styck)' },
|
|
{ value: 'tsk', label: 'tsk (tesked)' },
|
|
{ value: 'msk', label: 'msk (matsked)' },
|
|
];
|
|
|
|
const LOCATION_OPTIONS = [
|
|
{ value: '', label: 'Välj plats' },
|
|
{ value: 'Kyl', label: 'Kyl' },
|
|
{ value: 'Frys', label: 'Frys' },
|
|
{ value: 'Skafferi', label: 'Skafferi' },
|
|
{ value: 'Annat', label: 'Annat' },
|
|
];
|
|
|
|
export default function InventoryEditForm({ item }: Props) {
|
|
const [isEditing, setIsEditing] = useState(false);
|
|
const [isPending, startTransition] = useTransition();
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
if (!isEditing) {
|
|
return (
|
|
<button
|
|
type="button"
|
|
onClick={() => setIsEditing(true)}
|
|
style={{ padding: '0.5rem 0.75rem' }}
|
|
>
|
|
Redigera
|
|
</button>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div
|
|
style={{
|
|
width: '100%',
|
|
display: 'grid',
|
|
gap: '0.75rem',
|
|
marginTop: '0.5rem',
|
|
}}
|
|
>
|
|
<form
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
setError(null);
|
|
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);
|
|
startTransition(async () => {
|
|
try {
|
|
await updateInventoryItem(formData);
|
|
setIsEditing(false);
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : 'Okänt fel');
|
|
}
|
|
});
|
|
}}
|
|
style={{
|
|
display: 'grid',
|
|
gap: '0.75rem',
|
|
border: '1px solid #eee',
|
|
borderRadius: '8px',
|
|
padding: '0.75rem',
|
|
background: '#fafafa',
|
|
}}
|
|
>
|
|
<input type="hidden" name="id" value={item.id} />
|
|
|
|
<div
|
|
style={{
|
|
display: 'grid',
|
|
gap: '0.75rem',
|
|
gridTemplateColumns: 'repeat(auto-fit, minmax(180px, 1fr))',
|
|
}}
|
|
>
|
|
<label>
|
|
Mängd
|
|
<br />
|
|
<input
|
|
name="quantity"
|
|
type="text"
|
|
required
|
|
defaultValue={item.quantity}
|
|
style={{ width: '100%', padding: '0.5rem' }}
|
|
/>
|
|
</label>
|
|
|
|
<label>
|
|
Enhet
|
|
<br />
|
|
<select
|
|
name="unit"
|
|
defaultValue={item.unit}
|
|
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"
|
|
defaultValue={item.location || ''}
|
|
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"
|
|
defaultValue={item.brand || ''}
|
|
style={{ width: '100%', padding: '0.5rem' }}
|
|
/>
|
|
</label>
|
|
|
|
<label>
|
|
Bäst före
|
|
<br />
|
|
<input
|
|
name="bestBeforeDate"
|
|
type="date"
|
|
defaultValue={toDateInputValue(item.bestBeforeDate)}
|
|
style={{ width: '100%', padding: '0.5rem' }}
|
|
/>
|
|
</label>
|
|
</div>
|
|
|
|
<label>
|
|
Passar till
|
|
<br />
|
|
<input
|
|
name="suitableFor"
|
|
type="text"
|
|
defaultValue={item.suitableFor || ''}
|
|
style={{ width: '100%', padding: '0.5rem' }}
|
|
/>
|
|
</label>
|
|
|
|
<label>
|
|
Kommentar
|
|
<br />
|
|
<input
|
|
name="comment"
|
|
type="text"
|
|
defaultValue={item.comment || ''}
|
|
style={{ width: '100%', padding: '0.5rem' }}
|
|
/>
|
|
</label>
|
|
|
|
<label style={{ display: 'flex', alignItems: 'center', gap: '0.5rem' }}>
|
|
<input
|
|
name="opened"
|
|
type="checkbox"
|
|
defaultChecked={item.opened ?? false}
|
|
/>
|
|
Öppnad
|
|
</label>
|
|
|
|
<div style={{ display: 'flex', gap: '0.75rem', flexWrap: 'wrap' }}>
|
|
<button
|
|
type="submit"
|
|
disabled={isPending}
|
|
style={{ padding: '0.6rem 0.9rem' }}
|
|
>
|
|
{isPending ? 'Sparar...' : 'Spara ändringar'}
|
|
</button>
|
|
|
|
<button
|
|
type="button"
|
|
onClick={() => setIsEditing(false)}
|
|
disabled={isPending}
|
|
style={{ padding: '0.6rem 0.9rem' }}
|
|
>
|
|
Avbryt
|
|
</button>
|
|
</div>
|
|
</form>
|
|
|
|
{error ? <p style={{ color: 'crimson', margin: 0 }}>{error}</p> : null}
|
|
</div>
|
|
);
|
|
} |