Chapter 12 - Implement creating an invoice
This commit is contained in:
23
app/dashboard/invoices/create/page.tsx
Normal file
23
app/dashboard/invoices/create/page.tsx
Normal file
@@ -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 (
|
||||||
|
<main>
|
||||||
|
<Breadcrumbs
|
||||||
|
breadcrumbs={[
|
||||||
|
{ label: 'Invoices', href: '/dashboard/invoices' },
|
||||||
|
{
|
||||||
|
label: 'Create Invoice',
|
||||||
|
href: '/dashboard/invoices/create',
|
||||||
|
active: true,
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
/>
|
||||||
|
<Form customers={customers} />
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
||||||
36
app/lib/actions.ts
Normal file
36
app/lib/actions.ts
Normal file
@@ -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');
|
||||||
|
}
|
||||||
@@ -7,10 +7,11 @@ import {
|
|||||||
UserCircleIcon,
|
UserCircleIcon,
|
||||||
} from '@heroicons/react/24/outline';
|
} from '@heroicons/react/24/outline';
|
||||||
import { Button } from '@/app/ui/button';
|
import { Button } from '@/app/ui/button';
|
||||||
|
import {createInvoice} from "@/app/lib/actions";
|
||||||
|
|
||||||
export default function Form({ customers }: { customers: CustomerField[] }) {
|
export default function Form({ customers }: { customers: CustomerField[] }) {
|
||||||
return (
|
return (
|
||||||
<form>
|
<form action={createInvoice}>
|
||||||
<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