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
+23 -12
View File
@@ -1,29 +1,40 @@
'use client';
import { useState, useTransition } from 'react';
import { createProduct } from './actions';
import { useState } from 'react';
import { useRouter } from 'next/navigation';
export default function ProductForm() {
const [isPending, startTransition] = useTransition();
const [isPending, setIsPending] = useState(false);
const [error, setError] = useState<string | null>(null);
const router = useRouter();
return (
<form
onSubmit={(e) => {
onSubmit={async (e) => {
e.preventDefault();
setError(null);
const form = e.currentTarget;
const formData = new FormData(form);
const name = String((new FormData(form)).get('name') || '').trim();
startTransition(async () => {
try {
await createProduct(formData);
form.reset();
} catch (err) {
setError(err instanceof Error ? err.message : 'Okänt fel');
setIsPending(true);
try {
const res = await fetch('/api/admin/create-product', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name }),
});
if (!res.ok) {
const data = await res.json().catch(() => ({}));
throw new Error(data?.error || 'Kunde inte skapa produkt');
}
});
form.reset();
router.refresh();
} catch (err) {
setError(err instanceof Error ? err.message : 'Okänt fel');
} finally {
setIsPending(false);
}
}}
style={{
display: 'grid',