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(),
|
date: z.string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const UpdateInvoice = FormSchema.omit({ id: true, date: true });
|
||||||
|
|
||||||
const CreateInvoice = FormSchema.omit({ id: true, date: true });
|
const CreateInvoice = FormSchema.omit({ id: true, date: true });
|
||||||
|
|
||||||
export async function createInvoice(formData: FormData) {
|
export async function createInvoice(formData: FormData) {
|
||||||
@@ -31,6 +33,26 @@ export async function createInvoice(formData: FormData) {
|
|||||||
VALUES (${customerId}, ${amountInCents}, ${status}, ${date})
|
VALUES (${customerId}, ${amountInCents}, ${status}, ${date})
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
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');
|
revalidatePath('/dashboard/invoices');
|
||||||
redirect('/dashboard/invoices');
|
redirect('/dashboard/invoices');
|
||||||
}
|
}
|
||||||
@@ -16,7 +16,7 @@ export function CreateInvoice() {
|
|||||||
export function UpdateInvoice({ id }: { id: string }) {
|
export function UpdateInvoice({ id }: { id: string }) {
|
||||||
return (
|
return (
|
||||||
<Link
|
<Link
|
||||||
href="/dashboard/invoices"
|
href={`/dashboard/invoices/${id}/edit`}
|
||||||
className="rounded-md border p-2 hover:bg-gray-100"
|
className="rounded-md border p-2 hover:bg-gray-100"
|
||||||
>
|
>
|
||||||
<PencilIcon className="w-5" />
|
<PencilIcon className="w-5" />
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
} from '@heroicons/react/24/outline';
|
} from '@heroicons/react/24/outline';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { Button } from '@/app/ui/button';
|
import { Button } from '@/app/ui/button';
|
||||||
|
import { updateInvoice } from '@/app/lib/actions';
|
||||||
|
|
||||||
export default function EditInvoiceForm({
|
export default function EditInvoiceForm({
|
||||||
invoice,
|
invoice,
|
||||||
@@ -17,8 +18,10 @@ export default function EditInvoiceForm({
|
|||||||
invoice: InvoiceForm;
|
invoice: InvoiceForm;
|
||||||
customers: CustomerField[];
|
customers: CustomerField[];
|
||||||
}) {
|
}) {
|
||||||
|
|
||||||
|
const updateInvoiceWithId = updateInvoice.bind(null, invoice.id);
|
||||||
return (
|
return (
|
||||||
<form>
|
<form action={updateInvoiceWithId}>
|
||||||
<div className="rounded-md bg-gray-50 p-4 md:p-6">
|
<div className="rounded-md bg-gray-50 p-4 md:p-6">
|
||||||
{/* Customer Name */}
|
{/* Customer Name */}
|
||||||
<div className="mb-4">
|
<div className="mb-4">
|
||||||
|
|||||||
Reference in New Issue
Block a user