aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/qr-scanner/qr-scanner.component.js
blob: 4cc296441ebd29b9ff1bfe4843129bd1ccd6cc0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import {connect} from 'react-redux'
import { hideQrScanner, qrCodeDetected} from '../../actions'
import Instascan from 'instascan'
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
  }

  componentDidUpdate () {
    if (this.props.visible && this.camera && !this.scanning) {
      this.scanner = new Instascan.Scanner({
        video: this.camera,
        backgroundScan: false,
        continuous: true,
      })
      this.scanner.addListener('scan', (content) => {
        this.scanner.stop().then(_ => {
          const result = this.parseContent(content)
          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'})
          }
          this.scanning = false
        })
      })
      Instascan.Camera.getCameras().then((cameras) => {
        if (cameras.length > 0) {
          this.scanner.start(cameras[0])
          this.setState({ ready: true })
          this.setState({ msg: 'Place the QR code in front of your camera so we can read it...'})
          console.log('QR-SCANNER: started scanning with camera', cameras[0])
        } else {
          this.setState({ msg: 'No camera found :('})
        }
      }).catch(function (e) {
        console.error(e)
      })
      this.scanning = true
    }
  }

  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 = () => {
    this.scanner.stop().then(_ => {
      this.scanning = false
      this.props.hideQrScanner()
      this.setState({ ready: false })
    })
  }

  render () {
    const { visible } = this.props

    if (!visible) {
     return null
    }

    return (
      <div className={'qr-code-modal-wrapper'}>
        <div className={'qr-scanner'}
          style={{
            position: 'fixed',
            top: '50%',
            left: '50%',
            zIndex: 1050,
            minWidth: '320px',
            minHeight: '400px',
            maxWidth: '300px',
            maxHeight: '300px',
            transform: 'translate(-50%, -50%)',
            backgroundColor: '#ffffff',
            padding: '15px',
          }}
        >
          <h3 style={{
            textAlign: 'center',
            marginBottom: '20px',
            fontSize: '1.5rem',
            fontWeight: '500',
          }}>
            Scan QR code
          </h3>
            <div
              className={'qr-code-video-wrapper'}
              style={{
                overflow: 'hidden',
                width: '100%',
                height: '275px',
                display: 'flex',
                alignItems: 'center',
                justifyContent: 'center',
              }}>
              <video
                style={{
                  width: 'auto',
                  height: '275px',
                  marginLeft: '-15%',
                  display: this.state.ready ? 'block' : 'none',
                }}
                ref={(cam) => {
                  this.camera = cam
                }}
              />
             { !this.state.ready ? <Spinner color={'#F7C06C'} /> : null}
            </div>
          <div className={'qr-code-help'} style={{textAlign: 'center', fontSize: '12px', padding: '15px'}}>
            {this.state.msg}
          </div>
        </div>
        <div
          className={'qr-code-modal-overlay'}
          style={{
            position: 'fixed',
            top: '0',
            right: '0',
            bottom: '0',
            left: '0',
            zIndex: '1040',
            backgroundColor: 'rgba(0, 0, 0, 0.5)',
            animationFillMode: 'forwards',
            animationDuration: '0.3s',
            animationName: 'anim_171532470906313',
            animationTimingFunction: 'ease-out',
          }}
          onClick={this.stopAndClose}
        />
      </div>
    )
  }
}

function mapDispatchToProps (dispatch) {
  return {
    hideQrScanner: () => dispatch(hideQrScanner()),
    qrCodeDetected: (data) => dispatch(qrCodeDetected(data)),
  }
}
function mapStateToProps (state) {
  return {}
}

export default connect(mapStateToProps, mapDispatchToProps)(QrScanner)