import { useState, useEffect } from "react"; import { useParams, Link } from "react-router-dom"; import { Heart, ArrowLeft, Tag, User, Calendar } from "lucide-react"; const ProductDetail = () => { const { id } = useParams(); const [product, setProduct] = useState(null); const [isFavorite, setIsFavorite] = useState(false); const [showContactForm, setShowContactForm] = useState(false); const [message, setMessage] = useState(""); const [currentImage, setCurrentImage] = useState(0); // Fetch product details useEffect(() => { const fetchProduct = async () => { try { const response = await fetch( `http://localhost:3030/api/product/get_productID/${id}`, ); if (!response.ok) throw new Error("Failed to fetch product"); const data = await response.json(); if (data.success) { setProduct(data.data); // Update the state with product details } else { throw new Error(data.message || "Error fetching product"); } } catch (error) { console.error("Error fetching product:", error); } }; fetchProduct(); }, [id]); // Handle favorite toggle const toggleFavorite = () => { setIsFavorite(!isFavorite); }; // Handle message submission const handleSendMessage = (e) => { e.preventDefault(); // Handle message logic here (send to seller) console.log("Message sent:", message); setMessage(""); setShowContactForm(false); alert("Message sent to seller!"); }; // Image navigation const nextImage = () => { setCurrentImage((prev) => prev === product.images.length - 1 ? 0 : prev + 1, ); }; const prevImage = () => { setCurrentImage((prev) => prev === 0 ? product.images.length - 1 : prev - 1, ); }; const selectImage = (index) => { setCurrentImage(index); }; if (!product) return
Loading...
; // Handle loading state return (
Back to listings
{product.title}
{product.images.length > 1 && (
{product.images.map((image, index) => (
selectImage(index)} > {`${product.title}
))}
)}

{product.title}

${product.price}
{product.category}
Condition: {product.condition}
Posted on {product.datePosted}

{product.shortDescription}

{showContactForm && (

Contact Seller

setMessage(e.target.value)} className="w-full p-3 border border-gray-300 focus:outline-none focus:border-green-500" required />
setMessage(e.target.value)} placeholder="Hi, is this item still available?" className="w-full p-3 border border-gray-300 focus:outline-none focus:border-green-500" />
)}
{product.seller.avatar ? ( Seller ) : (
)}

{product.seller.name}

Member since {product.seller.memberSince}

Rating:{" "} {product.seller.rating}/5

Description

{product.description}
); }; export default ProductDetail;