import { useState, useEffect } from "react"; import ProductForm from "../components/ProductForm"; const Selling = () => { // State to store user's products const [products, setProducts] = useState([]); // State to control when editing form is shown const [showForm, setShowForm] = useState(false); // State to store the product being edited (or empty for new product) const [editingProduct, setEditingProduct] = useState({ name: "", price: "", description: "", categories: [], images: [], }); // Simulate fetching products from API/database on component mount useEffect(() => { // This would be replaced with a real API call const fetchProducts = async () => { // Mock data const mockProducts = [ { id: "1", name: "Vintage Camera", price: "299.99", description: "A beautiful vintage film camera in excellent condition", categories: ["Electronics", "Art & Collectibles"], images: ["/public/Pictures/Dell1.jpg"], }, { id: "2", name: "Leather Jacket", price: "149.50", description: "Genuine leather jacket, worn only a few times", categories: ["Clothing"], images: [], }, ]; setProducts(mockProducts); }; fetchProducts(); }, []); // Handle creating or updating a product const handleSaveProduct = () => { if (editingProduct.id) { // Update existing product setProducts( products.map((p) => (p.id === editingProduct.id ? editingProduct : p)), ); } else { // Create new product const newProduct = { ...editingProduct, id: Date.now().toString(), // Generate a temporary ID }; setProducts([...products, newProduct]); } // Reset form and hide it setShowForm(false); setEditingProduct({ name: "", price: "", description: "", categories: [], images: [], }); }; // Handle product deletion const handleDeleteProduct = (productId) => { if (window.confirm("Are you sure you want to delete this product?")) { setProducts(products.filter((p) => p.id !== productId)); } }; // Handle editing a product const handleEditProduct = (product) => { setEditingProduct({ ...product, images: product.images || [], // Ensure images array exists }); setShowForm(true); }; // Handle adding a new product const handleAddProduct = () => { setEditingProduct({ name: "", price: "", description: "", categories: [], images: [], }); setShowForm(true); }; return (

My Listings

{!showForm && ( )}
{showForm ? ( setShowForm(false)} /> ) : ( <> {products.length === 0 ? (

You don't have any listings yet

) : (
{products.map((product) => (
{product.images && product.images.length > 0 ? ( {product.name} ) : (
No image
)}

{product.name}

${product.price}

{product.categories && product.categories.length > 0 && (
{product.categories.map((category) => ( {category} ))}
)}

{product.description}

))}
)} )}
); }; export default Selling;