import React, { useState, useRef, useEffect } from "react"; import jsQR from "jsqr"; import { getImageUrl } from "../helpers/itemHelper"; import { getTables, updateTable, createTable } from "../helpers/tableHelper"; import { getCafe, saveCafeDetails, setConfirmationStatus, } from "../helpers/cafeHelpers"; import Switch from "react-switch"; // Import the Switch component const SetPaymentQr = ({ shop }) => { const [initialPos, setInitialPos] = useState({ left: shop.xposition, top: shop.yposition, }); const [isConfigFont, setIsConfigFont] = useState(false); const [fontsize, setfontsize] = useState(shop.fontsize); const [fontcolor, setfontcolor] = useState(shop.fontcolor); const [initialFontPos, setInitialFontPos] = useState({ left: shop.fontxposition, top: shop.fontyposition, }); const [initialSize, setInitialSize] = useState(shop.scale); const [bgImageUrl, setBgImageUrl] = useState(getImageUrl(shop.qrBackground)); const qrBackgroundInputRef = useRef(null); const shopUrl = window.location.hostname + "/" + shop.cafeId; const generateQRCodeUrl = (tableCode) => { if (tableCode != null) { return `https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=${encodeURIComponent( shopUrl + "/" + tableCode )}`; } else { return `https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=${encodeURIComponent( shopUrl )}`; } }; const [isConfig, setIsConfig] = useState(false); const [isConfigQR, setIsConfigQR] = useState(false); const [isViewTables, setIsViewTables] = useState(false); const [tables, setTables] = useState([]); const [selectedTable, setSelectedTable] = useState(null); useEffect(() => { const fetchData = async () => { try { console.log(shop); const fetchedTables = await getTables(shop.cafeId); setTables(fetchedTables); } catch (error) { console.error("Error fetching tables:", error); } }; fetchData(); }, [shop.cafeId]); const handleSave = () => { const qrBackgroundFile = qrBackgroundInputRef.current.files[0]; // Get the selected file for qrBackground // Prepare the details object const details = { qrSize: initialSize, qrPosition: initialPos, qrBackgroundFile, fontsize, fontcolor, fontPosition: initialFontPos, }; // Call saveCafeDetails function with the updated details object saveCafeDetails(shop.cafeId, details) .then((response) => { console.log("Cafe details saved:", response); }) .catch((error) => { console.error("Error saving cafe details:", error); }); }; const handlePositionChange = (e) => { const { name, value } = e.target; setInitialPos((prevPosition) => ({ ...prevPosition, [name]: parseFloat(value).toFixed(2), })); }; const handleFontPositionChange = (e) => { const { name, value } = e.target; setInitialFontPos((prevPosition) => ({ ...prevPosition, [name]: parseFloat(value).toFixed(2), })); }; const handleSizeChange = (e) => { setInitialSize(parseFloat(e.target.value).toFixed(2)); }; const handleFontSizeChange = (e) => { setfontsize(parseFloat(e.target.value).toFixed(2)); }; const handleFileChange = (e) => { const file = e.target.files[0]; if (file) { const newBgImage = URL.createObjectURL(file); // Create a temporary URL for display setBgImageUrl(newBgImage); } }; return (
Klik untuk ganti background
setIsConfig(!isConfig)}> {isConfig ? '˅' : '˃'} Konfigurasi
{ setIsConfigQR(!isConfigQR); setIsConfigFont(false) }}> {isConfigQR ? '˅' : '˃'} Konfigurasi QR
{isConfigQR && <>{ setIsConfigFont(!isConfigFont); setIsConfigQR(false) }}> {isConfigFont ? '˅' : '˃'} Konfigurasi nomor
{isConfigFont && ( <>setIsViewTables(!isViewTables)}> {isViewTables ? '˅' : '˃'} Daftar meja
{isViewTables &&