feat(api): implement bulk categorization and suggestion endpoints with authentication

refactor(actions): remove unused imports and console logs from product actions
refactor(EditProductForm): update category suggestion logic to use new API endpoint
This commit is contained in:
Nils-Johan Gynther
2026-04-19 19:04:04 +02:00
parent e0836cd269
commit 1ae9b336d8
6 changed files with 98 additions and 32 deletions
@@ -3,7 +3,7 @@
import { useState, useMemo, useEffect, useTransition, useCallback } from 'react';
import type { Product, Category } from '../../../features/inventory/types';
import EditProductForm from './EditProductForm';
import { bulkSetCategory, suggestBulkCategories } from './actions';
import { bulkSetCategory } from './actions';
type CategoryNode = Category & { children: CategoryNode[] };
@@ -54,15 +54,12 @@ export default function AdminProductList() {
const [aiApplying, setAiApplying] = useState(false);
const refetchProducts = useCallback(() => {
console.log('[AdminProductList] refetchProducts: starting fetch /api/products');
fetch('/api/products')
.then(async (r) => {
console.log('[AdminProductList] refetchProducts: HTTP', r.status);
if (!r.ok) throw new Error(`HTTP ${r.status}`);
return r.json();
})
.then((data) => {
console.log('[AdminProductList] refetchProducts: got', Array.isArray(data) ? data.length : 'non-array', 'products');
if (Array.isArray(data)) setProducts(data);
})
.catch((e) => console.error('[AdminProductList] refetchProducts error:', e))
@@ -157,9 +154,11 @@ export default function AdminProductList() {
setAiError(null);
setAiSuggestions(null);
try {
const results = await suggestBulkCategories();
setAiSuggestions(results);
setAiApproved(new Set(results.map((r) => r.productId)));
const res = await fetch('/api/admin/bulk-categorize', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({}) });
const data = await res.json();
if (!res.ok) throw new Error(data?.error ?? 'AI-kategorisering misslyckades');
setAiSuggestions(data);
setAiApproved(new Set(data.map((r: AiSuggestion) => r.productId)));
} catch (err) {
setAiError(err instanceof Error ? err.message : 'AI-kategorisering misslyckades');
} finally {
@@ -2,7 +2,6 @@
import { useState, useEffect } from 'react';
import type { Product } from '../../../features/inventory/types';
import { suggestProductCategory } from './actions';
type CategoryNode = {
id: number;
@@ -84,8 +83,10 @@ export default function EditProductForm({ product, onSaved, onDeleted }: Props)
setAiError(null);
setAiSuggestion(null);
try {
const result = await suggestProductCategory(product.id);
setAiSuggestion(result);
const res = await fetch(`/api/admin/suggest-category/${product.id}`);
const data = await res.json();
if (!res.ok) throw new Error(data?.error ?? 'AI-kategorisering misslyckades');
setAiSuggestion(data);
} catch (err) {
setAiError(err instanceof Error ? err.message : 'AI-kategorisering misslyckades');
} finally {
@@ -97,7 +98,6 @@ export default function EditProductForm({ product, onSaved, onDeleted }: Props)
e.preventDefault();
const formData = new FormData(e.currentTarget);
const rawTags = tagInput.split(',').map((t) => t.trim().toLowerCase()).filter(Boolean);
console.log('[EditProductForm] handleSubmit: PATCH /api/admin/product/', product.id);
setIsPending(true);
setError(null);
setSuccess(false);
@@ -116,7 +116,6 @@ export default function EditProductForm({ product, onSaved, onDeleted }: Props)
}),
});
const data = await res.json();
console.log('[EditProductForm] handleSubmit: HTTP', res.status, data);
if (!res.ok) {
setError(data?.error ?? 'Okänt fel');
return;
@@ -125,7 +124,6 @@ export default function EditProductForm({ product, onSaved, onDeleted }: Props)
setIsOpen(false);
onSaved(data as Product);
} catch (err) {
console.error('[EditProductForm] handleSubmit error:', err);
setError(err instanceof Error ? err.message : 'Okänt fel');
} finally {
setIsPending(false);
@@ -137,7 +135,6 @@ export default function EditProductForm({ product, onSaved, onDeleted }: Props)
setError(null);
setSuccess(false);
setIsPending(true);
console.log('[EditProductForm] handleDelete: DELETE /api/admin/product/', product.id);
try {
const res = await fetch(`/api/admin/product/${product.id}`, { method: 'DELETE' });
console.log('[EditProductForm] handleDelete: HTTP', res.status);
@@ -148,7 +145,6 @@ export default function EditProductForm({ product, onSaved, onDeleted }: Props)
}
onDeleted(product.id);
} catch (err) {
console.error('[EditProductForm] handleDelete error:', err);
setError(err instanceof Error ? err.message : 'Okänt fel');
} finally {
setIsPending(false);
+1 -7
View File
@@ -61,7 +61,6 @@ export async function setProductTags(productId: number, tags: string[]) {
export async function updateProductWithTags(formData: FormData, tags: string[]) {
const id = Number(formData.get('id'));
console.log('[actions:updateProductWithTags] called for product id:', id, 'tags:', tags);
const name = String(formData.get('name') || '').trim();
const canonicalName = String(formData.get('canonicalName') || '').trim();
const category = String(formData.get('category') || '').trim();
@@ -78,7 +77,6 @@ export async function updateProductWithTags(formData: FormData, tags: string[])
if (brand.length > 100) throw new Error('Varumärke får inte vara längre än 100 tecken.');
const authHeaders = await getAuthHeaders();
console.log('[actions:updateProductWithTags] auth headers present:', !!authHeaders.Authorization);
const res = await fetch(`${API_BASE}/api/products/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json', ...authHeaders },
@@ -99,7 +97,6 @@ export async function updateProductWithTags(formData: FormData, tags: string[])
}
const updatedProduct = await res.json();
console.log('[actions:updateProductWithTags] PATCH OK, product id:', (updatedProduct as any)?.id);
const tagsRes = await fetch(`${API_BASE}/api/products/${id}/tags`, {
method: 'PUT',
@@ -113,16 +110,13 @@ export async function updateProductWithTags(formData: FormData, tags: string[])
throw new Error(`Kunde inte uppdatera taggar: ${text}`);
}
console.log('[actions:updateProductWithTags] tags PUT OK, fetching full product...');
// Fetch the complete product (includes tags, categoryRef) after both updates
// Fetch the complete product
const fullRes = await fetch(`${API_BASE}/api/products/${id}`, {
headers: authHeaders,
cache: 'no-store',
});
console.log('[actions:updateProductWithTags] full product fetch HTTP', fullRes.status);
if (!fullRes.ok) return updatedProduct;
const result = await fullRes.json();
console.log('[actions:updateProductWithTags] returning full product id:', (result as any)?.id);
return result;
}