diff --git a/frontend/src/pages/Transactions.jsx b/frontend/src/pages/Transactions.jsx index 0baae61..fa096fb 100644 --- a/frontend/src/pages/Transactions.jsx +++ b/frontend/src/pages/Transactions.jsx @@ -1,6 +1,6 @@ import { useState, useEffect } from "react"; import { Link } from "react-router-dom"; -import { Calendar, CreditCard } from "lucide-react"; +import { Calendar, CreditCard, Trash2 } from "lucide-react"; const Transactions = () => { const [transactions, setTransactions] = useState([]); @@ -11,47 +11,55 @@ const Transactions = () => { 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) - }), + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ userID: 1 }), // replace with actual userID } ); - 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); + if (!Array.isArray(txData)) return; + + setTransactions( + txData.map((tx) => ({ + id: tx.TransactionID, + productId: tx.ProductID, + name: tx.ProductName || "Unnamed Product", + price: tx.Price != null ? parseFloat(tx.Price) : null, + image: tx.image_url || "/default-image.jpg", + date: tx.Date, + status: tx.PaymentStatus, + })) + ); } catch (error) { console.error("Failed to fetch transactions:", error); } }; - + fetchTransactions(); }, []); + const deleteTransaction = async (id) => { + try { + const res = await fetch( + "http://localhost:3030/api/transaction/deleteTransaction", + { + method: "DELETE", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ transactionID: id }), + } + ); + const data = await res.json(); + if (data.success) { + setTransactions((prev) => prev.filter((tx) => tx.id !== id)); + } else { + console.error("Delete failed:", data.message); + } + } catch (err) { + console.error("Error deleting transaction:", err); + } + }; + const formatDate = (dateString) => { const d = new Date(dateString); return d.toLocaleDateString("en-US", { @@ -64,7 +72,7 @@ const Transactions = () => { return (
-

All Transactions

+

My Transactions

{transactions.length === 0 ? ( @@ -89,8 +97,19 @@ const Transactions = () => { {transactions.map((tx) => (
+ {/* Delete Button */} + +
{tx.image ? ( @@ -153,7 +172,10 @@ const Transactions = () => {
  • - + Sell an Item
  • @@ -177,7 +199,10 @@ const Transactions = () => {
    -

    © {new Date().getFullYear()} Campus Marketplace. All rights reserved.

    +

    + © {new Date().getFullYear()} Campus Marketplace. All rights + reserved. +

    @@ -185,4 +210,4 @@ const Transactions = () => { ); }; -export default Transactions; \ No newline at end of file +export default Transactions;