@movalib/movalib-commons 1.54.0 → 1.55.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,202 @@
1
+ import { DocumentScanner, QrCode2 } from '@mui/icons-material';
2
+ import {
3
+ Box,
4
+ Button,
5
+ FormControl,
6
+ Grid,
7
+ InputLabel,
8
+ MenuItem,
9
+ Select,
10
+ type SelectProps,
11
+ darken,
12
+ useTheme,
13
+ } from '@mui/material';
14
+ import './QRCode.css';
15
+
16
+ import html2canvas from 'html2canvas';
17
+ import QRCodeStyling, { type Options } from 'qr-code-styling';
18
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
19
+ import { useReactToPrint } from 'react-to-print';
20
+ import { DownloadedQRCode } from '../../DownloadedQRCode';
21
+ import QRCodeImage from '../../assets/images/leaf_yellow_small.png';
22
+ import { flexCenter } from '../../helpers/Tools';
23
+ import { PLVComponent, PrintSize } from './PLVComponent';
24
+
25
+ type SelectChoice = null | 'A3' | 'A4' | 'QR_Headless' | 'QR_Google';
26
+
27
+ export const QrCodePLVContainer = ({ data }: { data: string }) => {
28
+ const [selectedChoice, setSelectedChoice] = useState<SelectChoice>(null);
29
+
30
+ const qrCodeRef = useRef<HTMLDivElement>(null);
31
+ const PLVrefA4 = useRef<PLVComponent>(null);
32
+ const PLVrefA3 = useRef<PLVComponent>(null);
33
+
34
+ const theme = useTheme();
35
+
36
+ const qrCodeOptions: Options = useMemo(
37
+ () => ({
38
+ width: 300,
39
+ height: 300,
40
+ type: 'canvas',
41
+ data: data,
42
+ image: QRCodeImage,
43
+ margin: 10,
44
+ qrOptions: {
45
+ typeNumber: 0,
46
+ mode: 'Byte',
47
+ errorCorrectionLevel: 'Q',
48
+ },
49
+ imageOptions: {
50
+ hideBackgroundDots: true,
51
+ imageSize: 0.4,
52
+ margin: 15,
53
+ },
54
+ dotsOptions: {
55
+ color: darken(theme.palette.primary.main, 0.4),
56
+ type: 'square',
57
+ },
58
+ backgroundOptions: {
59
+ color: 'white',
60
+ },
61
+ cornersSquareOptions: {
62
+ color: darken(theme.palette.secondary.main, 0.2),
63
+ type: 'square',
64
+ },
65
+ cornersDotOptions: {
66
+ color: darken(theme.palette.secondary.main, 0.2),
67
+ type: 'square',
68
+ },
69
+ }),
70
+ [data, theme.palette],
71
+ );
72
+
73
+ const qrCode = useMemo(() => new QRCodeStyling(qrCodeOptions), [qrCodeOptions]);
74
+
75
+ useEffect(() => {
76
+ if (qrCodeRef.current) {
77
+ qrCode.append(qrCodeRef.current);
78
+ }
79
+ }, [qrCode]);
80
+
81
+ const printA4PLV = useReactToPrint({
82
+ content: () => PLVrefA4.current,
83
+ documentTitle: 'Movalib_PLV_A4',
84
+ });
85
+
86
+ const printA3PLV = useReactToPrint({
87
+ content: () => PLVrefA3.current,
88
+ documentTitle: 'Movalib_PLV_A3',
89
+ });
90
+
91
+ const downloadQRCodeHeadless = useCallback(() => {
92
+ qrCode.download({
93
+ extension: 'png',
94
+ name: 'movalib-qr-code',
95
+ });
96
+ }, [qrCode.download]);
97
+
98
+ const downloadQrCodeWithCTA = useCallback(() => {
99
+ // Check if we have everything we need
100
+ const qrCodeContainer = document.querySelector<HTMLElement>('#qr-code-container');
101
+ const qrCodeDiv = qrCodeContainer?.querySelector<HTMLElement>('.qr-code');
102
+ const qrCodeCanva = qrCodeDiv?.querySelector('canvas');
103
+ const downloadedDiv = document.getElementById('qr-code-downloaded-container');
104
+
105
+ // If we don't have the QR Code, we can't download it
106
+ if (!(qrCodeContainer && qrCodeDiv && qrCodeCanva && downloadedDiv)) {
107
+ return;
108
+ }
109
+
110
+ const tmpRendered = downloadedDiv.cloneNode(true) as HTMLElement;
111
+ tmpRendered.style.display = 'flex';
112
+
113
+ const downloadedQrCodeImgContainer = tmpRendered.querySelector<HTMLElement>('#qr-code-img-container')!;
114
+
115
+ downloadedQrCodeImgContainer.append(qrCodeCanva);
116
+ // add it to the document to be able to download it
117
+ document.body.append(tmpRendered);
118
+
119
+ // create the canva of the tmpRendered div and download it
120
+ html2canvas(tmpRendered).then((canvas) => {
121
+ const a = document.createElement('a');
122
+ a.download = 'movalib-qr-code.png';
123
+ a.href = canvas.toDataURL('image/png');
124
+ a.click();
125
+ });
126
+
127
+ // remove the tmpRendered div
128
+ tmpRendered.remove();
129
+
130
+ // re-append the QR Code to the initial container
131
+ qrCode.append(qrCodeRef.current!);
132
+ }, [qrCode.append]);
133
+
134
+ const onChangeSelectedChoice: SelectProps<SelectChoice>['onChange'] = (e) => {
135
+ setSelectedChoice(e.target.value as SelectChoice);
136
+ };
137
+
138
+ const onClickDownload = useMemo(() => {
139
+ switch (selectedChoice) {
140
+ case 'A3':
141
+ return printA3PLV;
142
+ case 'A4':
143
+ return printA4PLV;
144
+ case 'QR_Headless':
145
+ return downloadQRCodeHeadless;
146
+ case 'QR_Google':
147
+ return downloadQrCodeWithCTA;
148
+ default:
149
+ return () => {};
150
+ }
151
+ }, [selectedChoice, printA3PLV, printA4PLV, downloadQRCodeHeadless, downloadQrCodeWithCTA]);
152
+ return (
153
+ <Box display='flex' flexDirection='column' alignItems='center' gap='24px'>
154
+ <PLVComponent ref={PLVrefA4} url={data} printSize={PrintSize.A4} />
155
+ <PLVComponent ref={PLVrefA3} url={data} printSize={PrintSize.A3} />
156
+
157
+ <Grid container alignItems='center'>
158
+ <Grid item xs={7}>
159
+ <FormControl fullWidth size='small'>
160
+ <InputLabel id='qrcode-plv-select'>Choisissez votre support</InputLabel>
161
+ <Select
162
+ labelId='qrcode-plv-select'
163
+ label='Choisissez votre support'
164
+ id='qrcode-plv-select'
165
+ onChange={onChangeSelectedChoice}
166
+ value={selectedChoice}
167
+ >
168
+ <MenuItem value={'A3'}>🧾&nbsp;&nbsp;PLV format A4</MenuItem>
169
+ <MenuItem value={'A4'}>🧾&nbsp;&nbsp;PLV format A3</MenuItem>
170
+ <MenuItem value={'QR_Google'}>QR Code format GoogleMyBusiness</MenuItem>
171
+ <MenuItem value={'QR_Headless'}>QR Code seul</MenuItem>
172
+ </Select>
173
+ </FormControl>
174
+ </Grid>
175
+ <Grid item xs={1} />
176
+ <Grid item xs={4}>
177
+ <Button
178
+ startIcon={selectedChoice === 'A3' || selectedChoice === 'A4' ? <DocumentScanner /> : <QrCode2 />}
179
+ onClick={onClickDownload}
180
+ variant='outlined'
181
+ disabled={!selectedChoice}
182
+ sx={{ color: darken(theme.palette.primary.main, 0.2), borderRadius: '10rem' }}
183
+ >
184
+ {selectedChoice === 'A3' || selectedChoice === 'A4' ? 'Imprimer' : 'Télécharger'}
185
+ </Button>
186
+ </Grid>
187
+ </Grid>
188
+ <div id='qr-code-container' style={flexCenter}>
189
+ <div
190
+ ref={qrCodeRef}
191
+ style={{
192
+ height: '300px',
193
+ width: '300px',
194
+ maxWidth: '300px !important',
195
+ }}
196
+ className='qr-code'
197
+ />
198
+ </div>
199
+ <DownloadedQRCode />
200
+ </Box>
201
+ );
202
+ };
package/src/QRCode.css DELETED
@@ -1,8 +0,0 @@
1
- .qr-code canvas {
2
- /*max-width: 300px !important;*/
3
- /* margin: 20px 0; */
4
- display: flex;
5
- align-items: center;
6
- justify-content: center;
7
- width: 100%;
8
- }