All files / src/sections/PayBuilder/components/InvoicePreview InvoicePDFPreview.tsx

69.23% Statements 9/13
61.53% Branches 8/13
75% Functions 3/4
75% Lines 9/12

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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213                                        14x               14x                                                 3x   3x 3x                                                           1x   1x                                                                                   3x                   14x                                                                                                                                                
import {
  Document,
  Image,
  Page,
  StyleSheet,
  Text,
  View,
} from "@react-pdf/renderer";
import LogoPDF from "@assets/images/LogoPDF.png";
import { Customer } from "@customTypes/customer.types";
import { ICartItem } from "@sections/PayBuilder/provider/CartContext";
import { MediaItem } from "@sections/PayBuilder/provider/provider.type";
import { addSizeToImage } from "@utils/image.helpers";
import "react-draft-wysiwyg-next/dist/react-draft-wysiwyg.css";
import { PDFFromToSection } from "./PDFFromToSection";
import { PDFHeader } from "./PDFHeader";
import PDFItemsTable from "./PDFItemsTable";
import { StyledText } from "./utils";
import { Font } from "@react-pdf/renderer";
 
Font.registerHyphenationCallback((word) => {
  // Split every N characters to force breaking
  const maxLength = 8;
  if (word.length <= maxLength) return [word];
 
  return word.match(new RegExp(`.{1,${maxLength}}`, "g")) || [word];
});
 
const InvoicePDFPreview = ({
  values,
  from,
  to,
  paragraphs,
  cartItems,
  subTotal,
  invoiceDate,
  invoiceReference,
  displayFees,
  fees,
}: {
  values: any;
  from: any;
  to: Customer | undefined;
  paragraphs: StyledText[];
  cartItems: ICartItem[];
  subTotal: string;
  invoiceDate: number | Date;
  invoiceReference: string;
  displayFees?: boolean;
  fees?: string;
}) => {
  const {
    Style: { logo },
  } = values;
 
  const dueDate = values.About.endsAt;
  return (
    <Document style={{ fontFamily: "Give Whyte" }}>
      <Page style={styles.body}>
        <View
          style={{
            gap: "24px",
            flex: 1,
          }}
        >
          <View
            id="header-section"
            style={{
              color: "#000",
              display: "flex",
              flexDirection: "row",
              justifyContent: "space-between",
              alignItems: "flex-start",
            }}
            fixed
          >
            <PDFHeader
              logo={logo}
              dueDate={dueDate}
              invoiceDate={invoiceDate}
              invoiceReference={invoiceReference}
            />
            {paragraphs.length > 0 && (
              <View style={styles.logoContainer}>
                {paragraphs.map((paragraph, index) => {
                  const { fontSize, fontFamily, ...rest } =
                    paragraph.style || {};
 
                  return (
                    <Text key={index} style={[styles.textRight, rest]}>
                      {paragraph.content}
                    </Text>
                  );
                })}
              </View>
            )}
 
            {typeof logo !== "string" &&
              logo &&
              logo.URL &&
              !paragraphs.length && (
                <Image
                  src={addSizeToImage((logo as any).URL, "original")}
                  style={{
                    borderRadius: 16,
                    height: (logo as MediaItem).logoSize,
                  }}
                />
              )}
          </View>
          <PDFFromToSection from={from} to={to} />
          <PDFItemsTable
            Items={cartItems}
            subTotal={subTotal}
            fees={fees}
            displayFees={displayFees}
          />
        </View>
        <View id="footer-section" style={styles.pageNumber} fixed>
          <>
            <View style={{ gap: "8px" }}>
              <Text style={styles.textFooter}>
                Transaction Secured and Encrypted by
              </Text>
 
              <Image src={LogoPDF} style={styles.logo} />
            </View>
            <Text
              style={{ fontSize: "8px" }}
              render={({ pageNumber, totalPages }) => {
                return `Page ${pageNumber} of ${totalPages}`;
              }}
            />
          </>
        </View>
      </Page>
    </Document>
  );
};
 
const styles = StyleSheet.create({
  body: {
    paddingTop: 48,
    paddingLeft: 48,
    paddingRight: 48,
    paddingBottom: 150,
  },
  title: {
    fontSize: 24,
    textAlign: "center",
    fontFamily: "Give Whyte",
  },
  author: {
    fontSize: 12,
    textAlign: "center",
    marginBottom: 40,
  },
  subtitle: {
    fontSize: 18,
    margin: 12,
    fontFamily: "Give Whyte",
  },
  page: {
    flexDirection: "column",
    backgroundColor: "#ffffff",
    padding: 30,
  },
  logoContainer: {
    maxWidth: 280,
  },
  textRight: {
    textAlign: "right",
  },
  logo: {
    width: 120,
    height: 12,
    objectFit: "cover",
  },
  text: {
    fontSize: 24,
    fontWeight: "light",
    lineHeight: "100%",
    fontFamily: "Give Whyte",
  },
  image: {
    marginVertical: 15,
    marginHorizontal: 100,
  },
  header: {
    fontSize: 12,
    marginBottom: 20,
    textAlign: "center",
    color: "grey",
  },
  pageNumber: {
    position: "absolute",
    bottom: 48,
    left: 48,
    right: 48,
    flexDirection: "row",
    justifyContent: "space-between",
    fontSize: 12,
    textAlign: "center",
    alignItems: "flex-end",
    color: "grey",
  },
  textFooter: {
    fontSize: 8,
  },
});
 
export default InvoicePDFPreview;