import React, { 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({ shopId, table, sendParam, deviceType, socket }) { const { shopIdentifier, tableCode } = useParams(); sendParam({ shopIdentifier, 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 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, socketId ); } console.log("transaction from " + deviceType + "success"); setIsPaymentLoading(false); }; return (