temp/src/Components/BarcodeScanner.jsx

206 lines
6.7 KiB
React
Raw Normal View History

2024-07-04 12:47:03 +05:30
import React, { useState, useEffect, useRef } from "react";
2024-07-03 23:13:35 +05:30
import { Html5QrcodeScanner } from "html5-qrcode";
2024-07-04 12:47:03 +05:30
import { Box, Button } from "@mui/material";
2024-07-03 23:13:35 +05:30
import LoadingContainer from "./LoadingContainer";
2024-07-03 18:42:11 +05:30
const BarcodeScanner = () => {
2024-07-03 23:13:35 +05:30
const [scanResult, setScanResult] = useState(null);
const [isLoading, setIsLoading] = useState(false);
const [barcodeInfo, setBarcodeInfo] = useState([]);
const [marksData, setMarksData] = useState([]);
2024-07-13 19:29:10 +05:30
const [partAData, setPartAData] = useState([]);
2024-07-04 12:47:03 +05:30
const scannerRef = useRef(null); // Use ref to store the scanner instance
2024-07-03 18:42:11 +05:30
2024-07-03 23:13:35 +05:30
useEffect(() => {
2024-07-04 12:47:03 +05:30
scannerRef.current = new Html5QrcodeScanner("reader", {
2024-07-03 23:13:35 +05:30
qrbox: {
width: 250,
height: 250,
},
fps: 5,
});
2024-07-04 13:10:42 +05:30
const fetchBarcodeData = () => {
2024-07-13 19:29:10 +05:30
if (!scanResult) {
return;
2024-07-06 13:08:43 +05:30
}
2024-07-03 23:13:35 +05:30
setIsLoading(true);
2024-07-13 19:29:10 +05:30
setMarksData([])
setBarcodeInfo([])
setPartAData([])
2024-07-03 23:13:35 +05:30
try {
const payload = {
2024-07-04 13:10:42 +05:30
qrcodeValue: scanResult,
2024-07-03 23:13:35 +05:30
};
fetch(
`${
import.meta.env.VITE_REACT_APP_BACKEND_URL
}/fetchQrcodeScannedInfo`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
}
)
.then((response) => response.json())
.then((responseData) => {
setIsLoading(false);
if (responseData.status === "success") {
setBarcodeInfo(responseData.results);
setMarksData(responseData?.marks);
2024-07-13 19:29:10 +05:30
setPartAData(responseData?.partAResults);
2024-07-03 23:13:35 +05:30
}
});
} catch (error) {
setIsLoading(false);
throw new Error(error);
}
};
2024-07-04 13:10:42 +05:30
window.fetchBarcodeData = fetchBarcodeData;
2024-07-03 23:13:35 +05:30
const success = (result) => {
2024-07-04 12:47:03 +05:30
// scannerRef.current.clear();
const readerEle = document.getElementById("reader");
if (readerEle) {
readerEle.style.visibility = "hidden";
}
2024-07-03 23:13:35 +05:30
setScanResult(result);
};
2024-07-04 13:10:42 +05:30
window.success = success;
2024-07-03 18:42:11 +05:30
2024-07-03 23:13:35 +05:30
const error = (err) => {
console.log("Error: ", err);
};
2024-07-04 13:10:42 +05:30
window.error = error;
2024-07-03 23:13:35 +05:30
// Ensure the element is in the DOM before initializing the scanner
if (document.getElementById("reader")) {
2024-07-04 12:47:03 +05:30
scannerRef.current.render(success, error);
2024-07-03 18:42:11 +05:30
}
2024-07-03 23:13:35 +05:30
// Cleanup the scanner on component unmount
return () => {
2024-07-04 12:47:03 +05:30
scannerRef.current.clear();
2024-07-03 23:13:35 +05:30
};
}, []);
2024-07-03 18:42:11 +05:30
2024-07-04 12:47:03 +05:30
const reinitializeScanner = () => {
2024-07-13 19:38:57 +05:30
console.log("calling reinitialize scanner ......")
2024-07-04 12:47:03 +05:30
setScanResult(null);
setBarcodeInfo([]);
setMarksData([]);
// if (document.getElementById("reader")) {
// console.log("Reinitializing scanner...");
// scannerRef.current.render(success, error);
// }
};
2024-07-13 19:38:57 +05:30
useEffect(()=>{
console.log("scan result ========== ",scanResult)
console.log("barcode Info ======= ",barcodeInfo)
console.log("marksData ========= ",marksData)
},[scanResult,barcodeInfo,marksData])
2024-07-04 12:47:03 +05:30
useEffect(() => {
2024-07-13 19:38:57 +05:30
console.log("Calling the use effect ..... scan result changed ...")
2024-07-04 12:47:03 +05:30
if (!scanResult) {
const readerEle = document.getElementById("reader");
console.log("Reader ELe ===== ", readerEle);
if (readerEle) {
console.log("Changing it to visible");
readerEle.style.visibility = "visible";
scannerRef.current.render(success, error);
}
2024-07-04 13:10:42 +05:30
} else {
fetchBarcodeData();
2024-07-04 12:47:03 +05:30
}
}, [scanResult]);
2024-07-03 18:42:11 +05:30
return (
2024-07-03 23:13:35 +05:30
<Box className="App">
<Box className="d-flex justify-content-center text-light bg-primary rounded py-3">
<h1>Welcome to exampaper.vidh.ai</h1>
</Box>
<Box className="my-3">
2024-07-13 19:29:10 +05:30
<Box className="d-flex justify-content-center align-items-center w-100">
2024-07-03 23:13:35 +05:30
{scanResult ? (
<h5>QR : {scanResult}</h5>
) : (
<div id="reader" style={{ width: "400px", height: "400px" }}></div>
)}
</Box>
2024-07-13 19:29:10 +05:30
{/* <Box className="d-flex d-md-none justify-content-center align-items-center w-100">
2024-07-03 23:13:35 +05:30
{scanResult ? (
<h5>QR : {scanResult}</h5>
) : (
<div id="reader" style={{ width: "400px", height: "400px" }}></div>
)}
2024-07-13 19:29:10 +05:30
</Box> */}
2024-07-03 23:13:35 +05:30
</Box>
<Box className="w-100 d-flex justify-content-center">
{barcodeInfo.length > 0 && (
<Box className="p-5 w-50 rounded shadow">
<h5>Candidate Name: {barcodeInfo[0]?.candidate_name}</h5>
<h5>Register Name : {barcodeInfo[0]?.register_number}</h5>
<h5>Subject Code : {barcodeInfo[0]?.subject_code}</h5>
<h5>Exam center code : {barcodeInfo[0]?.exam_centre_code}</h5>
<h5>Exam center : {barcodeInfo[0]?.exam_center}</h5>
</Box>
)}
2024-07-13 19:29:10 +05:30
{partAData.length > 0 && (
<Box className="p-5 w-50 rounded shadow">
<h5>Barcode : {partAData[0]?.barcode}</h5>
<h5>QRcode : {partAData[0]?.qrcode}</h5>
<h5>S.NO : {barcodeInfo[0]?.slno}</h5>
<h5>Booklet No : {barcodeInfo[0]?.booklet_serial_no}</h5>
<img src={`https://docs.exampaper.vidh.ai/${partAData?.s3_path}`} alt="PartA Image" width="50%" height="auto"/>
</Box>
)}
2024-07-03 23:13:35 +05:30
</Box>
<Box className="w-100 d-flex justify-content-center">
2024-07-04 12:47:03 +05:30
{scanResult ? (
marksData.length > 0 && barcodeInfo.length > 0 ? (
<>
<Box className="d-flex flex-column gap-4">
<Box className="p-5 rounded shadow">
<h5>Marks : {marksData[0]?.marks}</h5>
<h5>File Scanned Date : {marksData[0]?.file_scanned_date}</h5>
<h5>Cover QR code : {marksData[0]?.cover_barcode}</h5>
2024-07-13 19:29:10 +05:30
<img src={`https://docs.exampaper.vidh.ai/${marksData?.s3_path}`} alt="PartC Image" width="50%" height="auto"/>
2024-07-04 12:47:03 +05:30
</Box>
<Box>
<Button
className="p-3 bg-primary text-light rounded"
onClick={reinitializeScanner}
>
Scan Again
</Button>
</Box>
</Box>
</>
) : (
2024-07-04 13:10:42 +05:30
<Box className="w-50">
<Box className="p-5 rounded shadow">
<h5>Marks Data Not Found ..</h5>
</Box>
<Box className="my-3">
<Button
className="p-3 bg-primary text-light rounded"
2024-07-14 22:47:11 +05:30
onClick={()=>window.location.reload()}
2024-07-04 13:10:42 +05:30
>
Scan Again
</Button>
</Box>
2024-07-04 12:47:03 +05:30
</Box>
)
) : null}
2024-07-03 23:13:35 +05:30
</Box>
{isLoading && <LoadingContainer />}
</Box>
2024-07-03 18:42:11 +05:30
);
};
export default BarcodeScanner;