import React, { useState, useRef, useEffect } from "react"; import jsQR from "jsqr"; // Import jsQR library import { getImageUrl } from "../helpers/itemHelper"; import { saveCafeDetails } from "../helpers/cafeHelpers"; // Import the helper function const SetPaymentQr = ({ isConfigure, tableNo, qrCodeUrl, paymentUrl, initialQrPosition, initialQrSize, handleQrSave, shopId, // Pass cafeId as a prop to identify which cafe to update }) => { const [qrPosition, setQrPosition] = useState(initialQrPosition); const [qrSize, setQrSize] = useState(initialQrSize); const [qrPayment, setQrPayment] = useState(getImageUrl(paymentUrl)); const [qrCodeDetected, setQrCodeDetected] = useState(false); const qrPaymentInputRef = useRef(null); const overlayTextRef = useRef(null); const qrCodeContainerRef = useRef(null); // Use useEffect to detect QR code after qrPayment updates useEffect(() => { if (qrPayment) { detectQRCodeFromContainer(); } }, [qrPayment]); const handleFileChange = (e) => { const file = e.target.files[0]; if (file) { const newqrPayment = URL.createObjectURL(file); // Create a temporary URL for display setQrPayment(newqrPayment); } }; const detectQRCodeFromContainer = () => { const container = qrCodeContainerRef.current; const canvas = document.createElement("canvas"); const context = canvas.getContext("2d"); // Create an image element to load the background image const img = new Image(); img.crossOrigin = "Anonymous"; // Handle CORS if needed img.onload = () => { // Set canvas size canvas.width = container.offsetWidth; canvas.height = container.offsetHeight; // Draw image on canvas context.drawImage(img, 0, 0, canvas.width, canvas.height); const imageData = context.getImageData(0, 0, canvas.width, canvas.height); const qrCode = jsQR(imageData.data, canvas.width, canvas.height); if (qrCode) { setQrCodeDetected(true); console.log("QR Code detected:", qrCode.data); } else { setQrCodeDetected(false); console.log("No QR Code detected"); } }; img.src = qrPayment; // Load the image URL }; const handleSave = () => { const qrPaymentFile = qrPaymentInputRef.current.files[0]; // Get the selected file for qrPayment // Prepare the details object const details = { qrPosition, qrSize, qrPaymentFile, // Include qrPayment file }; // Call saveCafeDetails function with the updated details object saveCafeDetails(shopId, details) .then((response) => { console.log("Cafe details saved:", response); // handleQrSave(qrPosition, qrSize, qrPayment); }) .catch((error) => { console.error("Error saving cafe details:", error); }); }; return (
QR Code Detected
:No QR Code Detected
}