Chapter 12 - Implement deleting invoices

This commit is contained in:
2024-01-03 09:38:23 +01:00
parent 32871e3ff4
commit 3e4959dd9d
2 changed files with 9 additions and 2 deletions

View File

@@ -56,3 +56,8 @@ export async function updateInvoice(id: string, formData: FormData) {
revalidatePath('/dashboard/invoices'); revalidatePath('/dashboard/invoices');
redirect('/dashboard/invoices'); redirect('/dashboard/invoices');
} }
export async function deleteInvoice(id: string) {
await sql`DELETE FROM invoices WHERE id = ${id}`;
revalidatePath('/dashboard/invoices');
}

View File

@@ -1,5 +1,6 @@
import { PencilIcon, PlusIcon, TrashIcon } from '@heroicons/react/24/outline'; import { PencilIcon, PlusIcon, TrashIcon } from '@heroicons/react/24/outline';
import Link from 'next/link'; import Link from 'next/link';
import {deleteInvoice} from "@/app/lib/actions";
export function CreateInvoice() { export function CreateInvoice() {
return ( return (
@@ -25,12 +26,13 @@ export function UpdateInvoice({ id }: { id: string }) {
} }
export function DeleteInvoice({ id }: { id: string }) { export function DeleteInvoice({ id }: { id: string }) {
const deleteInvoiceWithId = deleteInvoice.bind(null, id);
return ( return (
<> <form action={deleteInvoiceWithId}>
<button className="rounded-md border p-2 hover:bg-gray-100"> <button className="rounded-md border p-2 hover:bg-gray-100">
<span className="sr-only">Delete</span> <span className="sr-only">Delete</span>
<TrashIcon className="w-5" /> <TrashIcon className="w-5" />
</button> </button>
</> </form>
); );
} }