Files
recipe-app/frontend/app/admin/products/actions.ts
T
Nils-Johan Gynther d5b360fd62 Refactor logging in IcaRecipeParser and QuickImportService to use NestJS Logger
- Updated IcaRecipeParser to replace console.log statements with Logger for better logging practices.
- Enhanced QuickImportService with Logger for consistent logging and error handling.
- Changed quantity validation in CreateRecipeIngredientDto and CreateRecipeDto to allow zero.
- Removed CanonicalNameForm and NameForm components from the frontend.
- Updated EditProductForm to use a select dropdown for categories instead of a text input.
- Added validation for product name, canonical name, and category length in product update action.
- Refactored unit options into a separate file for better reusability across components.
- Removed unused API fetching functions and inventory types to clean up the codebase.
2026-04-16 18:18:27 +02:00

49 lines
1.6 KiB
TypeScript

'use server';
import { revalidatePath } from 'next/cache';
import { API_BASE } from '../../../lib/api';
export async function updateProduct(formData: FormData) {
const id = Number(formData.get('id'));
const name = String(formData.get('name') || '').trim();
const canonicalName = String(formData.get('canonicalName') || '').trim();
const category = String(formData.get('category') || '').trim();
if (!name) throw new Error('Namn får inte vara tomt.');
if (name.length > 100) throw new Error('Namn får inte vara längre än 100 tecken.');
if (canonicalName.length > 100) throw new Error('Canonical name får inte vara längre än 100 tecken.');
if (category.length > 100) throw new Error('Kategori får inte vara längre än 100 tecken.');
const res = await fetch(`${API_BASE}/api/products/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: name || undefined,
canonicalName: canonicalName || undefined,
category: category || null,
}),
cache: 'no-store',
});
if (!res.ok) {
const text = await res.text();
throw new Error(`Kunde inte uppdatera produkt: ${text}`);
}
revalidatePath('/admin/products');
}
export async function deleteProduct(id: number) {
const res = await fetch(`${API_BASE}/api/products/${id}`, {
method: 'DELETE',
cache: 'no-store',
});
if (!res.ok) {
const text = await res.text();
throw new Error(`Kunde inte ta bort produkt: ${text}`);
}
revalidatePath('/admin/products');
}