feat(receipt-import): refactor product creation and category update to use server actions
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
'use server';
|
||||
|
||||
import { getAuthHeaders } from '@/lib/auth-headers';
|
||||
|
||||
const API_BASE = process.env.NEXT_PUBLIC_API_URL_INTERNAL || 'http://recipe-api:8080';
|
||||
|
||||
export async function createProductAction(name: string) {
|
||||
const authHeaders = await getAuthHeaders();
|
||||
console.log('[createProductAction] Creating product with name:', name);
|
||||
console.log('[createProductAction] Auth headers:', authHeaders ? 'YES' : 'NO');
|
||||
|
||||
const res = await fetch(`${API_BASE}/api/products`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...authHeaders },
|
||||
body: JSON.stringify({ name }),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const e = await res.json().catch(() => ({}));
|
||||
throw new Error(e.message ?? `HTTP ${res.status}`);
|
||||
}
|
||||
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export async function updateProductCategoryAction(productId: number, categoryId: number) {
|
||||
const authHeaders = await getAuthHeaders();
|
||||
|
||||
const res = await fetch(`${API_BASE}/api/products/${productId}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json', ...authHeaders },
|
||||
body: JSON.stringify({ categoryId }),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const e = await res.json().catch(() => ({}));
|
||||
throw new Error(e.message ?? `HTTP ${res.status}`);
|
||||
}
|
||||
|
||||
return res.json();
|
||||
}
|
||||
Reference in New Issue
Block a user