import React, { useRef, useEffect, useState } from "react"; import styles from "./Invoice.module.css"; import { useParams, useLocation } from "react-router-dom"; // Changed from useSearchParams to useLocation import { ThreeDots, ColorRing } from "react-loader-spinner"; import ItemLister from "../components/ItemLister"; import { getCartDetails } from "../helpers/itemHelper"; import { handlePaymentFromClerk, handlePaymentFromGuestSide, handlePaymentFromGuestDevice, } from "../helpers/transactionHelpers"; export default function Invoice({ table, sendParam, deviceType, socket }) { const { shopId, tableCode } = useParams(); sendParam({ shopId, tableCode }); const location = useLocation(); // Use useLocation hook instead of useSearchParams const searchParams = new URLSearchParams(location.search); // Pass location.search directly // const email = searchParams.get("email"); // const orderType = searchParams.get("orderType"); // const tableNumber = searchParams.get("tableNumber"); const [cartItems, setCartItems] = useState([]); const [totalPrice, setTotalPrice] = useState(0); const [isPaymentLoading, setIsPaymentLoading] = useState(false); // State for payment button loading animation const textareaRef = useRef(null); const [orderType, setOrderType] = useState("serve"); const [tableNumber, setTableNumber] = useState(""); const [email, setEmail] = useState(""); useEffect(() => { const fetchCartItems = async () => { try { const items = await getCartDetails(shopId); setCartItems(items); // Calculate total price based on fetched cart items const totalPrice = items.reduce((total, itemType) => { return ( total + itemType.itemList.reduce((subtotal, item) => { return subtotal + item.qty * item.price; }, 0) ); }, 0); setTotalPrice(totalPrice); } catch (error) { console.error("Error fetching cart items:", error); // Handle error if needed } }; fetchCartItems(); }, [shopId]); const handlePay = async (isCash) => { setIsPaymentLoading(true); console.log("tipe" + deviceType); if (deviceType == "clerk") { const pay = await handlePaymentFromClerk( shopId, email, isCash ? "cash" : "cashless", orderType, tableNumber ); } else if (deviceType == "guestSide") { const pay = await handlePaymentFromGuestSide( shopId, email, isCash ? "cash" : "cashless", orderType, tableNumber ); } else if (deviceType == "guestDevice") { const socketId = socket.id; const pay = await handlePaymentFromGuestDevice( shopId, isCash ? "cash" : "cashless", orderType, table.tableNo || tableNumber, textareaRef.current.value, socketId ); } console.log("transaction from " + deviceType + "success"); setIsPaymentLoading(false); }; useEffect(() => { const textarea = textareaRef.current; if (textarea) { const handleResize = () => { textarea.style.height = "auto"; textarea.style.height = `${textarea.scrollHeight}px`; }; handleResize(); // Initial resize textarea.addEventListener("input", handleResize); return () => textarea.removeEventListener("input", handleResize); } }, [textareaRef.current]); const handleOrderTypeChange = (event) => { setOrderType(event.target.value); }; const handleTableNumberChange = (event) => { setTableNumber(event.target.value); }; const handleEmailChange = (event) => { setEmail(event.target.value); }; return (