feat(api): add create and update product routes with authentication

refactor(admin): integrate router refresh after product updates in forms
fix(imports): update fetch paths for product creation and update in ReceiptImportClient
This commit is contained in:
Nils-Johan Gynther
2026-04-19 18:12:29 +02:00
parent 90b02c4690
commit f12d881395
7 changed files with 177 additions and 8 deletions
@@ -1,6 +1,7 @@
'use client';
import { useState, useMemo, useEffect, useTransition } from 'react';
import { useRouter } from 'next/navigation';
import type { Product, Category } from '../../../features/inventory/types';
import EditProductForm from './EditProductForm';
import { bulkSetCategory, suggestBulkCategories } from './actions';
@@ -39,6 +40,7 @@ function flattenTree(nodes: CategoryNode[], depth = 0): { id: number; label: str
}
export default function AdminProductList({ products }: Props) {
const router = useRouter();
const [search, setSearch] = useState('');
const [sort, setSort] = useState('createdDesc');
const [showUncategorizedOnly, setShowUncategorizedOnly] = useState(false);
@@ -127,6 +129,7 @@ export default function AdminProductList({ products }: Props) {
await bulkSetCategory(ids, categoryId);
setSelectedIds(new Set());
setBulkCategoryId('');
router.refresh();
} catch (err) {
setBulkError(err instanceof Error ? err.message : 'Fel vid uppdatering');
}
@@ -1,6 +1,7 @@
'use client';
import { useState, useTransition, useEffect } from 'react';
import { useRouter } from 'next/navigation';
import type { Product } from '../../../features/inventory/types';
import { updateProductWithTags, deleteProduct, suggestProductCategory } from './actions';
@@ -33,6 +34,7 @@ const inputStyle: React.CSSProperties = {
};
export default function EditProductForm({ product }: Props) {
const router = useRouter();
const [isOpen, setIsOpen] = useState(false);
const [isPending, startTransition] = useTransition();
const [error, setError] = useState<string | null>(null);
@@ -107,6 +109,7 @@ export default function EditProductForm({ product }: Props) {
await updateProductWithTags(formData, rawTags);
setSuccess(true);
setIsOpen(false);
router.refresh();
} catch (err) {
setError(err instanceof Error ? err.message : 'Okänt fel');
}
@@ -120,6 +123,7 @@ export default function EditProductForm({ product }: Props) {
startTransition(async () => {
try {
await deleteProduct(product.id);
router.refresh();
} catch (err) {
setError(err instanceof Error ? err.message : 'Okänt fel');
}
-6
View File
@@ -108,8 +108,6 @@ export async function updateProductWithTags(formData: FormData, tags: string[])
const text = await tagsRes.text();
throw new Error(`Kunde inte uppdatera taggar: ${text}`);
}
revalidatePath('/admin/products');
}
export async function deleteProduct(id: number) {
@@ -123,8 +121,6 @@ export async function deleteProduct(id: number) {
const text = await res.text();
throw new Error(`Kunde inte ta bort produkt: ${text}`);
}
revalidatePath('/admin/products');
}
export async function resetAllProducts() {
@@ -155,8 +151,6 @@ export async function bulkSetCategory(ids: number[], categoryId: number | null)
const text = await res.text();
throw new Error(`Kunde inte uppdatera produkter: ${text}`);
}
revalidatePath('/admin/products');
}
export async function suggestProductCategory(productId: number) {