|
| 1 | +import React, { useRef } from 'react'; |
| 2 | +import { QRCodeSVG } from 'qrcode.react'; |
| 3 | +import { Button } from '@chakra-ui/react'; |
| 4 | + |
| 5 | +interface EventQRCodeProps { |
| 6 | + eventKey: string; |
| 7 | + size?: number; |
| 8 | + color?: string; |
| 9 | + inNotification?: boolean; |
| 10 | +} |
| 11 | + |
| 12 | +const EventQRCode: React.FC<EventQRCodeProps> = ({ |
| 13 | + eventKey, |
| 14 | + size = 128, |
| 15 | + color = '#d4696a', // does this set a default color |
| 16 | + inNotification = false |
| 17 | +}) => { |
| 18 | + // creates react reference that can point to an svg element |
| 19 | + // useRef is react hook that creates mutable reference to object |
| 20 | + const qrRef = useRef<SVGSVGElement>(null); |
| 21 | + |
| 22 | + const baseUrl = import.meta.env.DEV |
| 23 | + ? 'http://127.0.0.1:8080' // development frontend URL |
| 24 | + : 'https://points.illinoiswcs.org'; // production frontend URL |
| 25 | + |
| 26 | + const loadingUrl = `${baseUrl}/success`; // redirect |
| 27 | + |
| 28 | + // download as svg |
| 29 | + const downloadSVG = (): void => { |
| 30 | + if (!qrRef.current) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + const clonedSvg = qrRef.current.cloneNode(true) as SVGSVGElement; |
| 35 | + |
| 36 | + // set attributes on the cloned SVG |
| 37 | + clonedSvg.setAttribute('viewBox', `0 0 ${size} ${size}`); |
| 38 | + clonedSvg.setAttribute('preserveAspectRatio', 'xMidYMid meet'); |
| 39 | + clonedSvg.setAttribute('width', `${size}px`); // Add px unit |
| 40 | + clonedSvg.setAttribute('height', `${size}px`); // Add px unit |
| 41 | + |
| 42 | + // add style to force size |
| 43 | + clonedSvg.style.width = `${size}px`; |
| 44 | + clonedSvg.style.height = `${size}px`; |
| 45 | + |
| 46 | + // turns qr code visual (svg) into xml code |
| 47 | + const svgData = new XMLSerializer().serializeToString(qrRef.current); |
| 48 | + |
| 49 | + // creates binary large object (blob) containing svg image data as xml |
| 50 | + const svgBlob = new Blob([svgData], { |
| 51 | + type: 'image/svg_xml;charset=utf-8' |
| 52 | + }); |
| 53 | + // creates temp url pointing to blob |
| 54 | + const svgUrl = URL.createObjectURL(svgBlob); |
| 55 | + |
| 56 | + // link element |
| 57 | + const downloadLink = document.createElement('a'); |
| 58 | + // points link to temp url containing blob info |
| 59 | + downloadLink.href = svgUrl; |
| 60 | + // names the link |
| 61 | + downloadLink.download = `qr-code-${eventKey}.svg`; |
| 62 | + // adds link to page |
| 63 | + document.body.appendChild(downloadLink); |
| 64 | + // clicks link to start download |
| 65 | + downloadLink.click(); |
| 66 | + // removes link from page |
| 67 | + document.body.removeChild(downloadLink); |
| 68 | + // removes temp url created |
| 69 | + URL.revokeObjectURL(svgUrl); |
| 70 | + }; |
| 71 | + |
| 72 | + // download as png |
| 73 | + const downloadPNG = (): void => { |
| 74 | + if (!qrRef.current) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // creates drawing canvas in memory |
| 79 | + const scale = 4; |
| 80 | + const canvas = document.createElement('canvas'); |
| 81 | + canvas.width = size * scale; |
| 82 | + canvas.height = size * scale; |
| 83 | + |
| 84 | + // tools for drawing on canvas |
| 85 | + const ctx = canvas.getContext('2d'); |
| 86 | + if (!ctx) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // smoothing |
| 91 | + ctx.imageSmoothingEnabled = true; |
| 92 | + ctx.imageSmoothingQuality = 'high'; |
| 93 | + ctx.scale(scale, scale); |
| 94 | + |
| 95 | + // creates new image object |
| 96 | + const svgData = new XMLSerializer().serializeToString(qrRef.current); |
| 97 | + const img = new Image(); |
| 98 | + |
| 99 | + // after image loads, draws it onto the canvas |
| 100 | + img.onload = () => { |
| 101 | + ctx.drawImage(img, 0, 0); |
| 102 | + |
| 103 | + // converts into png format |
| 104 | + const pngUrl = canvas.toDataURL('image/png'); |
| 105 | + |
| 106 | + const downloadLink = document.createElement('a'); |
| 107 | + downloadLink.href = pngUrl; |
| 108 | + downloadLink.download = `qr-code-${eventKey}.png`; |
| 109 | + document.body.appendChild(downloadLink); |
| 110 | + downloadLink.click(); |
| 111 | + document.body.removeChild(downloadLink); |
| 112 | + }; |
| 113 | + |
| 114 | + img.src = 'data:image/svg+xml;base64,' + btoa(svgData); |
| 115 | + }; |
| 116 | + |
| 117 | + return ( |
| 118 | + <div> |
| 119 | + <div |
| 120 | + style={ |
| 121 | + inNotification |
| 122 | + ? { |
| 123 | + marginLeft: 'auto', |
| 124 | + backgroundColor: '#c6f6d5', |
| 125 | + padding: '4px', |
| 126 | + borderRadius: '4px' |
| 127 | + } |
| 128 | + : {} |
| 129 | + } |
| 130 | + > |
| 131 | + <QRCodeSVG |
| 132 | + ref={qrRef} |
| 133 | + value={loadingUrl} |
| 134 | + size={size} |
| 135 | + fgColor={color} |
| 136 | + bgColor={inNotification ? '#d1fae5' : '#ffffff'} |
| 137 | + level="H" |
| 138 | + title={`QR Code for event ${eventKey}`} |
| 139 | + /> |
| 140 | + </div> |
| 141 | + <div style={{ marginTop: '10px' }}> |
| 142 | + <Button onClick={downloadPNG} mr={3} mt={5}> |
| 143 | + Download as PNG |
| 144 | + </Button> |
| 145 | + <Button onClick={downloadSVG} mt={5}> |
| 146 | + Download as SVG |
| 147 | + </Button> |
| 148 | + </div> |
| 149 | + </div> |
| 150 | + ); |
| 151 | +}; |
| 152 | + |
| 153 | +export default EventQRCode; |
0 commit comments