47 lines
1.4 KiB
JavaScript
47 lines
1.4 KiB
JavaScript
import React, { useState, useEffect } from 'react';
|
|
import { Text, View, StyleSheet, Button } from 'react-native';
|
|
import { BarCodeScanner } from 'expo-barcode-scanner';
|
|
|
|
export default function App() {
|
|
const [hasPermission, setHasPermission] = useState(null);
|
|
const [scanned, setScanned] = useState(false);
|
|
|
|
useEffect(() => {
|
|
(async () => {
|
|
const { status } = await BarCodeScanner.requestPermissionsAsync();
|
|
setHasPermission(status === 'granted');
|
|
})();
|
|
}, []);
|
|
|
|
const handleBarCodeScanned = ({ type, data }) => {
|
|
setScanned(true);
|
|
const timeInt = new Date().getTime();
|
|
const locationStr = '4321';
|
|
const newData = `${data}-${timeInt}-${locationStr}`;
|
|
alert(`Bar code with type ${type} and data ${data} has been scanned! Data to print on new bar code: ${newData}`);
|
|
};
|
|
|
|
if (hasPermission === null) {
|
|
return <Text>Requesting for camera permission</Text>;
|
|
}
|
|
if (hasPermission === false) {
|
|
return <Text>No access to camera</Text>;
|
|
}
|
|
|
|
return (
|
|
<View
|
|
style={{
|
|
flex: 1,
|
|
flexDirection: 'column',
|
|
justifyContent: 'flex-end',
|
|
}}>
|
|
<BarCodeScanner
|
|
onBarCodeScanned={scanned ? undefined : handleBarCodeScanned}
|
|
style={StyleSheet.absoluteFillObject}
|
|
/>
|
|
|
|
{scanned && <Button title={'Tap to Scan Again'} onPress={() => setScanned(false)} />}
|
|
</View>
|
|
);
|
|
}
|