Skip to content

Commit 4339f04

Browse files
committed
use existing modals
1 parent b6592ba commit 4339f04

File tree

12 files changed

+177
-235
lines changed

12 files changed

+177
-235
lines changed

app/_locales/en/messages.json

+12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"accept": {
33
"message": "Accept"
44
},
5+
"accessingYourCamera": {
6+
"message": "Accesing your camera..."
7+
},
58
"account": {
69
"message": "Account"
710
},
@@ -937,6 +940,12 @@
937940
"info": {
938941
"message": "Info"
939942
},
943+
"scanInstructions": {
944+
"message": "Place the QR code in front of your camera so we can read it..."
945+
},
946+
"scanQrCode": {
947+
"message": "Scan QR Code"
948+
},
940949
"shapeshiftBuy": {
941950
"message": "Buy with Shapeshift"
942951
},
@@ -1086,6 +1095,9 @@
10861095
"unknownNetworkId": {
10871096
"message": "Unknown network ID"
10881097
},
1098+
"unknownQrCode": {
1099+
"message": "Error: We couldn't identify that QR code"
1100+
},
10891101
"unlock": {
10901102
"message": "Unlock"
10911103
},

app/scripts/contentscript.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,6 @@ function blacklistedDomainCheck () {
198198
*/
199199
function redirectToPhishingWarning () {
200200
console.log('MetaMask - routing to Phishing Warning component')
201-
let extensionURL = extension.runtime.getURL('phishing.html')
201+
const extensionURL = extension.runtime.getURL('phishing.html')
202202
window.location.href = extensionURL
203203
}

ui/app/actions.js

-24
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ var actions = {
3333
ALERT_CLOSE: 'UI_ALERT_CLOSE',
3434
showAlert: showAlert,
3535
hideAlert: hideAlert,
36-
QR_SCANNER_OPEN: 'UI_QR_SCANNER_OPEN',
37-
QR_SCANNER_CLOSE: 'UI_QR_SCANNER_CLOSE',
3836
QR_CODE_DETECTED: 'UI_QR_CODE_DETECTED',
39-
showQrScanner,
40-
hideQrScanner,
4137
qrCodeDetected,
4238
// network dropdown open
4339
NETWORK_DROPDOWN_OPEN: 'UI_NETWORK_DROPDOWN_OPEN',
@@ -310,7 +306,6 @@ var actions = {
310306
CLEAR_PENDING_TOKENS: 'CLEAR_PENDING_TOKENS',
311307
setPendingTokens,
312308
clearPendingTokens,
313-
scanQrCode,
314309
}
315310

316311
module.exports = actions
@@ -1813,26 +1808,13 @@ function hideAlert () {
18131808
}
18141809
}
18151810

1816-
function showQrScanner () {
1817-
return {
1818-
type: actions.QR_SCANNER_OPEN,
1819-
}
1820-
}
1821-
18221811
function qrCodeDetected (qrCodeData) {
18231812
return {
18241813
type: actions.QR_CODE_DETECTED,
18251814
value: qrCodeData,
18261815
}
18271816
}
18281817

1829-
function hideQrScanner () {
1830-
return {
1831-
type: actions.QR_SCANNER_CLOSE,
1832-
}
1833-
}
1834-
1835-
18361818
function showLoadingIndication (message) {
18371819
return {
18381820
type: actions.SHOW_LOADING,
@@ -2275,9 +2257,3 @@ function clearPendingTokens () {
22752257
type: actions.CLEAR_PENDING_TOKENS,
22762258
}
22772259
}
2278-
2279-
function scanQrCode () {
2280-
return (dispatch, getState) => {
2281-
dispatch(actions.showQrScanner())
2282-
}
2283-
}

ui/app/app.js

-9
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ const AccountMenu = require('./components/account-menu')
3838
const Modal = require('./components/modals/index').Modal
3939
// Global Alert
4040
const Alert = require('./components/alert')
41-
42-
const QrScanner = require('./components/qr-scanner')
43-
4441
const AppHeader = require('./components/app-header')
4542

4643
import UnlockPage from './components/pages/unlock-page'
@@ -134,8 +131,6 @@ class App extends Component {
134131
// global alert
135132
h(Alert, {visible: this.props.alertOpen, msg: alertMessage}),
136133

137-
h(QrScanner, {visible: this.props.qrScannerOpen}),
138-
139134
h(AppHeader),
140135

141136
// sidebar
@@ -274,7 +269,6 @@ App.propTypes = {
274269
currentView: PropTypes.object,
275270
sidebarOpen: PropTypes.bool,
276271
alertOpen: PropTypes.bool,
277-
qrScannerOpen: PropTypes.bool,
278272
hideSidebar: PropTypes.func,
279273
isMascara: PropTypes.bool,
280274
isOnboarding: PropTypes.bool,
@@ -311,7 +305,6 @@ function mapStateToProps (state) {
311305
networkDropdownOpen,
312306
sidebarOpen,
313307
alertOpen,
314-
qrScannerOpen,
315308
alertMessage,
316309
isLoading,
317310
loadingMessage,
@@ -339,7 +332,6 @@ function mapStateToProps (state) {
339332
networkDropdownOpen,
340333
sidebarOpen,
341334
alertOpen,
342-
qrScannerOpen,
343335
alertMessage,
344336
isLoading,
345337
loadingMessage,
@@ -370,7 +362,6 @@ function mapStateToProps (state) {
370362
isMouseUser: state.appState.isMouseUser,
371363
betaUI: state.metamask.featureFlags.betaUI,
372364
isRevealingSeedWords: state.metamask.isRevealingSeedWords,
373-
Qr: state.appState.Qr,
374365
welcomeScreenSeen: state.metamask.welcomeScreenSeen,
375366

376367
// state needed to get account dropdown temporarily rendering from app bar

ui/app/components/modals/modal.js

+13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const CustomizeGasModal = require('../customize-gas-modal')
2121
const NotifcationModal = require('./notification-modal')
2222
const ConfirmResetAccount = require('./confirm-reset-account')
2323
const ConfirmRemoveAccount = require('./confirm-remove-account')
24+
const QRScanner = require('./qr-scanner')
2425
const TransactionConfirmed = require('./transaction-confirmed')
2526
const WelcomeBeta = require('./welcome-beta')
2627
const Notification = require('./notification')
@@ -346,6 +347,18 @@ const MODALS = {
346347
borderRadius: '8px',
347348
},
348349
},
350+
QR_SCANNER: {
351+
contents: h(QRScanner),
352+
mobileModalStyle: {
353+
...modalContainerMobileStyle,
354+
},
355+
laptopModalStyle: {
356+
...modalContainerLaptopStyle,
357+
},
358+
contentStyle: {
359+
borderRadius: '8px',
360+
},
361+
},
349362

350363
DEFAULT: {
351364
contents: [],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import QrScanner from './qr-scanner.container'
2+
module.exports = QrScanner
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { connect } from 'react-redux'
2+
import QrScanner from './qr-scanner.component'
3+
4+
const { hideModal, qrCodeDetected } = require('../../../actions')
5+
6+
const mapDispatchToProps = dispatch => {
7+
return {
8+
hideModal: () => dispatch(hideModal()),
9+
qrCodeDetected: (data) => dispatch(qrCodeDetected(data)),
10+
}
11+
}
12+
13+
export default connect(null, mapDispatchToProps)(QrScanner)

ui/app/components/qr-scanner/index.js

-2
This file was deleted.

0 commit comments

Comments
 (0)