Added a delete function
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { Link } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
import { Calendar, CreditCard } from "lucide-react";
|
import { Calendar, CreditCard, Trash2 } from "lucide-react";
|
||||||
|
|
||||||
const Transactions = () => {
|
const Transactions = () => {
|
||||||
const [transactions, setTransactions] = useState([]);
|
const [transactions, setTransactions] = useState([]);
|
||||||
@@ -11,39 +11,26 @@ const Transactions = () => {
|
|||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
"http://localhost:3030/api/transaction/getTransactionsByUser",
|
"http://localhost:3030/api/transaction/getTransactionsByUser",
|
||||||
{
|
{
|
||||||
method: "POST", // Use POST to send the userID
|
method: "POST",
|
||||||
headers: {
|
headers: { "Content-Type": "application/json" },
|
||||||
"Content-Type": "application/json",
|
body: JSON.stringify({ userID: 1 }), // replace with actual userID
|
||||||
},
|
|
||||||
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}`);
|
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
||||||
const { transactions: txData } = await response.json();
|
const { transactions: txData } = await response.json();
|
||||||
|
if (!Array.isArray(txData)) return;
|
||||||
|
|
||||||
console.log("API Response:", txData); // Log the API response for debugging
|
setTransactions(
|
||||||
|
txData.map((tx) => ({
|
||||||
if (!Array.isArray(txData)) {
|
|
||||||
console.error("Expected an array but got:", txData);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const transformed = txData.map((tx) => ({
|
|
||||||
id: tx.TransactionID,
|
id: tx.TransactionID,
|
||||||
productId: tx.ProductID,
|
productId: tx.ProductID,
|
||||||
name: tx.ProductName || "Unnamed Product", // Ensure ProductName is used
|
name: tx.ProductName || "Unnamed Product",
|
||||||
price: tx.Price != null ? parseFloat(tx.Price) : null,
|
price: tx.Price != null ? parseFloat(tx.Price) : null,
|
||||||
image: tx.Image_URL || "/default-image.jpg", // Ensure Image_URL is used
|
image: tx.image_url || "/default-image.jpg",
|
||||||
date: tx.Date,
|
date: tx.Date,
|
||||||
status: tx.PaymentStatus,
|
status: tx.PaymentStatus,
|
||||||
}));
|
}))
|
||||||
|
);
|
||||||
console.log("Transformed Data:", transformed); // Log the transformed data
|
|
||||||
|
|
||||||
setTransactions(transformed);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to fetch transactions:", error);
|
console.error("Failed to fetch transactions:", error);
|
||||||
}
|
}
|
||||||
@@ -52,6 +39,27 @@ const Transactions = () => {
|
|||||||
fetchTransactions();
|
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 formatDate = (dateString) => {
|
||||||
const d = new Date(dateString);
|
const d = new Date(dateString);
|
||||||
return d.toLocaleDateString("en-US", {
|
return d.toLocaleDateString("en-US", {
|
||||||
@@ -64,7 +72,7 @@ const Transactions = () => {
|
|||||||
return (
|
return (
|
||||||
<div className="max-w-6xl mx-auto">
|
<div className="max-w-6xl mx-auto">
|
||||||
<div className="flex justify-between items-center mb-6">
|
<div className="flex justify-between items-center mb-6">
|
||||||
<h1 className="text-2xl font-bold text-gray-800">All Transactions</h1>
|
<h1 className="text-2xl font-bold text-gray-800">My Transactions</h1>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{transactions.length === 0 ? (
|
{transactions.length === 0 ? (
|
||||||
@@ -89,8 +97,19 @@ const Transactions = () => {
|
|||||||
{transactions.map((tx) => (
|
{transactions.map((tx) => (
|
||||||
<div
|
<div
|
||||||
key={tx.id}
|
key={tx.id}
|
||||||
className="border-2 border-gray-200 overflow-hidden hover:shadow-md transition-shadow"
|
className="relative border-2 border-gray-200 overflow-hidden hover:shadow-md transition-shadow"
|
||||||
>
|
>
|
||||||
|
{/* Delete Button */}
|
||||||
|
<button
|
||||||
|
onClick={(e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
deleteTransaction(tx.id);
|
||||||
|
}}
|
||||||
|
className="absolute top-2 right-2 text-red-500 hover:text-red-600 z-10"
|
||||||
|
>
|
||||||
|
<Trash2 size={20} />
|
||||||
|
</button>
|
||||||
|
|
||||||
<Link to={`/product/${tx.productId}`}>
|
<Link to={`/product/${tx.productId}`}>
|
||||||
<div className="h-48 bg-gray-200 flex items-center justify-center">
|
<div className="h-48 bg-gray-200 flex items-center justify-center">
|
||||||
{tx.image ? (
|
{tx.image ? (
|
||||||
@@ -153,7 +172,10 @@ const Transactions = () => {
|
|||||||
</Link>
|
</Link>
|
||||||
</li>
|
</li>
|
||||||
<li className="mb-1">
|
<li className="mb-1">
|
||||||
<Link to="/selling" className="hover:text-white transition">
|
<Link
|
||||||
|
to="/selling"
|
||||||
|
className="hover:text-white transition"
|
||||||
|
>
|
||||||
Sell an Item
|
Sell an Item
|
||||||
</Link>
|
</Link>
|
||||||
</li>
|
</li>
|
||||||
@@ -177,7 +199,10 @@ const Transactions = () => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="border-t border-gray-700 mt-6 pt-6 text-center text-sm text-gray-400">
|
<div className="border-t border-gray-700 mt-6 pt-6 text-center text-sm text-gray-400">
|
||||||
<p>© {new Date().getFullYear()} Campus Marketplace. All rights reserved.</p>
|
<p>
|
||||||
|
© {new Date().getFullYear()} Campus Marketplace. All rights
|
||||||
|
reserved.
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</footer>
|
</footer>
|
||||||
|
|||||||
Reference in New Issue
Block a user