|
| 1 | +import React, { Component } from 'react' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | +import { BrowserQRCodeReader } from '@zxing/library' |
| 4 | +import adapter from 'webrtc-adapter' // eslint-disable-line import/no-nodejs-modules, no-unused-vars |
| 5 | +import Spinner from '../../spinner' |
| 6 | + |
| 7 | +export default class QrScanner extends Component { |
| 8 | + static propTypes = { |
| 9 | + hideModal: PropTypes.func.isRequired, |
| 10 | + qrCodeDetected: PropTypes.func, |
| 11 | + } |
| 12 | + |
| 13 | + static contextTypes = { |
| 14 | + t: PropTypes.func, |
| 15 | + } |
| 16 | + |
| 17 | + constructor (props, context) { |
| 18 | + super(props) |
| 19 | + this.state = { |
| 20 | + ready: false, |
| 21 | + msg: context.t('accessingYourCamera'), |
| 22 | + } |
| 23 | + this.scanning = false |
| 24 | + this.codeReader = null |
| 25 | + } |
| 26 | + |
| 27 | + componentDidMount () { |
| 28 | + console.log('[QR-SCANNER]: componentDidUpdate', this.scanning) |
| 29 | + if (!this.scanning) { |
| 30 | + this.scanning = true |
| 31 | + console.log('[QR-SCANNER]: componentDidUpdate - about to call initCamera') |
| 32 | + this.initCamera() |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + initCamera () { |
| 37 | + console.log('[QR-SCANNER]: initCamera') |
| 38 | + this.codeReader = new BrowserQRCodeReader() |
| 39 | + this.codeReader.getVideoInputDevices() |
| 40 | + .then(videoInputDevices => { |
| 41 | + console.log('[QR-SCANNER]: initCamera::getVideoInputDevices', videoInputDevices) |
| 42 | + setTimeout(_ => { |
| 43 | + this.setState({ |
| 44 | + ready: true, |
| 45 | + msg: this.context.t('scanInstructions')}) |
| 46 | + console.log('[QR-SCANNER]: initCamera::ready') |
| 47 | + }, 2000) |
| 48 | + |
| 49 | + console.log('[QR-SCANNER]: initCamera::started decoding...') |
| 50 | + this.codeReader.decodeFromInputVideoDevice(videoInputDevices[0].deviceId, 'video') |
| 51 | + .then(content => { |
| 52 | + console.log('[QR-SCANNER]: initCamera::decodeFromInputVideoDevice callback', content) |
| 53 | + this.codeReader.reset() |
| 54 | + const result = this.parseContent(content.text) |
| 55 | + if (result.type !== 'unknown') { |
| 56 | + this.props.qrCodeDetected(result) |
| 57 | + this.stopAndClose() |
| 58 | + } else { |
| 59 | + this.setState({msg: this.context.t('unknownQrCode')}) |
| 60 | + } |
| 61 | + }) |
| 62 | + .catch(err => { |
| 63 | + console.error('QR-SCANNER: decodeFromInputVideoDevice threw an exception: ', err) |
| 64 | + }) |
| 65 | + }).catch(err => { |
| 66 | + console.error('QR-SCANNER: getVideoInputDevices threw an exception: ', err) |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + parseContent (content) { |
| 71 | + let type = 'unknown' |
| 72 | + let values = {} |
| 73 | + |
| 74 | + // Here we could add more cases |
| 75 | + // To parse other codes (transactions for ex.) |
| 76 | + |
| 77 | + if (content.split('ethereum:').length > 1) { |
| 78 | + type = 'address' |
| 79 | + values = {'address': content.split('ethereum:')[1] } |
| 80 | + } |
| 81 | + return {type, values} |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + stopAndClose = () => { |
| 86 | + this.codeReader.reset() |
| 87 | + this.scanning = false |
| 88 | + this.setState({ ready: false }) |
| 89 | + this.props.hideModal() |
| 90 | + } |
| 91 | + |
| 92 | + render () { |
| 93 | + const { t } = this.context |
| 94 | + |
| 95 | + return ( |
| 96 | + <div className="modal-container"> |
| 97 | + <div className="modal-container__content"> |
| 98 | + <div className="modal-container__title"> |
| 99 | + { `${t('scanQrCode')}?` } |
| 100 | + </div> |
| 101 | + <div className="modal-container__description"> |
| 102 | + <div |
| 103 | + className={'qr-code-video-wrapper'} |
| 104 | + style={{ |
| 105 | + overflow: 'hidden', |
| 106 | + width: '100%', |
| 107 | + height: '275px', |
| 108 | + display: 'flex', |
| 109 | + alignItems: 'center', |
| 110 | + justifyContent: 'center', |
| 111 | + } |
| 112 | + }> |
| 113 | + <video |
| 114 | + id="video" |
| 115 | + style={{ |
| 116 | + width: 'auto', |
| 117 | + height: '275px', |
| 118 | + marginLeft: '-15%', |
| 119 | + display: this.state.ready ? 'block' : 'none', |
| 120 | + transform: 'scaleX(-1)', |
| 121 | + }} |
| 122 | + /> |
| 123 | + { !this.state.ready ? <Spinner color={'#F7C06C'} /> : null} |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + <div className="modal-container__footer"> |
| 128 | + {this.state.msg} |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + ) |
| 132 | + } |
| 133 | +} |
0 commit comments