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
+24 -10
View File
@@ -1,7 +1,7 @@
'use client';
import { useState, useTransition } from 'react';
import { consumeInventoryItem } from './actions';
import { useState } from 'react';
import { useRouter } from 'next/navigation';
type Props = {
id: number;
@@ -10,8 +10,9 @@ type Props = {
export default function InventoryConsumeForm({ id, unit }: Props) {
const [isOpen, setIsOpen] = useState(false);
const [isPending, startTransition] = useTransition();
const [isPending, setIsPending] = useState(false);
const [error, setError] = useState<string | null>(null);
const router = useRouter();
if (!isOpen) {
return (
@@ -44,14 +45,27 @@ export default function InventoryConsumeForm({ id, unit }: Props) {
const { quantity, unit: parsedUnit } = parseQuantityInput(raw, unit);
formData.set('amountUsed', String(quantity));
formData.set('unit', parsedUnit);
startTransition(async () => {
try {
await consumeInventoryItem(formData);
setIsOpen(false);
} catch (err) {
setError(err instanceof Error ? err.message : 'Okänt fel');
const comment = String(formData.get('comment') || '').trim();
const payload: Record<string, unknown> = { amountUsed: quantity };
if (comment) payload.comment = comment;
setIsPending(true);
try {
const res = await fetch(`/api/admin/inventory-item/${id}/consume`, {
method: 'POST',
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 förbruka');
}
});
setIsOpen(false);
router.refresh();
} catch (err) {
setError(err instanceof Error ? err.message : 'Okänt fel');
} finally {
setIsPending(false);
}
}}
style={{
display: 'grid',