Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 180 181 182 183 184 185 186 187 | 13x 13x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 4x 4x 4x 4x 17x 11x 11x 11x 17x 17x 17x 11x 11x 11x 10x 10x 10x 1x 11x 17x 4x 1x 17x 6x 12x | import { Font, pdf } from "@react-pdf/renderer";
import { usePayBuilderForm } from "@sections/PayBuilder/provider/PayBuilderFormProvider";
import GiveWhyteLight from "@assets/fonts/GiveWhyte/GiveWhyte-Light.woff";
import GiveWhyteBold from "@assets/fonts/GiveWhyte/ttf/GiveWhyte-Bold.ttf";
import GiveWhyteMedium from "@assets/fonts/GiveWhyte/ttf/GiveWhyte-Medium.ttf";
import GiveWhyteRegular from "@assets/fonts/GiveWhyte/ttf/GiveWhyte-Regular.ttf";
import { DEFAULT_INVOICE_REFERENCE } from "@sections/PayBuilder/consts";
import { useCart } from "@sections/PayBuilder/provider/CartContext";
import { MediaItem } from "@sections/PayBuilder/provider/provider.type";
import DOMPurify from "dompurify";
import { RefObject, useEffect, useRef, useState } from "react";
import "react-draft-wysiwyg-next/dist/react-draft-wysiwyg.css";
import { Document as VDocument, Page as VPage } from "react-pdf";
import { parseAmount } from "@utils/index";
import { isUrl } from "@sections/PayBuilder/helpers";
import InvoicePDFPreview from "./InvoicePDFPreview";
import {
extractParagraphs,
getHTMLFromWysiwyg,
isLogoMediaItem,
} from "./utils";
import { useIsPreviewMode } from "@sections/PayBuilder/provider/useIsPreviewMode";
import { useCardProcessingFees } from "@sections/PayBuilder/Checkout/hooks/useCardProcessingFees";
Font.register({
family: "Give Whyte",
fonts: [
{
src: GiveWhyteRegular,
fontWeight: "normal",
},
{
src: GiveWhyteMedium,
fontWeight: "medium",
},
{
src: GiveWhyteBold,
fontWeight: "bold",
},
{
src: GiveWhyteLight,
fontWeight: "light",
},
],
});
const PDFPage = ({ from }: { from: any }) => {
const { cartItems, subTotal, displayFees, fees } = useCart();
const { isPreviewMode } = useIsPreviewMode();
// added useCardProcessingFees to execute the responsible useEffect for setDisplayFees
useCardProcessingFees();
const { methods, data, customer } = usePayBuilderForm();
const [logoDimensions, setLogoDimensions] = useState<number | undefined>(
undefined,
);
const values = methods.getValues();
const {
About: { endsAt },
Style: { logo },
} = values;
const [pdfUrl, setPdfUrl] = useState("");
const [numPages, setNumPages] = useState<number | null>(null);
const pdfWrapperRef = useRef<RefObject<HTMLDivElement>>();
const [width, setWidth] = useState<number>();
useEffect(() => {
const resizeObserver = new ResizeObserver((entries) => {
for (const entry of entries) {
setWidth(entry.target.clientWidth * 0.95);
}
});
resizeObserver.observe((pdfWrapperRef as any).current);
return () => {
resizeObserver.disconnect();
};
}, []);
const getParagraphs = () => {
const shouldLogoBeImage = isLogoMediaItem(logo);
Iif (
logo &&
Object.keys(logo).length > 0 &&
(typeof logo === "string" || !shouldLogoBeImage)
) {
if (
(typeof logo === "string" && isUrl(logo)) ||
(typeof logo !== "string" && !(logo as any).URL)
)
return [];
const markup = getHTMLFromWysiwyg({
logo: typeof logo === "string" ? logo : (logo as any).URL,
});
if (markup) {
const sanitizedMarkup = DOMPurify.sanitize(markup);
const paragraphs = extractParagraphs(sanitizedMarkup);
return paragraphs;
}
return [];
} else {
return [];
}
};
const invoiceDate = data?.createdAt * 1000 || new Date();
const invoiceReference = data?.reference || DEFAULT_INVOICE_REFERENCE;
useEffect(() => {
const generatePdf = async () => {
try {
const blob = await pdf(
<InvoicePDFPreview
values={values}
invoiceDate={invoiceDate}
cartItems={cartItems}
from={from}
invoiceReference={invoiceReference}
to={customer}
subTotal={subTotal}
paragraphs={getParagraphs()}
displayFees={displayFees}
fees={isPreviewMode ? "0.00" : parseAmount(fees)}
/>,
).toBlob();
const pdfUrl = URL.createObjectURL(blob);
setPdfUrl(pdfUrl);
return () => URL.revokeObjectURL(pdfUrl);
} catch (error) {
console.error("PDF rendering failed:", error);
}
};
generatePdf();
}, [logo, cartItems, endsAt, logoDimensions, customer, displayFees, fees]);
useEffect(() => {
if (logo && (logo as MediaItem)?.logoSize) {
setLogoDimensions((logo as MediaItem)?.logoSize);
}
}, [(logo as MediaItem)?.logoSize]);
return (
<>
<div
id="preview"
style={{
maxHeight: "100%",
height: "100%",
width: "100%",
overflowY: "auto",
margin: "0 auto",
}}
ref={pdfWrapperRef as any}
>
{pdfUrl && (
<VDocument
file={pdfUrl}
loading={<div style={{ backgroundColor: "#fff" }}></div>}
onLoadSuccess={({ numPages }) => setNumPages(numPages)}
>
{Array.from(new Array(numPages), (_, index) => (
<div key={`page_${index + 1}`} style={{ marginBottom: "20px" }}>
<VPage
pageNumber={index + 1}
renderTextLayer={false}
renderAnnotationLayer={false}
width={width || undefined}
/>
</div>
))}
</VDocument>
)}
</div>
</>
);
};
export default PDFPage;
|