Chapter 12 - Implement updating an invoice

This commit is contained in:
2024-01-03 09:36:23 +01:00
parent 3e0842aef2
commit 32871e3ff4
4 changed files with 53 additions and 2 deletions

View File

@@ -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) {
@@ -31,6 +33,26 @@ export async function createInvoice(formData: FormData) {
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');
redirect('/dashboard/invoices');
}