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
+22 -11
View File
@@ -1,8 +1,8 @@
'use client';
import { useState, useTransition } from 'react';
import { useState } from 'react';
import { useRouter } from 'next/navigation';
import type { Product } from '../../features/inventory/types';
import { addPantryItem } from './actions';
type Props = {
products: Product[];
@@ -11,23 +11,34 @@ type Props = {
export default function AddToPantryForm({ products, pantryProductIds }: Props) {
const [selectedId, setSelectedId] = useState('');
const [isPending, startTransition] = useTransition();
const [isPending, setIsPending] = useState(false);
const [error, setError] = useState<string | null>(null);
const router = useRouter();
const available = products.filter((p) => !pantryProductIds.has(p.id));
function handleSubmit(e: React.FormEvent) {
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
if (!selectedId) return;
setError(null);
startTransition(async () => {
try {
await addPantryItem(Number(selectedId));
setSelectedId('');
} catch (err) {
setError(err instanceof Error ? err.message : 'Okänt fel');
setIsPending(true);
try {
const res = await fetch('/api/admin/pantry-item', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ productId: Number(selectedId) }),
});
if (!res.ok) {
const data = await res.json().catch(() => ({}));
throw new Error(data?.error || 'Kunde inte lägga till');
}
});
setSelectedId('');
router.refresh();
} catch (err) {
setError(err instanceof Error ? err.message : 'Okänt fel');
} finally {
setIsPending(false);
}
}
return (
+5 -7
View File
@@ -1,7 +1,6 @@
'use client';
import { useTransition } from 'react';
import { removePantryItem } from './actions';
import { useRouter } from 'next/navigation';
type PantryItem = {
id: number;
@@ -20,13 +19,12 @@ type Props = {
};
export default function PantryList({ items, inventoryByProductId }: Props) {
const [isPending, startTransition] = useTransition();
const router = useRouter();
function handleRemove(id: number, name: string) {
async function handleRemove(id: number, name: string) {
if (!confirm(`Ta bort "${name}" från baslagret?`)) return;
startTransition(async () => {
await removePantryItem(id);
});
const res = await fetch(`/api/admin/pantry-item/${id}`, { method: 'DELETE' });
if (res.ok) router.refresh();
}
if (items.length === 0) {