1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
"use client";
import { Invoice, InvoiceSchema } from "@/models/invoice";
import { zodResolver } from "@hookform/resolvers/zod";
import {
useFieldArray,
useForm,
useWatch,
SubmitHandler,
} from "react-hook-form";
import { Input } from "./ui/input";
import { cn } from "@/utils/cn";
import { Button } from "./ui/button";
import { generateInvoice } from "@/lib/generate";
const defaultItem = {
description: "",
quantity: 1,
rate: 0,
};
const defaultValues: Invoice = {
invoice_number: 0,
from: "",
line_items: [defaultItem],
currency: "USD",
};
export const InvoiceGenerator = () => {
const {
register,
handleSubmit,
control,
watch,
formState: { errors },
} = useForm<Invoice>({
resolver: zodResolver(InvoiceSchema),
defaultValues,
});
const { fields, append, remove } = useFieldArray({
control,
name: "line_items",
});
const lineItems = useWatch({ control, name: "line_items" });
const currency = watch("currency", "USD");
const onSubmit: SubmitHandler<Invoice> = async (data) => {
console.log(data);
const blob = await generateInvoice(data);
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = "invoice.pdf";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
a.remove();
};
return (
<form
onSubmit={handleSubmit(onSubmit)}
className="max-w-5xl rounded-sm border p-4 shadow-lg"
>
<div className="flex justify-between gap-4">
<section className="grid auto-rows-min gap-2 md:grid-cols-2">
<Input
{...register("from")}
placeholder="Who is this from?"
className="md:col-span-2"
/>
{errors["from"] && (
<p className="md:col-span-2">{errors["from"].message ?? "Error"}</p>
)}
<div className="grid">
<label>Bill To</label>
<Input
{...register("bill_to")}
className="w-full"
placeholder="Who is this for?"
/>
</div>
<div className="grid">
<label>Ship To</label>
<Input
{...register("ship_to")}
className="w-full"
placeholder="(optional)"
/>
</div>
</section>
<section className="grid auto-rows-min grid-cols-2 gap-2">
<label>Invoice #</label>
<Input {...register("invoice_number", { valueAsNumber: true })} />
<label>Date</label>
<Input {...register("date")} type="date" />
<label>Due Date</label>
<Input {...register("due_date")} type="date" />
<label>PO number</label>
<Input {...register("po_number")} />
</section>
</div>
<h2>Your Items</h2>
<div
className={cn(
"bg-foreground text-background rounded-sm px-2 py-1",
"mb-2 grid grid-cols-11 gap-2",
)}
>
<p className="col-span-7">Item</p>
<p className="pl-2">Quantity</p>
<p className="pl-2">Rate</p>
<p className="col-span-2 pr-4 text-right">Amount</p>
</div>
<section className="space-y-2">
{fields.map((f, ind) => (
<div key={f.id} className="group relative grid grid-cols-11 gap-2">
<Input
{...register(`line_items.${ind}.description`)}
placeholder="Description of item/service..."
className="col-span-7"
/>
<Input
{...register(`line_items.${ind}.quantity`, {
valueAsNumber: true,
})}
/>
<Input
{...register(`line_items.${ind}.rate`, { valueAsNumber: true })}
/>
<div className="relative col-span-2 my-auto flex justify-end pr-6 text-right">
<p>
{(
(lineItems[ind]?.rate ?? 0) * (lineItems[ind]?.quantity ?? 0)
).toLocaleString(undefined, { style: "currency", currency })}
</p>
<Button
type="button"
onClick={() => remove(ind)}
className={cn(
"absolute -right-1 m-auto hidden size-fit",
"border-none group-hover:block hover:text-gray-500",
)}
>
X
</Button>
</div>
</div>
))}
<Button
type="button"
className="border-green-700 text-green-700 hover:bg-gray-100"
onClick={() => append(defaultItem)}
>
+ Item
</Button>
</section>
<section className="pr-6 text-right text-xl">
Subtotal:{" "}
<b>
{lineItems
.reduce(
(acc, cur) => acc + (cur.rate ?? 0) * (cur.quantity ?? 0),
0,
)
.toLocaleString(undefined, { style: "currency", currency })}
</b>
</section>
<section className={cn("mt-4 flex flex-row-reverse")}>
<Button
className={cn("border-green-700 text-green-700", "hover:bg-gray-100")}
>
Download
</Button>
</section>
</form>
);
};
|