From f030e5e96eccdfa99a20b5809c2e0bccbe3a1d9f Mon Sep 17 00:00:00 2001 From: Albert Date: Thu, 18 Jun 2026 08:51:19 +0200 Subject: [PATCH] feat: add delete order button to admin panel DELETE endpoint + trash button (with confirm dialog) in expanded order row. Co-Authored-By: Claude Sonnet 4.6 --- src/app/admin/page.tsx | 30 +++++++++++++++++++++++++++++- src/app/api/orders/route.ts | 13 +++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/app/admin/page.tsx b/src/app/admin/page.tsx index 0ff5eb3..ccd4cb3 100644 --- a/src/app/admin/page.tsx +++ b/src/app/admin/page.tsx @@ -4,7 +4,7 @@ import { useState, useEffect, useCallback } from 'react' import Image from 'next/image' import { Download, RefreshCw, LogOut, Search, Package, - ChevronDown, Check, X, ShoppingBag, Truck, Users, Euro, Sheet, + ChevronDown, Check, X, ShoppingBag, Truck, Users, Euro, Sheet, Trash2, } from 'lucide-react' import clsx from 'clsx' @@ -80,6 +80,7 @@ export default function AdminPage() { const [statusFilter, setStatusFilter] = useState('ALL') const [expandedId, setExpandedId] = useState(null) const [updatingId, setUpdatingId] = useState(null) + const [deletingId, setDeletingId] = useState(null) const [syncing, setSyncing] = useState(false) const [syncMsg, setSyncMsg] = useState('') @@ -143,6 +144,25 @@ export default function AdminPage() { } } + async function deleteOrder(orderId: string, orderNumber: string) { + if (!confirm(`Segur que vols esborrar la comanda ${orderNumber}? Aquesta acció no es pot desfer.`)) return + setDeletingId(orderId) + try { + await fetch('/api/orders', { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + 'x-admin-password': password, + }, + body: JSON.stringify({ id: orderId }), + }) + setOrders((prev) => prev.filter((o) => o.id !== orderId)) + setExpandedId(null) + } finally { + setDeletingId(null) + } + } + async function handleSyncSheets() { setSyncing(true) setSyncMsg('') @@ -454,6 +474,14 @@ export default function AdminPage() { ))} + )} diff --git a/src/app/api/orders/route.ts b/src/app/api/orders/route.ts index 1574a5e..35c3089 100644 --- a/src/app/api/orders/route.ts +++ b/src/app/api/orders/route.ts @@ -51,6 +51,19 @@ export async function GET(req: NextRequest) { return NextResponse.json({ orders }) } +// DELETE /api/orders — delete order by id +export async function DELETE(req: NextRequest) { + if (!checkAdmin(req)) { + return NextResponse.json({ error: 'No autoritzat' }, { status: 401 }) + } + + const { id } = await req.json() + if (!id) return NextResponse.json({ error: 'Falta id' }, { status: 400 }) + + await prisma.order.delete({ where: { id } }) + return NextResponse.json({ ok: true }) +} + // PATCH /api/orders — update order status export async function PATCH(req: NextRequest) { if (!checkAdmin(req)) {