feat(api): add PATCH endpoint for updating product status with authentication

This commit is contained in:
Nils-Johan Gynther
2026-04-19 19:15:54 +02:00
parent 151a7e335d
commit 829b7a80fc
3 changed files with 45 additions and 4 deletions
@@ -1,7 +1,7 @@
'use client';
import { useState, useTransition } from 'react';
import { setProductStatus } from '../actions';
import { useRouter } from 'next/navigation';
type PendingProduct = {
id: number;
@@ -17,14 +17,24 @@ export default function PendingProductsClient({ products: initial }: { products:
const [isPending, startTransition] = useTransition();
const [error, setError] = useState<string | null>(null);
const [processing, setProcessing] = useState<number | null>(null);
const router = useRouter();
function handleAction(id: number, status: 'active' | 'rejected') {
setError(null);
setProcessing(id);
startTransition(async () => {
try {
await setProductStatus(id, status);
const res = await fetch(`/api/admin/product-status/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ status }),
});
if (!res.ok) {
const data = await res.json().catch(() => ({}));
throw new Error(data?.error || 'Fel vid uppdatering');
}
setProducts((prev) => prev.filter((p) => p.id !== id));
router.refresh();
} catch (err) {
setError(err instanceof Error ? err.message : 'Fel vid uppdatering');
} finally {