import React, { Component } from 'react' import PropTypes from 'prop-types' import {connect} from 'react-redux' import adapter from 'webrtc-adapter' // eslint-disable-line import/no-nodejs-modules, no-unused-vars import { BrowserQRCodeReader } from '@zxing/library' import { hideQrScanner, qrCodeDetected} from '../../actions' import Spinner from '../spinner' class QrScanner extends Component { static propTypes = { visible: PropTypes.bool, hideQrScanner: PropTypes.func, qrCodeDetected: PropTypes.func, } constructor (props) { super(props) this.state = { ready: false, msg: 'Accesing your camera...', } this.scanning = false this.codeReader = null } componentDidUpdate () { if (this.props.visible && this.camera && !this.scanning) { this.scanning = true this.initCamera() } } initCamera () { console.log('QR-SCANNER: initCamera ') this.codeReader = new BrowserQRCodeReader() this.codeReader.getVideoInputDevices() .then(videoInputDevices => { console.log('QR-SCANNER: getVideoInputDevices ', videoInputDevices) setTimeout(_ => { this.setState({ ready: true, msg: 'Place the QR code in front of your camera so we can read it...'}) console.log('QR-SCANNER: this.state.ready = true') }, 2000) console.log('QR-SCANNER: started scanning...') this.codeReader.decodeFromInputVideoDevice(videoInputDevices[0].deviceId, 'video') .then(content => { console.log('QR-SCANNER: content found!', content) this.codeReader.reset() console.log('QR-SCANNER: stopped scanning...') const result = this.parseContent(content.text) if (result.type !== 'unknown') { console.log('QR-SCANNER: CODE DETECTED', result) this.props.qrCodeDetected(result) this.props.hideQrScanner() this.setState({ ready: false }) } else { this.setState({msg: 'Error: We couldn\'t identify that QR code'}) console.log('QR-SCANNER: Unknown code') } }) .catch(err => { console.log('QR-SCANNER: decodeFromInputVideoDevice threw an exception: ', err) }) }).catch(err => { console.log('QR-SCANNER: getVideoInputDevices threw an exception: ', err) }) } parseContent (content) { let type = 'unknown' let values = {} // Here we could add more cases // To parse other codes (transactions for ex.) if (content.split('ethereum:').length > 1) { type = 'address' values = {'address': content.split('ethereum:')[1] } } return {type, values} } stopAndClose = () => { console.log('QR-SCANNER: stopping scanner...') this.codeReader.reset() this.scanning = false this.props.hideQrScanner() this.setState({ ready: false }) } render () { const { visible } = this.props if (!visible) { return null } return (

Scan QR code

{this.state.msg}
) } } function mapDispatchToProps (dispatch) { return { hideQrScanner: () => dispatch(hideQrScanner()), qrCodeDetected: (data) => dispatch(qrCodeDetected(data)), } } function mapStateToProps (state) { return {} } export default connect(mapStateToProps, mapDispatchToProps)(QrScanner)