From 212ad32783125d09d043c80b38e0342adda07f82 Mon Sep 17 00:00:00 2001 From: Biswakalyan Bhuyan Date: Wed, 16 Oct 2024 16:29:16 +0530 Subject: Add BarcodeScanner component for scanning barcodes and displaying confirmation popup --- src/components/BarcodeScanner.js | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'src') 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 Requesting camera permission...; + if (hasPermission === false) return No access to camera; + + return ( + + + {scanned &&