Skip to content

Free & Open-Source Invoice Generator - No Sign-Up, No Ads, Instant PDF Export, 100% In-Browser, and Fully Customizable Templates.

refactor: extract initial invoice data to constants and improve error page recovery (#55)

Vlad Sazonaub0502330e1c5
diff --git a/src/app/constants/index.ts b/src/app/constants/index.ts
new file mode 100644
index 0000000..1aaff62
--- /dev/null
+++ b/src/app/constants/index.ts
@@ -0,0 +1,85 @@
+"use client";
+
+import {
+ DEFAULT_SELLER_DATA,
+ SUPPORTED_CURRENCIES,
+ SUPPORTED_LANGUAGES,
+ SUPPORTED_DATE_FORMATS,
+ type InvoiceData,
+} from "../schema";
+import dayjs from "dayjs";
+
+const today = dayjs().format("YYYY-MM-DD");
+const lastDayOfMonth = dayjs().endOf("month").format("YYYY-MM-DD");
+const invoiceCurrentMonthAndYear = dayjs().format("MM-YYYY");
+const paymentDue = dayjs(today).add(14, "days").format("YYYY-MM-DD");
+
+const EUR = SUPPORTED_CURRENCIES[0];
+const EN = SUPPORTED_LANGUAGES[0];
+const DEFAULT_DATE_FORMAT = SUPPORTED_DATE_FORMATS[0];
+
+export const INITIAL_INVOICE_DATA = {
+ language: EN,
+ currency: EUR,
+ invoiceNumber: `1/${invoiceCurrentMonthAndYear}`,
+
+ dateOfIssue: today,
+ dateOfService: lastDayOfMonth,
+ dateFormat: DEFAULT_DATE_FORMAT,
+
+ invoiceType: "Reverse Charge",
+ invoiceTypeFieldIsVisible: true,
+
+ seller: DEFAULT_SELLER_DATA,
+ buyer: {
+ name: "Buyer name",
+ address: "Buyer address",
+ vatNo: "Buyer vat number",
+ vatNoFieldIsVisible: true,
+ email: "[email protected]",
+ },
+ items: [
+ {
+ invoiceItemNumberIsVisible: true,
+
+ name: "Item name",
+ nameFieldIsVisible: true,
+
+ typeOfGTU: "",
+ typeOfGTUFieldIsVisible: true,
+
+ amount: 1,
+ amountFieldIsVisible: true,
+
+ unit: "service",
+ unitFieldIsVisible: true,
+
+ netPrice: 0,
+ netPriceFieldIsVisible: true,
+
+ vat: "NP",
+ vatFieldIsVisible: true,
+
+ netAmount: 0,
+ netAmountFieldIsVisible: true,
+
+ vatAmount: 0.0,
+ vatAmountFieldIsVisible: true,
+
+ preTaxAmount: 0,
+ preTaxAmountFieldIsVisible: true,
+ },
+ ],
+ total: 0,
+ paymentMethod: "wire transfer",
+
+ paymentDue: paymentDue,
+
+ notes: "Reverse charge",
+ notesFieldIsVisible: true,
+
+ vatTableSummaryIsVisible: true,
+ paymentMethodFieldIsVisible: true,
+ personAuthorizedToReceiveFieldIsVisible: true,
+ personAuthorizedToIssueFieldIsVisible: true,
+} as const satisfies InvoiceData;
diff --git a/src/app/error.tsx b/src/app/error.tsx
index 487fb66..1f7c5a0 100644
--- a/src/app/error.tsx
+++ b/src/app/error.tsx
@@ -4,6 +4,9 @@ import { Button } from "@/components/ui/button";
import { CircleAlert } from "lucide-react";
import { useEffect } from "react";
import { toast } from "sonner";
+import { PDF_DATA_LOCAL_STORAGE_KEY } from "./components/invoice-form";
+import { INITIAL_INVOICE_DATA } from "./constants";
+import { useOpenPanel } from "@openpanel/nextjs";
export default function Error({
error,
@@ -12,6 +15,8 @@ export default function Error({
error: Error & { digest?: string };
reset: () => void;
}) {
+ const openPanel = useOpenPanel();
+
useEffect(() => {
// Log the error to an error reporting service
console.error(error);
@@ -29,8 +34,15 @@ export default function Error({
<div className="flex h-dvh flex-col items-center justify-center gap-4">
<div className="flex flex-col items-center justify-center gap-4">
<ErrorMessage>
- Something went wrong. Please try to refresh the page or contact
- support via this{" "}
+ Something went wrong. Please try to refresh the page or fill a bug
+ report{" "}
+ <a
+ href="https://pdfinvoicegenerator.userjot.com/board/bugs"
+ className="underline"
+ >
+ here
+ </a>{" "}
+ or contact support via this{" "}
<a href="mailto:[email protected]" className="underline">
link
</a>
@@ -38,12 +50,45 @@ export default function Error({
<Button
onClick={
// Attempt to recover by trying to re-render the segment
- () => reset()
+ () => {
+ reset();
+ openPanel.track("error_button_try_again_clicked");
+ }
}
_variant="outline"
>
Try again
</Button>
+ <Button
+ onClick={() => {
+ try {
+ // Clear the invoice data and start from scratch
+ localStorage.setItem(
+ PDF_DATA_LOCAL_STORAGE_KEY,
+ JSON.stringify(INITIAL_INVOICE_DATA)
+ );
+
+ // Attempt to recover by trying to re-render the segment
+ reset();
+
+ toast.success("Invoice data cleared", {
+ closeButton: true,
+ richColors: true,
+ });
+
+ openPanel.track("error_button_start_from_scratch_clicked");
+ } catch (error) {
+ console.error(error);
+
+ toast.error("Error clearing the invoice data", {
+ closeButton: true,
+ richColors: true,
+ });
+ }
+ }}
+ >
+ Clear the Invoice Data and Start from Scratch
+ </Button>
</div>
</div>
);
diff --git a/src/app/page.tsx b/src/app/page.tsx
index ddb50ca..61f2d76 100644
--- a/src/app/page.tsx
+++ b/src/app/page.tsx
@@ -1,17 +1,9 @@
"use client";
-import {
- DEFAULT_SELLER_DATA,
- invoiceSchema,
- SUPPORTED_CURRENCIES,
- SUPPORTED_DATE_FORMATS,
- SUPPORTED_LANGUAGES,
- type InvoiceData,
-} from "@/app/schema";
+import { invoiceSchema, type InvoiceData } from "@/app/schema";
import { Button } from "@/components/ui/button";
import { CustomTooltip, TooltipProvider } from "@/components/ui/tooltip";
import { useMediaQuery } from "@/hooks/use-media-query";
-import dayjs from "dayjs";
import {
compressToEncodedURIComponent,
decompressFromEncodedURIComponent,
@@ -27,6 +19,7 @@ import {
import { InvoicePDFDownloadLink } from "./components/invoice-pdf-download-link";
import { InvoicePdfTemplate } from "./components/invoice-pdf-template";
import { RegenerateInvoiceButton } from "./components/regenerate-invoice-button";
+import { INITIAL_INVOICE_DATA } from "./constants";
const InvoicePDFViewer = dynamic(
() =>
@@ -47,81 +40,6 @@ const InvoicePDFViewer = dynamic(
}
);
-const today = dayjs().format("YYYY-MM-DD");
-const lastDayOfMonth = dayjs().endOf("month").format("YYYY-MM-DD");
-const invoiceCurrentMonthAndYear = dayjs().format("MM-YYYY");
-const paymentDue = dayjs(today).add(14, "days").format("YYYY-MM-DD");
-
-const EUR = SUPPORTED_CURRENCIES[0];
-const EN = SUPPORTED_LANGUAGES[0];
-const DEFAULT_DATE_FORMAT = SUPPORTED_DATE_FORMATS[0];
-
-const initialInvoiceData = {
- language: EN,
- currency: EUR,
- invoiceNumber: `1/${invoiceCurrentMonthAndYear}`,
-
- dateOfIssue: today,
- dateOfService: lastDayOfMonth,
- dateFormat: DEFAULT_DATE_FORMAT,
-
- invoiceType: "Reverse Charge",
- invoiceTypeFieldIsVisible: true,
-
- seller: DEFAULT_SELLER_DATA,
- buyer: {
- name: "Buyer name",
- address: "Buyer address",
- vatNo: "Buyer vat number",
- vatNoFieldIsVisible: true,
- email: "[email protected]",
- },
- items: [
- {
- invoiceItemNumberIsVisible: true,
-
- name: "Item name",
- nameFieldIsVisible: true,
-
- typeOfGTU: "",
- typeOfGTUFieldIsVisible: true,
-
- amount: 1,
- amountFieldIsVisible: true,
-
- unit: "service",
- unitFieldIsVisible: true,
-
- netPrice: 0,
- netPriceFieldIsVisible: true,
-
- vat: "NP",
- vatFieldIsVisible: true,
-
- netAmount: 0,
- netAmountFieldIsVisible: true,
-
- vatAmount: 0.0,
- vatAmountFieldIsVisible: true,
-
- preTaxAmount: 0,
- preTaxAmountFieldIsVisible: true,
- },
- ],
- total: 0,
- paymentMethod: "wire transfer",
-
- paymentDue: paymentDue,
-
- notes: "Reverse charge",
- notesFieldIsVisible: true,
-
- vatTableSummaryIsVisible: true,
- paymentMethodFieldIsVisible: true,
- personAuthorizedToReceiveFieldIsVisible: true,
- personAuthorizedToIssueFieldIsVisible: true,
-} as const satisfies InvoiceData;
-
export default function Home() {
const router = useRouter();
const searchParams = useSearchParams();
@@ -162,15 +80,14 @@ export default function Home() {
const parsedData = invoiceSchema.parse(json);
setInvoiceDataState(parsedData);
- // setInvoiceDataState(restOfInvoiceData);
} else {
// if no data in local storage, set initial data
- setInvoiceDataState(initialInvoiceData);
+ setInvoiceDataState(INITIAL_INVOICE_DATA);
}
} catch (error) {
console.error("Failed to load saved invoice data:", error);
- setInvoiceDataState(initialInvoiceData);
+ setInvoiceDataState(INITIAL_INVOICE_DATA);
}
};