import { useState, useEffect } from "react"; import { Link } from "react-router-dom"; import { Heart, Tag, Trash2, Filter, ChevronDown } from "lucide-react"; const Favorites = () => { const [favorites, setFavorites] = useState([]); const [showFilters, setShowFilters] = useState(false); const [sortBy, setSortBy] = useState("dateAdded"); const [filterCategory, setFilterCategory] = useState("All"); const storedUser = JSON.parse(sessionStorage.getItem("user")); const mapCategory = (id) => { const categories = { 1: "Electronics", 2: "Textbooks", 3: "Furniture", 4: "Clothing", 5: "Kitchen", 6: "Other", }; return categories[id] || "Other"; }; function reloadPage() { var doctTimestamp = new Date(performance.timing.domLoading).getTime(); var now = Date.now(); if (now > doctTimestamp) { location.reload(); } } const removeFromFavorites = async (itemID) => { const response = await fetch( "http://localhost:3030/api/product/delFavorite", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ userID: storedUser.ID, productID: itemID, }), }, ); const data = await response.json(); if (data.success) { reloadPage(); } if (!response.ok) throw new Error("Failed to fetch products"); console.log(response); console.log(`Add Product -> History: ${itemID}`); }; useEffect(() => { const fetchFavorites = async () => { try { const response = await fetch( "http://localhost:3030/api/product/getFavorites", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ userID: storedUser.ID }), }, ); const data = await response.json(); const favoritesData = data.favorites; if (!Array.isArray(favoritesData)) { console.error("Expected an array but got:", favoritesData); return; } const transformed = favoritesData.map((item) => ({ id: item.ProductID, title: item.Name, price: parseFloat(item.Price), category: mapCategory(item.CategoryID), // 👈 map numeric category to a string image: item.image_url || "/default-image.jpg", condition: "Used", // or another field if you add `Condition` to your DB seller: item.SellerName, datePosted: formatDatePosted(item.Date), dateAdded: item.Date || new Date().toISOString(), })); setFavorites(transformed); } catch (error) { console.error("Failed to fetch favorites:", error); } }; fetchFavorites(); }, []); const formatDatePosted = (dateString) => { const postedDate = new Date(dateString); const today = new Date(); const diffInMs = today - postedDate; const diffInDays = Math.floor(diffInMs / (1000 * 60 * 60 * 24)); return `${diffInDays}d ago`; }; const sortedFavorites = [...favorites].sort((a, b) => { if (sortBy === "dateAdded") return new Date(b.dateAdded) - new Date(a.dateAdded); if (sortBy === "priceHigh") return b.price - a.price; if (sortBy === "priceLow") return a.price - b.price; return 0; }); const filteredFavorites = filterCategory === "All" ? sortedFavorites : sortedFavorites.filter((item) => item.category === filterCategory); // rest of the JSX remains unchanged... return (

My Favorites

{/* Filters and Sorting */} {showFilters && (
)} {/* Favorites List */} {filteredFavorites.length === 0 ? (

No favorites yet

Items you save will appear here. Start browsing to add items to your favorites.

Browse Listings
) : (
{filteredFavorites.map((item) => (
{item.title}

{item.title}

${item.price}
{item.category} • {item.condition}
Listed {item.datePosted} {item.seller}
))}
)} {/* Show count if there are favorites */} {filteredFavorites.length > 0 && (
Showing {filteredFavorites.length}{" "} {filteredFavorites.length === 1 ? "item" : "items"} {filterCategory !== "All" && ` in ${filterCategory}`}
)}
); }; export default Favorites;