@ -1,99 +1,139 @@
import React , { Component } from 'react'
import React , { Component } from 'react'
import PropTypes from 'prop-types'
import PropTypes from 'prop-types'
import log from 'loglevel'
import { BrowserQRCodeReader } from '@zxing/library'
import { BrowserQRCodeReader } from '@zxing/library'
import { getEnvironmentType } from '../../../../../../app/scripts/lib/util'
import { ENVIRONMENT _TYPE _FULLSCREEN } from '../../../../../../app/scripts/lib/enums'
import Spinner from '../../../ui/spinner'
import Spinner from '../../../ui/spinner'
import WebcamUtils from '../../../../../lib/webcam-utils'
import WebcamUtils from '../../../../../lib/webcam-utils'
import PageContainerFooter from '../../../ui/page-container/page-container-footer/page-container-footer.component'
import PageContainerFooter from '../../../ui/page-container/page-container-footer/page-container-footer.component'
const READY _STATE = {
ACCESSING _CAMERA : 'ACCESSING_CAMERA' ,
NEED _TO _ALLOW _ACCESS : 'NEED_TO_ALLOW_ACCESS' ,
READY : 'READY' ,
}
export default class QrScanner extends Component {
export default class QrScanner extends Component {
static propTypes = {
static propTypes = {
hideModal : PropTypes . func . isRequired ,
hideModal : PropTypes . func . isRequired ,
qrCodeDetected : PropTypes . func ,
qrCodeDetected : PropTypes . func . isRequired ,
scanQrCode : PropTypes . func ,
error : PropTypes . bool ,
errorType : PropTypes . string ,
}
}
static contextTypes = {
static contextTypes = {
t : PropTypes . func ,
t : PropTypes . func ,
}
}
constructor ( props , context ) {
constructor ( props ) {
super ( props )
super ( props )
this . state = {
this . state = this . getInitialState ( )
ready : false ,
msg : context . t ( 'accessingYourCamera' ) ,
}
this . codeReader = null
this . codeReader = null
this . permissionChecker = null
this . permissionChecker = null
this . needsToReinit = false
this . mounted = false
// Clear pre-existing qr code data before scanning
// Clear pre-existing qr code data before scanning
this . props . qrCodeDetected ( null )
this . props . qrCodeDetected ( null )
}
}
componentDidMount ( ) {
componentDidMount ( ) {
this . mounted = true
this . checkEnvironment ( )
}
componentDidUpdate ( _ , prevState ) {
const { ready } = this . state
if ( prevState . ready !== ready ) {
if ( ready === READY _STATE . READY ) {
this . initCamera ( )
} else if ( ready === READY _STATE . NEED _TO _ALLOW _ACCESS ) {
this . checkPermissions ( )
}
}
}
getInitialState ( ) {
return {
ready : READY _STATE . ACCESSING _CAMERA ,
error : null ,
}
}
checkEnvironment = async ( ) => {
try {
const { environmentReady } = await WebcamUtils . checkStatus ( )
if ( ! environmentReady && getEnvironmentType ( ) !== ENVIRONMENT _TYPE _FULLSCREEN ) {
const currentUrl = new URL ( window . location . href )
const currentHash = currentUrl . hash
const currentRoute = currentHash
? currentHash . substring ( 1 )
: null
global . platform . openExtensionInBrowser ( currentRoute )
}
} catch ( error ) {
if ( this . mounted ) {
this . setState ( { error } )
}
}
// initial attempt is required to trigger permission prompt
this . initCamera ( )
this . initCamera ( )
}
}
async checkPermisisions ( ) {
checkPermissions = async ( ) => {
const { permissions } = await WebcamUtils . checkStatus ( )
try {
if ( permissions ) {
const { permissions } = await WebcamUtils . checkStatus ( )
clearTimeout ( this . permissionChecker )
if ( permissions ) {
// Let the video stream load first...
// Let the video stream load first...
setTimeout ( _ => {
await new Promise ( resolve => setTimeout ( resolve , 2000 ) )
this . setState ( {
if ( ! this . mounted ) {
ready : true ,
return
msg : this . context . t ( 'scanInstructions' ) ,
} )
if ( this . needsToReinit ) {
this . initCamera ( )
this . needsToReinit = false
}
}
} , 2000 )
this . setState ( { ready : READY _STATE . READY } )
} else {
} else if ( this . mounted ) {
// Keep checking for permissions
// Keep checking for permissions
this . permissionChecker = setTimeout ( _ => {
this . permissionChecker = setTimeout ( this . checkPermissions , 1000 )
this . checkPermisisions ( )
}
} , 1000 )
} catch ( error ) {
if ( this . mounted ) {
this . setState ( { error } )
}
}
}
}
}
componentWillUnmount ( ) {
componentWillUnmount ( ) {
this . mounted = false
clearTimeout ( this . permissionChecker )
clearTimeout ( this . permissionChecker )
if ( this . codeReader ) {
if ( this . codeReader ) {
this . codeReader . reset ( )
this . codeReader . reset ( )
}
}
}
}
initCamera ( ) {
initCamera = async ( ) => {
this . codeReader = new BrowserQRCodeReader ( )
this . codeReader = new BrowserQRCodeReader ( )
this . codeReader . getVideoInputDevices ( )
try {
. then ( ( ) => {
await this . codeReader . getVideoInputDevices ( )
clearTimeout ( this . permissionChecker )
const content = await this . codeReader . decodeFromInputVideoDevice ( undefined , 'video' )
this . checkPermisisions ( )
const result = this . parseContent ( content . text )
this . codeReader . decodeFromInputVideoDevice ( undefined , 'video' )
if ( ! this . mounted ) {
. then ( content => {
return
const result = this . parseContent ( content . text )
} else if ( result . type !== 'unknown' ) {
if ( result . type !== 'unknown' ) {
this . props . qrCodeDetected ( result )
this . props . qrCodeDetected ( result )
this . stopAndClose ( )
this . stopAndClose ( )
} else {
} else {
this . setState ( { error : new Error ( this . context . t ( 'unknownQrCode' ) ) } )
this . setState ( { msg : this . context . t ( 'unknownQrCode' ) } )
}
}
} catch ( error ) {
} )
if ( ! this . mounted ) {
. catch ( err => {
return
if ( err && err . name === 'NotAllowedError' ) {
}
this . setState ( { msg : this . context . t ( 'youNeedToAllowCameraAccess' ) } )
if ( error . name === 'NotAllowedError' ) {
clearTimeout ( this . permissionChecker )
log . info ( ` Permission denied: ' ${ error } ' ` )
this . needsToReinit = true
this . setState ( { ready : READY _STATE . NEED _TO _ALLOW _ACCESS } )
this . checkPermisisions ( )
} else {
}
this . setState ( { error } )
} )
}
} ) . catch ( err => {
}
console . error ( '[QR-SCANNER]: getVideoInputDevices threw an exception: ' , err )
} )
}
}
parseContent ( content ) {
parseContent ( content ) {
@ -126,89 +166,108 @@ export default class QrScanner extends Component {
if ( this . codeReader ) {
if ( this . codeReader ) {
this . codeReader . reset ( )
this . codeReader . reset ( )
}
}
this . setState ( { ready : false } )
this . props . hideModal ( )
this . props . hideModal ( )
}
}
tryAgain = ( ) => {
tryAgain = ( ) => {
// close the modal
clearTimeout ( this . permissionChecker )
this . stopAndClose ( )
if ( this . codeReader ) {
// wait for the animation and try again
this . codeReader . reset ( )
setTimeout ( _ => {
}
this . props . scanQrCode ( )
this . setState ( this . getInitialState ( ) , ( ) => {
} , 1000 )
this . checkEnvironment ( )
} )
}
}
renderVideo ( ) {
renderError ( ) {
return (
const { t } = this . context
< div className = "qr-scanner__content__video-wrapper" >
const { error } = this . state
< video
id = "video"
style = { {
display : this . state . ready ? 'block' : 'none' ,
} }
/ >
{ ! this . state . ready ? < Spinner color = "#F7C06C" / > : null }
< / d i v >
)
}
renderErrorModal ( ) {
let title , msg
let title , msg
if ( error . type === 'NO_WEBCAM_FOUND' ) {
if ( this . props . error ) {
title = t ( 'noWebcamFoundTitle' )
if ( this . props . errorType === 'NO_WEBCAM_FOUND' ) {
msg = t ( 'noWebcamFound' )
title = this . context . t ( 'noWebcamFoundTitle' )
} else if ( error . message === t ( 'unknownQrCode' ) ) {
msg = this . context . t ( 'noWebcamFound' )
msg = t ( 'unknownQrCode' )
} else {
} else {
title = this . context . t ( 'unknownCameraErrorTitle' )
title = t ( 'unknownCameraErrorTitle' )
msg = this . context . t ( 'unknownCameraError' )
msg = t ( 'unknownCameraError' )
}
}
}
return (
return (
< div className = "qr-scanner" >
< >
< div className = "qr-scanner__close" onClick = { this . stopAndClose } > < / d i v >
< div className = "qr-scanner__image" >
< div className = "qr-scanner__image" >
< img src = "images/webcam.svg" width = { 70 } height = { 70 } / >
< img src = "images/webcam.svg" width = { 70 } height = { 70 } / >
< / d i v >
< / d i v >
< div className = "qr-scanner__title" >
{
{ title }
title
< / d i v >
? (
< div className = "qr-scanner__title" >
{ title }
< / d i v >
)
: null
}
< div className = "qr-scanner__error" >
< div className = "qr-scanner__error" >
{ msg }
{ msg }
< / d i v >
< / d i v >
< PageContainerFooter
< PageContainerFooter
onCancel = { this . stopAndClose }
onCancel = { this . stopAndClose }
onSubmit = { this . tryAgain }
onSubmit = { this . tryAgain }
cancelText = { this . context . t ( 'cancel' ) }
cancelText = { t ( 'cancel' ) }
submitText = { this . context . t ( 'tryAgain' ) }
submitText = { t ( 'tryAgain' ) }
submitButtonType = "confirm"
submitButtonType = "confirm"
/ >
/ >
< / d i v >
< / >
)
)
}
}
render ( ) {
renderVideo ( ) {
const { t } = this . context
const { t } = this . context
const { ready } = this . state
if ( this . props . error ) {
let message
return this . renderErrorModal ( )
if ( ready === READY _STATE . ACCESSING _CAMERA ) {
message = t ( 'accessingYourCamera' )
} else if ( ready === READY _STATE . READY ) {
message = t ( 'scanInstructions' )
} else if ( ready === READY _STATE . NEED _TO _ALLOW _ACCESS ) {
message = t ( 'youNeedToAllowCameraAccess' )
}
}
return (
return (
< div className = "qr-scanner" >
< >
< div className = "qr-scanner__close" onClick = { this . stopAndClose } > < / d i v >
< div className = "qr-scanner__title" >
< div className = "qr-scanner__title" >
{ ` ${ t ( 'scanQrCode' ) } ` }
{ ` ${ t ( 'scanQrCode' ) } ` }
< / d i v >
< / d i v >
< div className = "qr-scanner__content" >
< div className = "qr-scanner__content" >
{ this . renderVideo ( ) }
< div className = "qr-scanner__content__video-wrapper" >
< video
id = "video"
style = { {
display : ready === READY _STATE . READY ? 'block' : 'none' ,
} }
/ >
{ ready !== READY _STATE . READY ? < Spinner color = "#F7C06C" / > : null }
< / d i v >
< / d i v >
< / d i v >
< div className = "qr-scanner__status" >
< div className = "qr-scanner__status" >
{ this . state . msg }
{ message }
< / d i v >
< / d i v >
< / >
)
}
render ( ) {
const { error } = this . state
return (
< div className = "qr-scanner" >
< div className = "qr-scanner__close" onClick = { this . stopAndClose } > < / d i v >
{
error
? this . renderError ( )
: this . renderVideo ( )
}
< / d i v >
< / d i v >
)
)
}
}