import { useState, useEffect } from "react"; import { Link, useNavigate } from "react-router-dom"; import { Tag, ChevronLeft, ChevronRight, Bookmark, BookmarkCheck, } from "lucide-react"; import FloatingAlert from "../components/FloatingAlert"; // adjust path if needed const Home = () => { const navigate = useNavigate(); const [listings, setListings] = useState([]); const [recommended, setRecommended] = useState([]); const [history, sethistory] = useState([]); const [error, setError] = useState(null); const [showAlert, setShowAlert] = useState(false); //After user data storing the session. const storedUser = JSON.parse(sessionStorage.getItem("user")); const toggleFavorite = async (id) => { const response = await fetch( "http://localhost:3030/api/product/addFavorite", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ userID: storedUser.ID, productID: id, }), }, ); const data = await response.json(); if (data.success) { setShowAlert(true); } console.log(`Add Product -> History: ${id}`); }; const addHistory = async (id) => { const response = await fetch( "http://localhost:3030/api/history/addHistory", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ userID: storedUser.ID, productID: id, }), }, ); }; function reloadPage() { var doctTimestamp = new Date(performance.timing.domLoading).getTime(); var now = Date.now(); var tenSec = 10 * 1000; if (now > doctTimestamp + tenSec) { location.reload(); } } reloadPage(); useEffect(() => { const fetchrecomProducts = async () => { try { const response = await fetch( "http://localhost:3030/api/engine/recommended", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ id: storedUser.ID, }), }, ); if (!response.ok) throw new Error("Failed to fetch products"); const data = await response.json(); if (data.success) { setRecommended( data.data.map((product) => ({ id: product.ProductID, title: product.ProductName, // Use the alias from SQL price: product.Price, category: product.Category, // Ensure this gets the category name image: product.ProductImage, // Use the alias for image URL seller: product.SellerName, // Fetch seller name properly datePosted: product.DateUploaded, // Use the actual date isFavorite: false, // Default state })), ); reloadPage(); } else { throw new Error(data.message || "Error fetching products"); } } catch (error) { console.error("Error fetching products:", error); setError(error.message); } }; fetchrecomProducts(); }, []); useEffect(() => { const fetchProducts = async () => { try { const response = await fetch( "http://localhost:3030/api/product/getProduct", ); if (!response.ok) throw new Error("Failed to fetch products"); const data = await response.json(); if (data.success) { setListings( data.data.map((product) => ({ id: product.ProductID, title: product.ProductName, // Use the alias from SQL price: product.Price, category: product.Category, // Ensure this gets the category name image: product.ProductImage, // Use the alias for image URL seller: product.SellerName, // Fetch seller name properly datePosted: product.DateUploaded, // Use the actual date isFavorite: false, // Default state })), ); } else { throw new Error(data.message || "Error fetching products"); } } catch (error) { console.error("Error fetching products:", error); setError(error.message); } }; fetchProducts(); }, []); useEffect(() => { const fetchrecomProducts = async () => { // Get the user's data from localStorage try { const response = await fetch( "http://localhost:3030/api/history/getHistory", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ id: storedUser.ID, }), }, ); if (!response.ok) throw new Error("Failed to fetch products"); const data = await response.json(); if (data.success) { sethistory( data.data.map((product) => ({ id: product.ProductID, title: product.ProductName, // Use the alias from SQL price: product.Price, category: product.Category, // Ensure this gets the category name image: product.ProductImage, // Use the alias for image URL seller: product.SellerName, // Fetch seller name properly datePosted: product.DateUploaded, // Use the actual date })), ); } else { throw new Error(data.message || "Error fetching products"); } } catch (error) { console.error("Error fetching products:", error); setError(error.message); } }; fetchrecomProducts(); }, []); const handleSelling = () => { navigate("/selling"); }; return (
{/* Hero Section with School Background */}
{/* Background Image - Positioned at bottom */}
University of Calgary {/* Dark overlay for better text readability */}
{/* Content */}

Buy and Sell on Campus

The marketplace exclusively for university students. Find everything you need or sell what you don't.

{/* Recent Listings */} {showAlert && ( setShowAlert(false)} /> )}

Recommendation

{/* Left Button - Overlaid on products */} {/* Scrollable Listings Container */}
{recommended.map((recommended) => ( addHistory(recommended.id)} className="bg-white border border-gray-200 hover:shadow-md transition-shadow w-70 flex-shrink-0 relative" >
{recommended.title}

{recommended.title}

${recommended.price}
{recommended.category}
{recommended.datePosted} {recommended.seller}
))}
{/* Right Button - Overlaid on products */}
{/* Recent Listings */} {showAlert && ( setShowAlert(false)} /> )}

Recent Listings

{/* Left Button - Overlaid on products */} {/* Scrollable Listings Container */}
{listings.map((listing) => (
{listing.title} addHistory(listing.id)} className="w-full h-48 object-cover" />

{listing.title}

${listing.price}
{listing.category}
{listing.datePosted} {listing.seller}
))}
{/* Right Button - Overlaid on products */}
{/* History Section */} {showAlert && ( setShowAlert(false)} /> )}

History

{/* Left Button - Overlaid on products */} {/* Scrollable Listings Container */}
{history.map((history) => (
{history.title}

{history.title}

${history.price}
{history.category}
{history.datePosted} {history.seller}
))}
{/* Right Button - Overlaid on products */}
{/* Footer - Added here */}
); }; export default Home;