@@ -34,22 +34,40 @@ import * as React from 'react';
3434import useAuth from '../../../../hooks/useAuth' ;
3535import { useNavigate } from 'react-router-dom' ;
3636import { getCustomFieldValuesForDetails } from '../../type' ;
37+ import { QRCodeSVG } from 'qrcode.react' ;
3738
3839interface PropsType {
3940 asset : AssetDTO ;
4041 loading : boolean ;
4142}
43+ const downloadQRCode = ( value : string ) => {
44+ const svgElement = document . getElementById ( `qr-code-${ value } ` ) ;
45+ if ( ! svgElement ) return ;
4246
43- const LabelWrapper = styled ( Box ) (
44- ( { theme } ) => `
45- font-size: ${ theme . typography . pxToRem ( 10 ) } ;
46- font-weight: bold;
47- text-transform: uppercase;
48- border-radius: ${ theme . general . borderRadiusSm } ;
49- padding: ${ theme . spacing ( 0.9 , 1.5 , 0.7 ) } ;
50- line-height: 1;
51- `
52- ) ;
47+ const svgData = new XMLSerializer ( ) . serializeToString ( svgElement ) ;
48+ const canvas = document . createElement ( 'canvas' ) ;
49+ const ctx = canvas . getContext ( '2d' ) ;
50+ const img = new Image ( ) ;
51+
52+ canvas . width = 120 ;
53+ canvas . height = 120 ;
54+
55+ const svgBlob = new Blob ( [ svgData ] , { type : 'image/svg+xml;charset=utf-8' } ) ;
56+ const url = URL . createObjectURL ( svgBlob ) ;
57+
58+ img . onload = ( ) => {
59+ ctx ?. drawImage ( img , 0 , 0 ) ;
60+ URL . revokeObjectURL ( url ) ;
61+
62+ const pngUrl = canvas . toDataURL ( 'image/png' ) ;
63+ const link = document . createElement ( 'a' ) ;
64+ link . href = pngUrl ;
65+ link . download = `qr-code-${ value } .png` ;
66+ link . click ( ) ;
67+ } ;
68+
69+ img . src = url ;
70+ } ;
5371const AssetDetails = ( { asset, loading } : PropsType ) => {
5472 const { t } : { t : any } = useTranslation ( ) ;
5573 const theme = useTheme ( ) ;
@@ -73,7 +91,7 @@ const AssetDetails = ({ asset, loading }: PropsType) => {
7391 : null
7492 } ,
7593 { label : t ( 'area' ) , value : asset ?. area } ,
76- { label : t ( 'barcode' ) , value : asset ?. barCode }
94+ { label : t ( 'barcode' ) , value : asset ?. barCode , barcode : true }
7795 ] ;
7896 const moreInfosFields = [
7997 {
@@ -95,18 +113,34 @@ const AssetDetails = ({ asset, loading }: PropsType) => {
95113 ] ;
96114 const BasicField = ( {
97115 label,
98- value
116+ value,
117+ barcode
99118 } : {
100119 label : string | number ;
101120 value : string | number ;
121+ barcode ?: boolean ;
102122 } ) => {
103123 return value ? (
104124 < Grid item xs = { 12 } >
105125 < Stack spacing = { 5 } direction = "row" >
106126 < Typography variant = "h6" fontWeight = "bold" >
107127 { label }
108128 </ Typography >
109- < Typography variant = "h6" > { value } </ Typography >
129+ < Box >
130+ < Typography variant = "h6" > { value } </ Typography >
131+ { barcode && value && (
132+ < QRCodeSVG
133+ id = { `qr-code-${ value } ` }
134+ value = { value . toString ( ) }
135+ size = { 120 }
136+ level = "H"
137+ onClick = { ( ) => {
138+ downloadQRCode ( value . toString ( ) ) ;
139+ } }
140+ style = { { marginTop : theme . spacing ( 1 ) , cursor : 'pointer' } }
141+ />
142+ ) }
143+ </ Box >
110144 </ Stack >
111145 < Divider sx = { { mt : 1 } } />
112146 </ Grid >
0 commit comments