aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatarLibravatar Biswakalyan Bhuyan <biswa@surgot.in> 2024-10-16 16:29:16 +0530
committerLibravatarLibravatar Biswakalyan Bhuyan <biswa@surgot.in> 2024-10-16 16:29:16 +0530
commit212ad32783125d09d043c80b38e0342adda07f82 (patch)
treed8deaf9b750b9276663c42526e0652eb78be89e8
parent39025c5fb017781e9f25d52263bc7ffd1effbe51 (diff)
downloadmall-app-212ad32783125d09d043c80b38e0342adda07f82.tar.gz
mall-app-212ad32783125d09d043c80b38e0342adda07f82.tar.bz2
mall-app-212ad32783125d09d043c80b38e0342adda07f82.zip
Add BarcodeScanner component for scanning barcodes and displaying confirmation popup
-rw-r--r--src/components/BarcodeScanner.js50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/components/BarcodeScanner.js b/src/components/BarcodeScanner.js
index e69de29..73ae19a 100644
--- a/src/components/BarcodeScanner.js
+++ b/src/components/BarcodeScanner.js
@@ -0,0 +1,50 @@
+import React, { useState, useEffect } from 'react';
+import { View, Text, Button, Alert } from 'react-native';
+import { BarCodeScanner } from 'expo-barcode-scanner';
+
+const BarcodeScanner = ({ onScan }) => {
+ 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);
+ // Simulated product fetch (replace this with an actual service call)
+ const product = {
+ name: 'Sample Product',
+ price: 29.99,
+ description: 'This is a sample product description.',
+ barcode: data
+ };
+ onScan(product);
+ Alert.alert(
+ "Product Scanned",
+ `Do you wish to add ${product.name} to the cart?`,
+ [
+ { text: "Cancel", onPress: () => setScanned(false) },
+ { text: "Add to Cart", onPress: () => onScan(product) },
+ ]
+ );
+ };
+
+ if (hasPermission === null) return <Text>Requesting camera permission...</Text>;
+ if (hasPermission === false) return <Text>No access to camera</Text>;
+
+ return (
+ <View style={{ flex: 1 }}>
+ <BarCodeScanner
+ onBarCodeScanned={scanned ? undefined : handleBarCodeScanned}
+ style={{ flex: 1 }}
+ />
+ {scanned && <Button title="Scan Again" onPress={() => setScanned(false)} />}
+ </View>
+ );
+};
+
+export default BarcodeScanner;