import React, { useEffect, useState } from "react"; import styles from "./Transactions.module.css"; import { useParams } from "react-router-dom"; import { ColorRing } from "react-loader-spinner"; import { getMyTransactions, confirmTransaction, declineTransaction, getTransactionsFromCafe, } from "../helpers/transactionHelpers"; import { getTables } from "../helpers/tableHelper"; import TableCanvas from "../components/TableCanvas"; export default function Transactions({ shopId, propsShopId, sendParam, deviceType }) { const { shopIdentifier, tableId } = useParams(); if (sendParam) sendParam({ shopIdentifier, tableId }); const [tables, setTables] = useState([]); const [selectedTable, setSelectedTable] = useState(null); const [transactions, setTransactions] = useState([]); const [myTransactions, setMyTransactions] = useState([]); const [isPaymentLoading, setIsPaymentLoading] = useState(false); useEffect(() => { const fetchTransactions = async () => { try { let response; response = await getTransactionsFromCafe(shopId || propsShopId, 5, false); setTransactions(response); response = await getMyTransactions(shopId || propsShopId, 5); setMyTransactions(response); } catch (error) { console.error("Error fetching transactions:", error); } }; fetchTransactions(); }, [shopId || propsShopId]); useEffect(() => { const fetchData = async () => { try { const fetchedTables = await getTables(shopId || propsShopId); setTables(fetchedTables); } catch (error) { console.error("Error fetching tables:", error); } }; fetchData(); }, [shopId || propsShopId]); const calculateTotalPrice = (detailedTransactions) => { return detailedTransactions.reduce((total, dt) => { return total + dt.qty * dt.Item.price; }, 0); }; const handleConfirm = async (transactionId) => { setIsPaymentLoading(true); try { const c = await confirmTransaction(transactionId); if (c) { // Update the confirmed status locally setTransactions((prevTransactions) => prevTransactions.map((transaction) => transaction.transactionId === transactionId ? { ...transaction, confirmed: 1 } // Set to confirmed : transaction ) ); } } catch (error) { console.error("Error processing payment:", error); } finally { setIsPaymentLoading(false); } }; const handleDecline = async (transactionId) => { setIsPaymentLoading(true); try { const c = await declineTransaction(transactionId); if (c) { // Update the confirmed status locally setTransactions((prevTransactions) => prevTransactions.map((transaction) => transaction.transactionId === transactionId ? { ...transaction, confirmed: -1 } // Set to confirmed : transaction ) ); } } catch (error) { console.error("Error processing payment:", error); } finally { setIsPaymentLoading(false); } }; return (

Daftar transaksi

{/* */}
{transactions && transactions.map((transaction) => (
setSelectedTable(transaction.Table || { tableId: 0 }) } >
{transaction.confirmed === 1 ? ( ) : transaction.confirmed === -1 || transaction.confirmed === -2 ? (
) : transaction.confirmed === 2 ? ( ) : transaction.confirmed === 3 ? ( "Transaction success" ) : ( )}

{transaction.confirmed === 1 ? ( "Silahkan cek pembayaran" ) : transaction.confirmed === -1 ? ( "Dibatalkan oleh kasir" ) : transaction.confirmed === -2 ? ( "Dibatalkan oleh pelanggan" ) : transaction.confirmed === 2 ? ( "Sedang diproses" ) : transaction.confirmed === 3 ? ( "Transaction success" ) : ( "Silahkan cek ketersediaan" )}

Transaction ID: {transaction.transactionId}

Payment Type: {transaction.payment_type}

{transaction.paymentClaimed && transaction.confirmed < 2 && (

payment claimed

)}
    {transaction.DetailedTransactions.map((detail) => (
  • {detail.Item.name} - {detail.qty} x Rp{" "} {detail.Item.price}
  • ))}

{transaction.serving_type === "pickup" ? "Self pickup" : `Serve to ${transaction.Table ? transaction.Table.tableNo : "N/A" }`}

{transaction.notes != "" && ( <>
Note :