Chapter 12 - Implement updating an invoice
This commit is contained in:
26
app/dashboard/invoices/[id]/edit/page.tsx
Normal file
26
app/dashboard/invoices/[id]/edit/page.tsx
Normal file
@@ -0,0 +1,26 @@
|
||||
import Form from '@/app/ui/invoices/edit-form';
|
||||
import Breadcrumbs from '@/app/ui/invoices/breadcrumbs';
|
||||
import {fetchCustomers, fetchInvoiceById} from '@/app/lib/data';
|
||||
|
||||
export default async function Page({ params }: { params: { id: string } }) {
|
||||
const id = params.id;
|
||||
const [invoice, customers] = await Promise.all([
|
||||
fetchInvoiceById(id),
|
||||
fetchCustomers(),
|
||||
]);
|
||||
return (
|
||||
<main>
|
||||
<Breadcrumbs
|
||||
breadcrumbs={[
|
||||
{ label: 'Invoices', href: '/dashboard/invoices' },
|
||||
{
|
||||
label: 'Edit Invoice',
|
||||
href: `/dashboard/invoices/${id}/edit`,
|
||||
active: true,
|
||||
},
|
||||
]}
|
||||
/>
|
||||
<Form invoice={invoice} customers={customers} />
|
||||
</main>
|
||||
);
|
||||
}
|
||||
@@ -13,6 +13,8 @@ const FormSchema = z.object({
|
||||
date: z.string(),
|
||||
});
|
||||
|
||||
const UpdateInvoice = FormSchema.omit({ id: true, date: true });
|
||||
|
||||
const CreateInvoice = FormSchema.omit({ id: true, date: true });
|
||||
|
||||
export async function createInvoice(formData: FormData) {
|
||||
@@ -34,3 +36,23 @@ export async function createInvoice(formData: FormData) {
|
||||
revalidatePath('/dashboard/invoices');
|
||||
redirect('/dashboard/invoices');
|
||||
}
|
||||
|
||||
|
||||
export async function updateInvoice(id: string, formData: FormData) {
|
||||
const { customerId, amount, status } = UpdateInvoice.parse({
|
||||
customerId: formData.get('customerId'),
|
||||
amount: formData.get('amount'),
|
||||
status: formData.get('status'),
|
||||
});
|
||||
|
||||
const amountInCents = amount * 100;
|
||||
|
||||
await sql`
|
||||
UPDATE invoices
|
||||
SET customer_id = ${customerId}, amount = ${amountInCents}, status = ${status}
|
||||
WHERE id = ${id}
|
||||
`;
|
||||
|
||||
revalidatePath('/dashboard/invoices');
|
||||
redirect('/dashboard/invoices');
|
||||
}
|
||||
@@ -16,7 +16,7 @@ export function CreateInvoice() {
|
||||
export function UpdateInvoice({ id }: { id: string }) {
|
||||
return (
|
||||
<Link
|
||||
href="/dashboard/invoices"
|
||||
href={`/dashboard/invoices/${id}/edit`}
|
||||
className="rounded-md border p-2 hover:bg-gray-100"
|
||||
>
|
||||
<PencilIcon className="w-5" />
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
} from '@heroicons/react/24/outline';
|
||||
import Link from 'next/link';
|
||||
import { Button } from '@/app/ui/button';
|
||||
import { updateInvoice } from '@/app/lib/actions';
|
||||
|
||||
export default function EditInvoiceForm({
|
||||
invoice,
|
||||
@@ -17,8 +18,10 @@ export default function EditInvoiceForm({
|
||||
invoice: InvoiceForm;
|
||||
customers: CustomerField[];
|
||||
}) {
|
||||
|
||||
const updateInvoiceWithId = updateInvoice.bind(null, invoice.id);
|
||||
return (
|
||||
<form>
|
||||
<form action={updateInvoiceWithId}>
|
||||
<div className="rounded-md bg-gray-50 p-4 md:p-6">
|
||||
{/* Customer Name */}
|
||||
<div className="mb-4">
|
||||
|
||||
Reference in New Issue
Block a user