feat(api): implement new API routes for bulk category updates, inventory consumption, and product management with authentication

This commit is contained in:
Nils-Johan Gynther
2026-04-19 19:11:09 +02:00
parent 1ae9b336d8
commit 6f24aee18d
17 changed files with 440 additions and 85 deletions
+32 -12
View File
@@ -1,7 +1,7 @@
'use client';
import { useState, useTransition } from 'react';
import { updateInventoryItem } from './actions';
import { useState } from 'react';
import { useRouter } from 'next/navigation';
import type { InventoryItem } from '../../features/inventory/types';
import { UNIT_OPTIONS } from '../../lib/units';
@@ -46,8 +46,9 @@ const LOCATION_OPTIONS = [
export default function InventoryEditForm({ item }: Props) {
const [isEditing, setIsEditing] = useState(false);
const [isPending, startTransition] = useTransition();
const [isPending, setIsPending] = useState(false);
const [error, setError] = useState<string | null>(null);
const router = useRouter();
if (!isEditing) {
return (
@@ -79,16 +80,35 @@ export default function InventoryEditForm({ item }: Props) {
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');
const payload: Record<string, unknown> = { opened: formData.get('opened') === 'on' };
if (raw) payload.quantity = quantity;
if (parsedUnit) payload.unit = parsedUnit;
payload.location = String(formData.get('location') || '').trim();
payload.brand = String(formData.get('brand') || '').trim();
payload.suitableFor = String(formData.get('suitableFor') || '').trim();
payload.comment = String(formData.get('comment') || '').trim();
const bestBeforeDate = String(formData.get('bestBeforeDate') || '').trim();
payload.bestBeforeDate = bestBeforeDate || null;
setIsPending(true);
try {
const res = await fetch(`/api/admin/inventory-item/${item.id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
});
if (!res.ok) {
const data = await res.json().catch(() => ({}));
throw new Error(data?.error || 'Kunde inte uppdatera');
}
});
setIsEditing(false);
router.refresh();
} catch (err) {
setError(err instanceof Error ? err.message : 'Okänt fel');
} finally {
setIsPending(false);
}
}}
style={{
display: 'grid',