From 06e53d05dc23494d4f0cfd506c01cde13dadedd4 Mon Sep 17 00:00:00 2001 From: Donkie Date: Sat, 15 Jul 2023 14:39:20 +0200 Subject: [PATCH] Client: Created QR code printing dialog component --- .../src/components/qrCodePrintingDialog.tsx | 359 ++++++++++++++++++ 1 file changed, 359 insertions(+) create mode 100644 client/src/components/qrCodePrintingDialog.tsx diff --git a/client/src/components/qrCodePrintingDialog.tsx b/client/src/components/qrCodePrintingDialog.tsx new file mode 100644 index 0000000..061bc0a --- /dev/null +++ b/client/src/components/qrCodePrintingDialog.tsx @@ -0,0 +1,359 @@ +import React, { useRef } from "react"; +import { + Modal, + Slider, + Button, + Select, + Row, + Col, + QRCode, + Form, + Divider, + Switch, +} from "antd"; +import ReactToPrint from "react-to-print"; +import { useSavedState } from "../utils/saveload"; + +interface QRCodeData { + id: number; + content: string; +} + +interface QRCodePrintingDialogProps { + visible: boolean; + onCancel: () => void; +} + +interface PaperDimensions { + width: number; + height: number; +} + +const paperDimensions: { [key: string]: PaperDimensions } = { + A3: { + width: 297, + height: 420, + }, + A4: { + width: 210, + height: 297, + }, + A5: { + width: 148, + height: 210, + }, + Letter: { + width: 216, + height: 279, + }, + Legal: { + width: 216, + height: 356, + }, + Tabloid: { + width: 279, + height: 432, + }, +}; + +const QRCodePrintingDialog: React.FC = ({ + visible, + onCancel, +}) => { + const [marginLeft, setMarginLeft] = useSavedState("print-marginLeft", 10); + const [marginTop, setMarginTop] = useSavedState("print-marginTop", 10); + const [marginRight, setMarginRight] = useSavedState("print-marginRight", 10); + const [marginBottom, setMarginBottom] = useSavedState( + "print-marginBottom", + 10 + ); + const [codesPerRow, setCodesPerRow] = useSavedState("print-codesPerRow", 3); + const [paperSize, setPaperSize] = useSavedState("print-paperSize", "A4"); + const [rowHeight, setRowHeight] = useSavedState("print-rowHeight", 70); + const [showContent, setShowContent] = useSavedState( + "print-showContent", + true + ); + const [previewScale, setPreviewScale] = useSavedState( + "print-previewScale", + 0.6 + ); + const [showBorder, setShowBorder] = useSavedState("print-showBorder", true); + const [textSize, setTextSize] = useSavedState("print-textSize", 7); + + const qrCodeData: QRCodeData[] = Array.from({ length: 10 }).map( + (_, index) => ({ + id: index, + content: `QR Code ${index + 1}`, + }) + ); + + const paperWidth = paperDimensions[paperSize].width; + const paperHeight = paperDimensions[paperSize].height; + + const printRef = useRef(null); + + const codesPerPage = + codesPerRow * + Math.floor((paperHeight - marginTop - marginBottom) / rowHeight); + const pageBlocks = []; + for (let i = 0; i < qrCodeData.length; i += 1) { + if (i % codesPerPage === 0) { + pageBlocks.push(qrCodeData.slice(i, i + codesPerPage)); + } + } + const rowBlocks = pageBlocks.map((pageBlock, idx) => ( +
+ + {pageBlock.map((qrCode) => ( +
+ + {showContent && ( +
+ {qrCode.content} +
+ )} +
+ ))} +
+
+ )); + + return ( + } + content={() => printRef.current} + />, + ]} + width={1000} // Set the modal width to accommodate the preview + > + + +
+
+ + {rowBlocks} +
+
+ + +
+ + { + setMarginLeft(value); + }} + /> + + + { + setMarginTop(value); + }} + /> + + + { + setMarginRight(value); + }} + /> + + + { + setMarginBottom(value); + }} + /> + + + { + setCodesPerRow(value); + }} + /> + + + + + + { + setRowHeight(value); + }} + /> + + + { + setTextSize(value); + }} + /> + + + setShowContent(checked)} + /> + + + setShowBorder(checked)} + /> + + + + { + setPreviewScale(value); + }} + /> + + + +
+
+ ); +}; + +export default QRCodePrintingDialog;