// import { useState } from "react"; // import { Link } from "react-router-dom"; // const Transactions = () => { // return
; // }; // export default Transactions; // import { useState, useEffect } from "react"; // const Transactions = () => { // const [transactions, setTransactions] = useState([]); // const [isLoading, setIsLoading] = useState(true); // const [error, setError] = useState(null); // useEffect(() => { // const fetchTransactions = async () => { // try { // setIsLoading(true); // setError(null); // const response = await fetch( // "http://localhost:3030/api/transaction/getAllTransactions" // ); // const result = await response.json(); // if (!response.ok) { // throw new Error(result.error || "Failed to fetch transactions"); // } // setTransactions(result.transactions); // } catch (err) { // setError(err.message); // } finally { // setIsLoading(false); // } // }; // fetchTransactions(); // }, []); // if (isLoading) { // return
Loading transactions...
; // } // if (error) { // return
Error: {error}
; // } // return ( //
//

All Transactions

// {transactions.length === 0 ? ( //

No transactions found.

// ) : ( // // // // // // // // // // // // {transactions.map((tx) => ( // // // // // // // // ))} // //
Transaction IDUser IDProduct IDDatePayment Status
{tx.TransactionID}{tx.UserID}{tx.ProductID} // {new Date(tx.Date).toLocaleString()} // {tx.PaymentStatus}
// )} //
// ); // }; // export default Transactions; // src/pages/Transactions.jsx import { useState, useEffect } from "react"; import { Link } from "react-router-dom"; import { Calendar, CreditCard } from "lucide-react"; const Transactions = () => { const [transactions, setTransactions] = useState([]); const storedUser = JSON.parse(sessionStorage.getItem("user")); useEffect(() => { const fetchTransactions = async () => { try { const response = await fetch( "http://localhost:3030/api/product/getTransactions", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ userID: storedUser.ID }), } ); const data = await response.json(); const txData = data.transactions; 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, price: tx.Price ? parseFloat(tx.Price) : null, image: tx.image_url || "/default-image.jpg", date: tx.Date, status: tx.PaymentStatus, })); 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 (

My Transactions

{transactions.length === 0 ? (

No transactions yet

Once you make a purchase, your transactions will 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}

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