@mitesh-bhagwant/reactjs-invoice
Version:
Invoices for React.js
45 lines (44 loc) • 2.3 kB
JavaScript
"use client";
import { jsx as _jsx } from "react/jsx-runtime";
import { useEffect, useState } from "react";
import { PDFDownloadLink } from "@react-pdf/renderer";
import { BasicInvoice } from "../invoices/BasicInvoice";
// Import other templates as they're created
// import { ProInvoice } from "../invoices/ProInvoice";
export var DownloadInvoiceButton = function (_a) {
var _b = _a.fileName, fileName = _b === void 0 ? "invoice.pdf" : _b, _c = _a.className, className = _c === void 0 ? "" : _c, children = _a.children, invoiceData = _a.invoiceData, _d = _a.template, template = _d === void 0 ? "default" : _d, _e = _a.disabled, disabled = _e === void 0 ? false : _e;
var _f = useState(false), mounted = _f[0], setMounted = _f[1];
useEffect(function () {
setMounted(true);
}, []);
// Validate invoice data
// Select template based on type
var getInvoiceTemplate = function () {
switch (template) {
case "default":
return _jsx(BasicInvoice, { invoiceData: invoiceData });
// case "pro":
// return <ProInvoice data={invoiceData} />;
// case "modern":
// return <ModernInvoice data={invoiceData} />;
default:
return _jsx(BasicInvoice, { invoiceData: invoiceData });
}
};
// Show loading state during SSR
if (!mounted) {
return (_jsx("button", { disabled: true, className: className, children: children || "Preparing..." }));
}
// Show disabled state
if (disabled) {
return (_jsx("button", { disabled: true, className: className, children: children || "Download PDF" }));
}
return (_jsx(PDFDownloadLink, { document: getInvoiceTemplate(), fileName: fileName, children: function (_a) {
var loading = _a.loading, error = _a.error;
if (error) {
return (_jsx("button", { disabled: true, className: className, title: "Error generating PDF", children: children || "Error" }));
}
return loading ? (_jsx("button", { disabled: true, className: className, children: children || "Generating..." })) : (_jsx("button", { className: className, children: children || "Download PDF" }));
} }));
};
export default DownloadInvoiceButton;