Created
May 26, 2020 01:51
-
-
Save gcmatheusj/5eda17a2974894bca1846e1e37ad2577 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {useState, useRef} from 'react'; | |
import { | |
View, | |
WebView, | |
Text, | |
Linking, | |
Dimensions, | |
StyleSheet, | |
} from 'react-native'; | |
import QRCodeScanner from 'react-native-qrcode-scanner'; | |
import ModalWebView from './components/ModalWebView'; | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
flexDirection: 'column', | |
backgroundColor: 'black', | |
}, | |
touchable: { | |
padding: 16, | |
}, | |
text: { | |
fontSize: 21, | |
color: 'rgb(0,122,255)', | |
}, | |
cameraContainer: { | |
height: Dimensions.get('window').height, | |
}, | |
}); | |
const QRCodeScreen = () => { | |
const [modalVisible, setModalVisible] = useState(false); | |
const [success, setSuccess] = useState(null); | |
const [url, setUrl] = useState(''); | |
const scanner = useRef(null); | |
const openLink = () => { | |
Linking.openURL(url).catch((err) => alert('An error occured', err)); | |
}; | |
const handleButton = () => { | |
setModalVisible(!modalVisible); | |
if (scanner.current) { | |
scanner.reactivate(); | |
} | |
}; | |
const onSuccess = async (e) => { | |
setSuccess(true); | |
setModalVisible(true); | |
setUrl(e.data); | |
}; | |
return ( | |
<View style={styles.container}> | |
<QRCodeScanner | |
onRead={onSuccess} | |
showMarker={true} | |
checkAndroid6Permissions={true} | |
ref={scanner} | |
cameraStyle={styles.cameraContainer} | |
bottomContent={ | |
<View style={styles.touchable}> | |
{success && <Text style={styles.text}>OK. Got it!</Text>} | |
</View> | |
} | |
/> | |
<ModalWebView | |
handleButton={handleButton} | |
modalVisible={modalVisible} | |
url={url} | |
openLink={openLink} | |
/> | |
</View> | |
); | |
}; | |
export default QRCodeScreen; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment