import React, { useState, useEffect, useMemo } from "react"; import { useSearchParams } from "react-router-dom"; import axios from "axios"; import { Search, SlidersHorizontal, X, Grid3X3, LayoutList, } from "lucide-react"; import { Button } from "../components/ui/button"; import { Input } from "../components/ui/input"; import { Badge } from "../components/ui/badge"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "../components/ui/select"; import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetTrigger, } from "../components/ui/sheet"; import { Slider } from "../components/ui/slider"; import ProductCard from "../components/cards/ProductCard"; import { getCached, setCache } from "../utils/apiCache"; const API = `${process.env.REACT_APP_BACKEND_URL}/api`; const Products = () => { const [searchParams, setSearchParams] = useSearchParams(); const [products, setProducts] = useState([]); const [loading, setLoading] = useState(true); const [search, setSearch] = useState(searchParams.get("search") || ""); const [category, setCategory] = useState( searchParams.get("category") || "all" ); const [priceRange, setPriceRange] = useState([0, 3000]); const [sortBy, setSortBy] = useState("name"); const [viewMode, setViewMode] = useState("grid"); const [filtersOpen, setFiltersOpen] = useState(false); const categories = [ { value: "all", label: "All Products" }, { value: "phones", label: "Phones" }, { value: "laptops", label: "Laptops" }, { value: "tablets", label: "Tablets" }, { value: "wearables", label: "Wearables" }, { value: "accessories", label: "Accessories" }, ]; useEffect(() => { // Scroll to top when component mounts window.scrollTo(0, 0); }, []); useEffect(() => { fetchProducts(); }, [category, search]); const fetchProducts = async () => { setLoading(true); try { const params = new URLSearchParams(); if (category && category !== "all") params.append("category", category); if (search) params.append("search", search); const cacheKey = `products-${params.toString()}`; const cached = getCached(cacheKey); if (cached) { setProducts(cached); setLoading(false); return; } const response = await axios.get(`${API}/products?${params.toString()}`); setProducts(response.data); setCache(cacheKey, response.data); } catch (error) { console.error("Failed to fetch products:", error); } finally { setLoading(false); } }; const handleSearch = (e) => { e.preventDefault(); setSearchParams({ search, category }); fetchProducts(); }; const handleCategoryChange = (value) => { setCategory(value); setSearchParams({ search, category: value }); }; const clearFilters = () => { setSearch(""); setCategory("all"); setPriceRange([0, 3000]); setSearchParams({}); }; const filteredProducts = products .filter((p) => p.price >= priceRange[0] && p.price <= priceRange[1]) .sort((a, b) => { if (sortBy === "price-low") return a.price - b.price; if (sortBy === "price-high") return b.price - a.price; return a.name.localeCompare(b.name); }); const activeFiltersCount = [ category !== "all", search !== "", priceRange[0] > 0 || priceRange[1] < 3000, ].filter(Boolean).length; const FilterContent = () => (
{/* Categories */}

Categories

{categories.map((cat) => ( ))}
{/* Price Range */}

Price Range

${priceRange[0]} ${priceRange[1]}
{/* Clear Filters */} {activeFiltersCount > 0 && ( )}
); return (
{/* Header */}

Products

Discover our wide range of premium electronics

{/* Search & Filters Bar */}
{/* Search */}
setSearch(e.target.value)} className="pl-10 rounded-full" data-testid="product-search-input" />
{/* Controls */}
{/* Sort */} {/* View Mode */}
{/* Mobile Filters */} Filters
{/* Active Filters */} {(category !== "all" || search) && (
{category !== "all" && ( {categories.find((c) => c.value === category)?.label} )} {search && ( Search: {search} )}
)} {/* Main Content */}
{/* Desktop Sidebar */} {/* Products Grid */}
{loading ? (
{[...Array(6)].map((_, i) => (
))}
) : filteredProducts.length === 0 ? (

No products found

Try adjusting your search or filters

) : ( <>

Showing {filteredProducts.length} products

{filteredProducts.map((product) => ( ))}
)}
); }; export default Products;