diff --git a/app/dashboard/invoices/create/page.tsx b/app/dashboard/invoices/create/page.tsx new file mode 100644 index 0000000..39b768e --- /dev/null +++ b/app/dashboard/invoices/create/page.tsx @@ -0,0 +1,23 @@ +import Form from '@/app/ui/invoices/create-form'; +import Breadcrumbs from '@/app/ui/invoices/breadcrumbs'; +import { fetchCustomers } from '@/app/lib/data'; + +export default async function Page() { + const customers = await fetchCustomers(); + + return ( +
+ +
+
+ ); +} \ No newline at end of file diff --git a/app/lib/actions.ts b/app/lib/actions.ts new file mode 100644 index 0000000..e4f9bd6 --- /dev/null +++ b/app/lib/actions.ts @@ -0,0 +1,36 @@ +'use server'; + +import { z } from 'zod'; +import { sql } from '@vercel/postgres'; +import { revalidatePath } from 'next/cache'; +import {redirect} from "next/navigation"; + +const FormSchema = z.object({ + id: z.string(), + customerId: z.string(), + amount: z.coerce.number(), + status: z.enum(['pending', 'paid']), + date: z.string(), +}); + +const CreateInvoice = FormSchema.omit({ id: true, date: true }); + +export async function createInvoice(formData: FormData) { + + const { customerId, amount, status } = CreateInvoice.parse({ + customerId: formData.get('customerId'), + amount: formData.get('amount'), + status: formData.get('status'), + }); + + const amountInCents = amount * 100; + const date = new Date().toISOString().split('T')[0]; + + await sql` + INSERT INTO invoices (customer_id, amount, status, date) + VALUES (${customerId}, ${amountInCents}, ${status}, ${date}) + `; + + revalidatePath('/dashboard/invoices'); + redirect('/dashboard/invoices'); +} \ No newline at end of file diff --git a/app/ui/invoices/create-form.tsx b/app/ui/invoices/create-form.tsx index 35099ce..8882ac5 100644 --- a/app/ui/invoices/create-form.tsx +++ b/app/ui/invoices/create-form.tsx @@ -7,10 +7,11 @@ import { UserCircleIcon, } from '@heroicons/react/24/outline'; import { Button } from '@/app/ui/button'; +import {createInvoice} from "@/app/lib/actions"; export default function Form({ customers }: { customers: CustomerField[] }) { return ( - +
{/* Customer Name */}