From 76741e304f87b13d6780d9a6f01c169529096f34 Mon Sep 17 00:00:00 2001
From: frontend perkafean
<165241731+frontendperkafean@users.noreply.github.com>
Date: Mon, 5 Aug 2024 09:44:01 +0000
Subject: [PATCH] working on qr
---
src/components/QR.js | 156 +++++++++++++++++++++++++++--------
src/components/TableList.js | 28 ++++---
src/components/TablesPage.js | 1 +
src/helpers/tableHelper.js | 23 ++++++
4 files changed, 162 insertions(+), 46 deletions(-)
diff --git a/src/components/QR.js b/src/components/QR.js
index c20d2f9..91336a0 100644
--- a/src/components/QR.js
+++ b/src/components/QR.js
@@ -1,8 +1,9 @@
-import React, { useState } from "react";
+import React, { useState, useRef } from "react";
import html2canvas from "html2canvas";
const QRCodeWithBackground = ({
isConfigure,
+ tableNo,
qrCodeUrl,
backgroundUrl,
initialQrPosition,
@@ -14,6 +15,8 @@ const QRCodeWithBackground = ({
const [qrPosition, setQrPosition] = useState(initialQrPosition);
const [qrSize, setQrSize] = useState(initialQrSize);
const [bgImage, setBgImage] = useState(backgroundUrl);
+ const fileInputRef = useRef(null);
+ const overlayTextRef = useRef(null);
const handlePositionChange = (e) => {
const { name, value } = e.target;
@@ -45,6 +48,10 @@ const QRCodeWithBackground = ({
const printQRCode = () => {
const element = document.getElementById("qr-code-container");
if (element) {
+ // Hide overlay text
+ const overlayText = overlayTextRef.current;
+ if (overlayText) overlayText.style.display = "none";
+
html2canvas(element, {
useCORS: true,
backgroundColor: null,
@@ -65,12 +72,49 @@ const QRCodeWithBackground = ({
})
.catch((err) => {
console.error("Error capturing image:", err);
+ })
+ .finally(() => {
+ // Show overlay text again
+ if (overlayText) overlayText.style.display = "flex";
});
} else {
console.error("Element not found for printing.");
}
};
+ const saveImage = () => {
+ const element = document.getElementById("qr-code-container");
+ if (element) {
+ // Hide overlay text
+ const overlayText = overlayTextRef.current;
+ if (overlayText) overlayText.style.display = "none";
+
+ html2canvas(element, {
+ useCORS: true,
+ backgroundColor: null,
+ })
+ .then((canvas) => {
+ canvas.toBlob((blob) => {
+ const link = document.createElement("a");
+ link.href = URL.createObjectURL(blob);
+ link.download = `table ${tableNo}_QRCode.png`;
+ document.body.appendChild(link);
+ link.click();
+ document.body.removeChild(link);
+ });
+ })
+ .catch((err) => {
+ console.error("Error capturing image:", err);
+ })
+ .finally(() => {
+ // Show overlay text again
+ if (overlayText) overlayText.style.display = "flex";
+ });
+ } else {
+ console.error("Element not found for saving.");
+ }
+ };
+
return (
+ {/* Overlay text that triggers file input */}
+
fileInputRef.current.click()}
+ >
+ Click To Change Image
+
+ {/* Hidden file input */}
+
{isConfigure ? (
@@ -159,17 +219,6 @@ const QRCodeWithBackground = ({
-
-
-
) : (
-
+
+
+
+
)}
);
@@ -257,6 +331,20 @@ const styles = {
fileInput: {
marginLeft: "10px",
},
+ overlayText: {
+ position: "absolute",
+ fontSize: "70px",
+ backgroundColor: "rgba(0, 0, 0, 0.7)",
+ top: "0",
+ bottom: "0",
+ color: "white",
+ width: "100%",
+ display: "flex",
+ alignItems: "center",
+ justifyContent: "center",
+ cursor: "pointer", // Indicates it's clickable
+ zIndex: 10, // Ensure it appears above other elements
+ },
};
export default QRCodeWithBackground;
diff --git a/src/components/TableList.js b/src/components/TableList.js
index c9a24c7..7ef36b0 100644
--- a/src/components/TableList.js
+++ b/src/components/TableList.js
@@ -1,7 +1,7 @@
import React, { useState } from "react";
import QRCodeWithBackground from "./QR"; // Adjust path as needed
-const TableList = ({ tables, onSelectTable, selectedTable }) => {
+const TableList = ({ shopUrl, tables, onSelectTable, selectedTable }) => {
const [initialPos, setInitialPos] = useState({ left: 50, top: 50 });
const [initialSize, setInitialSize] = useState(20);
const [bgImageUrl, setBgImageUrl] = useState(
@@ -10,7 +10,7 @@ const TableList = ({ tables, onSelectTable, selectedTable }) => {
const generateQRCodeUrl = (tableCode) =>
`https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=${encodeURIComponent(
- tableCode
+ shopUrl + "/" + tableCode
)}`;
const handleBackgroundUrlChange = (newUrl) => {
@@ -77,16 +77,20 @@ const TableList = ({ tables, onSelectTable, selectedTable }) => {
{table.tableNo === 0 ? "Clerk" : `Table ${table.tableNo}`} -
Position: ({table.xposition}, {table.yposition})
- {table.tableId == selectedTable?.tableId && (
-
+ {table.tableNo != 0 && table.tableId == selectedTable?.tableId && (
+ <>
+
+ {shopUrl + "/" + table.tableCode}
+ >
)}
))}
diff --git a/src/components/TablesPage.js b/src/components/TablesPage.js
index 8c250a8..c2b1ea2 100644
--- a/src/components/TablesPage.js
+++ b/src/components/TablesPage.js
@@ -201,6 +201,7 @@ const TablesPage = ({ shopId }) => {
tableNo={tableNo}
/>