diff --git a/frontend/app/admin/products/AdminProductList.tsx b/frontend/app/admin/products/AdminProductList.tsx index 7003fa26..efdcb3fe 100644 --- a/frontend/app/admin/products/AdminProductList.tsx +++ b/frontend/app/admin/products/AdminProductList.tsx @@ -1,9 +1,8 @@ 'use client'; -import { useState, useMemo, useEffect, useTransition, useCallback } from 'react'; +import { useState, useMemo, useEffect, useCallback } from 'react'; import type { Product, Category } from '../../../features/inventory/types'; import EditProductForm from './EditProductForm'; -import { bulkSetCategory } from './actions'; type CategoryNode = Category & { children: CategoryNode[] }; @@ -43,7 +42,7 @@ export default function AdminProductList() { const [selectedIds, setSelectedIds] = useState>(new Set()); const [bulkCategoryId, setBulkCategoryId] = useState(''); const [categoryTree, setCategoryTree] = useState([]); - const [isPending, startTransition] = useTransition(); + const [isBulkPending, setIsBulkPending] = useState(false); const [bulkError, setBulkError] = useState(null); // AI-kategorisering state @@ -132,21 +131,30 @@ export default function AdminProductList() { }); }; - const handleBulkApply = () => { + const handleBulkApply = async () => { setBulkError(null); const ids = Array.from(selectedIds); if (ids.length === 0) return; const categoryId = bulkCategoryId === '' ? null : bulkCategoryId === '__remove__' ? null : Number(bulkCategoryId); - startTransition(async () => { - try { - await bulkSetCategory(ids, categoryId); - setSelectedIds(new Set()); - setBulkCategoryId(''); - refetchProducts(); - } catch (err) { - setBulkError(err instanceof Error ? err.message : 'Fel vid uppdatering'); + setIsBulkPending(true); + try { + const res = await fetch('/api/admin/bulk-set-category', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ ids, categoryId }), + }); + if (!res.ok) { + const data = await res.json().catch(() => ({})); + throw new Error(data?.error || 'Fel vid uppdatering'); } - }); + setSelectedIds(new Set()); + setBulkCategoryId(''); + refetchProducts(); + } catch (err) { + setBulkError(err instanceof Error ? err.message : 'Fel vid uppdatering'); + } finally { + setIsBulkPending(false); + } }; const handleAiCategorize = async () => { @@ -178,7 +186,15 @@ export default function AdminProductList() { grouped.get(cid)!.push(s.productId); } for (const [categoryId, ids] of grouped.entries()) { - await bulkSetCategory(ids, categoryId); + const res = await fetch('/api/admin/bulk-set-category', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ ids, categoryId }), + }); + if (!res.ok) { + const data = await res.json().catch(() => ({})); + throw new Error(data?.error || 'Fel vid tillämpning'); + } } setAiSuggestions(null); setAiApproved(new Set()); @@ -287,10 +303,10 @@ export default function AdminProductList() {