Files
Campus-Plug/frontend/src/pages/Home.jsx

455 lines
16 KiB
React
Raw Normal View History

2025-03-18 18:09:15 -06:00
import { useState, useEffect } from "react";
2025-03-14 16:14:10 -06:00
import { Link, useNavigate } from "react-router-dom";
2025-04-03 18:56:39 -06:00
import { Tag, Heart } from "lucide-react";
2025-03-05 22:30:52 -07:00
const Home = () => {
const navigate = useNavigate();
2025-03-18 18:09:15 -06:00
const [listings, setListings] = useState([]);
2025-04-03 18:56:39 -06:00
const [recommended, setRecommended] = useState([]);
2025-04-04 00:02:04 -06:00
const [history, sethistory] = useState([]);
2025-03-18 18:09:15 -06:00
const [error, setError] = useState(null);
2025-04-03 18:56:39 -06:00
useEffect(() => {
const fetchrecomProducts = async () => {
// Get the user's data from localStorage
const storedUser = JSON.parse(sessionStorage.getItem("user"));
console.log(storedUser);
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();
console.log(data);
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
})),
);
} else {
throw new Error(data.message || "Error fetching products");
}
} catch (error) {
console.error("Error fetching products:", error);
setError(error.message);
}
};
fetchrecomProducts();
}, []);
2025-03-18 18:09:15 -06:00
useEffect(() => {
const fetchProducts = async () => {
try {
const response = await fetch(
2025-03-24 23:04:12 -06:00
"http://localhost:3030/api/product/get_product",
);
2025-03-18 18:09:15 -06:00
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,
2025-03-25 14:47:54 -06:00
title: product.ProductName, // Use the alias from SQL
2025-03-18 18:09:15 -06:00
price: product.Price,
2025-03-25 14:47:54 -06:00
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
2025-03-24 23:04:12 -06:00
})),
2025-03-18 18:09:15 -06:00
);
} else {
throw new Error(data.message || "Error fetching products");
}
} catch (error) {
console.error("Error fetching products:", error);
setError(error.message);
}
};
fetchProducts();
}, []);
2025-03-14 16:14:10 -06:00
2025-04-04 00:02:04 -06:00
useEffect(() => {
const fetchrecomProducts = async () => {
// Get the user's data from localStorage
const storedUser = JSON.parse(sessionStorage.getItem("user"));
console.log(storedUser);
try {
const response = await fetch("http://localhost:3030/api/get/history", {
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();
console.log(data);
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
isFavorite: false, // Default state
})),
);
} else {
throw new Error(data.message || "Error fetching products");
}
} catch (error) {
console.error("Error fetching products:", error);
setError(error.message);
}
};
fetchrecomProducts();
}, []);
2025-03-05 22:30:52 -07:00
// Toggle favorite status
const toggleFavorite = (id, e) => {
e.preventDefault(); // Prevent navigation when clicking the heart icon
2025-03-18 18:09:15 -06:00
setListings((prevListings) =>
prevListings.map((listing) =>
2025-03-14 16:14:10 -06:00
listing.id === id
? { ...listing, isFavorite: !listing.isFavorite }
2025-03-24 23:04:12 -06:00
: listing,
),
2025-03-05 22:30:52 -07:00
);
};
const handleSelling = () => {
2025-03-14 16:14:10 -06:00
navigate("/selling");
};
2025-03-05 22:30:52 -07:00
return (
<div>
2025-03-14 16:14:10 -06:00
{/* Hero Section with School Background */}
<div className="relative py-12 px-4 mb-8 shadow-sm">
{/* Background Image - Positioned at bottom */}
<div className="absolute inset-0 z-0 overflow-hidden bg-black bg-opacity-100">
<img
src="../public/Ucalgary.png"
alt="University of Calgary"
className="w-full h-full object-cover object-bottom opacity-50"
/>
{/* Dark overlay for better text readability */}
</div>
{/* Content */}
<div className="max-w-2xl mx-auto text-center relative z-1">
<h1 className="text-3xl font-bold text-white mb-4">
2025-03-05 22:30:52 -07:00
Buy and Sell on Campus
</h1>
2025-03-14 16:14:10 -06:00
<p className="text-white mb-6">
The marketplace exclusively for university students. Find everything
you need or sell what you don't.
2025-03-05 22:30:52 -07:00
</p>
2025-03-14 16:14:10 -06:00
<button
onClick={handleSelling}
className="bg-green-500 hover:bg-green-600 text-white font-medium py-2 px-6 focus:outline-none focus:ring-2 focus:ring-green-400 transition-colors"
>
2025-03-05 22:30:52 -07:00
Post an Item
</button>
</div>
</div>
2025-03-14 16:14:10 -06:00
2025-03-05 22:30:52 -07:00
{/* Recent Listings */}
2025-03-29 17:28:09 -06:00
<div className="relative py-4">
2025-03-14 16:14:10 -06:00
<h2 className="text-xl font-semibold text-gray-800 mb-4">
2025-03-29 17:28:09 -06:00
Recommendation
2025-03-14 16:14:10 -06:00
</h2>
2025-03-29 17:28:09 -06:00
<div className="relative">
{/* Left Button - Overlaid on products */}
<button
onClick={() =>
document
.getElementById("RecomContainer")
.scrollBy({ left: -400, behavior: "smooth" })
}
className="absolute left-0 top-1/2 transform -translate-y-1/2 bg-gray-800 bg-opacity-70 text-white p-4 rounded-full z-20 hidden md:flex items-center justify-center w-12 h-12"
>
</button>
{/* Scrollable Listings Container */}
<div
id="RecomContainer"
className="overflow-x-auto whitespace-nowrap flex space-x-6 scroll-smooth scrollbar-hide px-10 pl-0"
>
2025-04-03 18:56:39 -06:00
{recommended.map((recommended) => (
2025-03-29 17:28:09 -06:00
<Link
2025-04-03 18:56:39 -06:00
key={recommended.id}
to={`/product/${recommended.id}`}
2025-03-29 17:28:09 -06:00
className="bg-white border border-gray-200 hover:shadow-md transition-shadow w-70 flex-shrink-0 relative"
>
<div className="relative">
<img
2025-04-03 18:56:39 -06:00
src={recommended.image}
alt={recommended.title}
2025-03-29 17:28:09 -06:00
className="w-full h-48 object-cover"
2025-03-05 22:30:52 -07:00
/>
2025-03-29 17:28:09 -06:00
<button
2025-04-03 18:56:39 -06:00
onClick={(e) => toggleFavorite(recommended.id, e)}
2025-03-29 17:28:09 -06:00
className="absolute top-2 right-2 p-2 bg-white rounded-full shadow-sm"
>
<Heart
className={`h-6 w-6 ${
2025-04-03 18:56:39 -06:00
recommended.isFavorite
2025-03-29 17:28:09 -06:00
? "text-red-500 fill-red-500"
: "text-gray-400"
}`}
/>
</button>
</div>
2025-03-14 16:14:10 -06:00
2025-03-29 17:28:09 -06:00
<div className="p-4">
2025-03-05 22:30:52 -07:00
<h3 className="text-lg font-medium text-gray-800 leading-tight">
2025-04-03 18:56:39 -06:00
{recommended.title}
2025-03-05 22:30:52 -07:00
</h3>
2025-03-29 17:28:09 -06:00
<span className="font-semibold text-green-600 block mt-1">
2025-04-03 18:56:39 -06:00
${recommended.price}
2025-03-14 16:14:10 -06:00
</span>
2025-03-29 17:28:09 -06:00
<div className="flex items-center text-sm text-gray-500 mt-2">
<Tag className="h-4 w-4 mr-1" />
2025-04-03 18:56:39 -06:00
<span>{recommended.category}</span>
2025-03-29 17:28:09 -06:00
</div>
<div className="flex justify-between items-center pt-2 border-t border-gray-100 mt-3">
<span className="text-xs text-gray-500">
2025-04-03 18:56:39 -06:00
{recommended.datePosted}
2025-03-29 17:28:09 -06:00
</span>
<span className="text-sm font-medium text-gray-700">
2025-04-03 18:56:39 -06:00
{recommended.seller}
2025-03-29 17:28:09 -06:00
</span>
</div>
2025-03-05 22:30:52 -07:00
</div>
2025-03-29 17:28:09 -06:00
</Link>
))}
</div>
2025-03-14 16:14:10 -06:00
2025-03-29 17:28:09 -06:00
{/* Right Button - Overlaid on products */}
<button
onClick={() =>
document
.getElementById("RecomContainer")
.scrollBy({ left: 400, behavior: "smooth" })
}
className="absolute right-0 top-1/2 transform -translate-y-1/2 bg-gray-800 bg-opacity-70 text-white p-4 rounded-full z-20 hidden md:flex items-center justify-center w-12 h-12"
>
</button>
</div>
</div>
{/* Recent Listings */}
<div className="relative py-4">
<h2 className="text-xl font-semibold text-gray-800 mb-4">
Recent Listings
</h2>
<div className="relative">
{/* Left Button - Overlaid on products */}
<button
onClick={() =>
document
.getElementById("listingsContainer")
.scrollBy({ left: -400, behavior: "smooth" })
}
className="absolute left-0 top-1/2 transform -translate-y-1/2 bg-gray-800 bg-opacity-70 text-white p-4 rounded-full z-20 hidden md:flex items-center justify-center w-12 h-12"
>
</button>
{/* Scrollable Listings Container */}
<div
id="listingsContainer"
className="overflow-x-auto whitespace-nowrap flex space-x-6 scroll-smooth scrollbar-hide px-10 pl-0"
>
{listings.map((listing) => (
<Link
key={listing.id}
to={`/product/${listing.id}`}
className="bg-white border border-gray-200 hover:shadow-md transition-shadow w-70 flex-shrink-0 relative"
>
<div className="relative">
<img
src={listing.image}
alt={listing.title}
className="w-full h-48 object-cover"
/>
<button
onClick={(e) => toggleFavorite(listing.id, e)}
className="absolute top-2 right-2 p-2 bg-white rounded-full shadow-sm"
>
<Heart
className={`h-6 w-6 ${
listing.isFavorite
? "text-red-500 fill-red-500"
: "text-gray-400"
}`}
/>
</button>
2025-03-05 22:30:52 -07:00
</div>
2025-03-14 16:14:10 -06:00
2025-03-29 17:28:09 -06:00
<div className="p-4">
<h3 className="text-lg font-medium text-gray-800 leading-tight">
{listing.title}
</h3>
<span className="font-semibold text-green-600 block mt-1">
${listing.price}
2025-03-14 16:14:10 -06:00
</span>
2025-03-29 17:28:09 -06:00
<div className="flex items-center text-sm text-gray-500 mt-2">
<Tag className="h-4 w-4 mr-1" />
<span>{listing.category}</span>
</div>
<div className="flex justify-between items-center pt-2 border-t border-gray-100 mt-3">
<span className="text-xs text-gray-500">
{listing.datePosted}
</span>
<span className="text-sm font-medium text-gray-700">
{listing.seller}
</span>
</div>
2025-03-05 22:30:52 -07:00
</div>
2025-03-29 17:28:09 -06:00
</Link>
))}
</div>
{/* Right Button - Overlaid on products */}
<button
onClick={() =>
document
.getElementById("listingsContainer")
.scrollBy({ left: 400, behavior: "smooth" })
}
className="absolute right-0 top-1/2 transform -translate-y-1/2 bg-gray-800 bg-opacity-70 text-white p-4 rounded-full z-20 hidden md:flex items-center justify-center w-12 h-12"
>
</button>
2025-03-05 22:30:52 -07:00
</div>
</div>
2025-04-04 00:02:04 -06:00
{/* Recent Listings */}
<div className="relative py-4">
<h2 className="text-xl font-semibold text-gray-800 mb-4">History</h2>
<div className="relative">
{/* Left Button - Overlaid on products */}
<button
onClick={() =>
document
.getElementById("HistoryContainer")
.scrollBy({ left: -400, behavior: "smooth" })
}
className="absolute left-0 top-1/2 transform -translate-y-1/2 bg-gray-800 bg-opacity-70 text-white p-4 rounded-full z-20 hidden md:flex items-center justify-center w-12 h-12"
>
</button>
{/* Scrollable Listings Container */}
<div
id="HistoryContainer"
className="overflow-x-auto whitespace-nowrap flex space-x-6 scroll-smooth scrollbar-hide px-10 pl-0"
>
{history.map((history) => (
<Link
key={history.id}
to={`/product/${history.id}`}
className="bg-white border border-gray-200 hover:shadow-md transition-shadow w-70 flex-shrink-0 relative"
>
<div className="relative">
<img
src={history.image}
alt={history.title}
className="w-full h-48 object-cover"
/>
<button
onClick={(e) => toggleFavorite(history.id, e)}
className="absolute top-2 right-2 p-2 bg-white rounded-full shadow-sm"
>
<Heart
className={`h-6 w-6 ${
history.isFavorite
? "text-red-500 fill-red-500"
: "text-gray-400"
}`}
/>
</button>
</div>
<div className="p-4">
<h3 className="text-lg font-medium text-gray-800 leading-tight">
{history.title}
</h3>
<span className="font-semibold text-green-600 block mt-1">
${history.price}
</span>
<div className="flex items-center text-sm text-gray-500 mt-2">
<Tag className="h-4 w-4 mr-1" />
<span>{history.category}</span>
</div>
<div className="flex justify-between items-center pt-2 border-t border-gray-100 mt-3">
<span className="text-xs text-gray-500">
{history.datePosted}
</span>
<span className="text-sm font-medium text-gray-700">
{history.seller}
</span>
</div>
</div>
</Link>
))}
</div>
{/* Right Button - Overlaid on products */}
<button
onClick={() =>
document
.getElementById("HistoryContainer")
.scrollBy({ left: 400, behavior: "smooth" })
}
className="absolute right-0 top-1/2 transform -translate-y-1/2 bg-gray-800 bg-opacity-70 text-white p-4 rounded-full z-20 hidden md:flex items-center justify-center w-12 h-12"
>
</button>
</div>
</div>
2025-03-05 22:30:52 -07:00
</div>
);
};
2025-03-14 16:14:10 -06:00
export default Home;