import { useState, useEffect } from "react"; import { Link } from "react-router-dom"; import { Calendar, CreditCard } from "lucide-react"; const Transactions = () => { const [transactions, setTransactions] = useState([]); useEffect(() => { const fetchTransactions = async () => { try { const response = await fetch( "http://localhost:3030/api/transaction/getTransactionsByUser", { method: "POST", // Use POST to send the userID headers: { "Content-Type": "application/json", }, body: JSON.stringify({ userID: 1, // Replace with the actual userID (e.g., from context or state) }), } ); if (!response.ok) throw new Error(`HTTP ${response.status}`); const { transactions: txData } = await response.json(); console.log("API Response:", txData); // Log the API response for debugging if (!Array.isArray(txData)) { console.error("Expected an array but got:", txData); return; } const transformed = txData.map((tx) => ({ id: tx.TransactionID, productId: tx.ProductID, name: tx.ProductName || "Unnamed Product", // Ensure ProductName is used price: tx.Price != null ? parseFloat(tx.Price) : null, image: tx.Image_URL || "/default-image.jpg", // Ensure Image_URL is used date: tx.Date, status: tx.PaymentStatus, })); console.log("Transformed Data:", transformed); // Log the transformed data setTransactions(transformed); } catch (error) { console.error("Failed to fetch transactions:", error); } }; fetchTransactions(); }, []); const formatDate = (dateString) => { const d = new Date(dateString); return d.toLocaleDateString("en-US", { year: "numeric", month: "short", day: "numeric", }); }; return (

All Transactions

{transactions.length === 0 ? (

No transactions found

Once transactions are created, they’ll appear here.

Browse Listings
) : ( <>
{transactions.map((tx) => (
{tx.image ? ( {tx.name} ) : (
No image
)}

{tx.name}

{tx.price !== null && (

${tx.price.toFixed(2)}

)}
{formatDate(tx.date)}

Status: {tx.status}

))}
Showing {transactions.length}{" "} {transactions.length === 1 ? "transaction" : "transactions"}
)}
); }; export default Transactions;