Files
recipe-app/frontend/app/admin/products/actions.ts
T
Nils-Johan Gynther 21dc06829a feat(meal-plan): add servings field to MealPlanEntry and update related functionality
feat(products): implement bulk update for product categories

feat(recipes): add servings input to WriteRecipePage and update MealPlanClient for servings management

refactor(types): enhance Product and Category types with additional properties
2026-04-17 22:50:41 +02:00

108 lines
3.6 KiB
TypeScript

'use server';
import { revalidatePath } from 'next/cache';
import { API_BASE } from '../../../lib/api';
import { getAuthHeaders } from '../../../lib/auth-headers';
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();
const subcategory = String(formData.get('subcategory') || '').trim();
const brand = String(formData.get('brand') || '').trim();
const categoryIdRaw = formData.get('categoryId');
const categoryId = categoryIdRaw !== '' && categoryIdRaw != null ? Number(categoryIdRaw) : null;
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.');
if (subcategory.length > 100) throw new Error('Underkategori får inte vara längre än 100 tecken.');
if (brand.length > 100) throw new Error('Varumärke 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', ...(await getAuthHeaders()) },
body: JSON.stringify({
name: name || undefined,
canonicalName: canonicalName || undefined,
category: category || null,
subcategory: subcategory || null,
brand: brand || null,
categoryId,
}),
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 setProductTags(productId: number, tags: string[]) {
const res = await fetch(`${API_BASE}/api/products/${productId}/tags`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json', ...(await getAuthHeaders()) },
body: JSON.stringify({ tags }),
cache: 'no-store',
});
if (!res.ok) {
const text = await res.text();
throw new Error(`Kunde inte uppdatera taggar: ${text}`);
}
revalidatePath('/admin/products');
}
export async function deleteProduct(id: number) {
const res = await fetch(`${API_BASE}/api/products/${id}`, {
method: 'DELETE',
headers: { ...(await getAuthHeaders()) },
cache: 'no-store',
});
if (!res.ok) {
const text = await res.text();
throw new Error(`Kunde inte ta bort produkt: ${text}`);
}
revalidatePath('/admin/products');
}
export async function resetAllProducts() {
const res = await fetch(`${API_BASE}/api/products/reset-all`, {
method: 'POST',
headers: { ...(await getAuthHeaders()) },
cache: 'no-store',
});
if (!res.ok) {
const text = await res.text();
throw new Error(`Kunde inte återställa produkter: ${text}`);
}
revalidatePath('/admin/products');
}
export async function bulkSetCategory(ids: number[], categoryId: number | null) {
if (ids.length === 0) return;
const res = await fetch(`${API_BASE}/api/products/bulk-update`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', ...(await getAuthHeaders()) },
body: JSON.stringify({ ids, categoryId }),
cache: 'no-store',
});
if (!res.ok) {
const text = await res.text();
throw new Error(`Kunde inte uppdatera produkter: ${text}`);
}
revalidatePath('/admin/products');
}