'use client'; import { Product } from '@/lib/data'; import { ProductCard } from '@/components/cards/ProductCard'; import { CardGrid } from '@/components/cards/CardGrid'; import { useState, useMemo } from 'react'; import { Button } from '@/components/ui'; interface ProductListProps { products: Product[]; locale?: 'de' | 'en'; enableFiltering?: boolean; enableSorting?: boolean; enablePagination?: boolean; itemsPerPage?: number; } export function ProductList({ products, locale = 'de', enableFiltering = false, enableSorting = false, enablePagination = false, itemsPerPage = 12 }: ProductListProps) { const [currentPage, setCurrentPage] = useState(1); const [sortOrder, setSortOrder] = useState<'asc' | 'desc'>('asc'); const [filterStock, setFilterStock] = useState<'all' | 'instock' | 'outofstock'>('all'); // Filter products const filteredProducts = useMemo(() => { let filtered = [...products]; // Filter by stock status if (filterStock !== 'all') { filtered = filtered.filter(p => p.stockStatus === filterStock); } // Sort products if (enableSorting) { filtered.sort((a, b) => { const aPrice = parseFloat(a.regularPrice?.replace(/[^\d.]/g, '') || '0'); const bPrice = parseFloat(b.regularPrice?.replace(/[^\d.]/g, '') || '0'); if (sortOrder === 'asc') { return aPrice - bPrice; } else { return bPrice - aPrice; } }); } return filtered; }, [products, filterStock, sortOrder, enableSorting]); // Pagination const paginatedProducts = useMemo(() => { if (!enablePagination) return filteredProducts; const startIndex = (currentPage - 1) * itemsPerPage; const endIndex = startIndex + itemsPerPage; return filteredProducts.slice(startIndex, endIndex); }, [filteredProducts, currentPage, itemsPerPage, enablePagination]); const totalPages = enablePagination ? Math.ceil(filteredProducts.length / itemsPerPage) : 1; // Handlers const handleSortChange = () => { setSortOrder(prev => prev === 'asc' ? 'desc' : 'asc'); }; const handleFilterChange = (status: 'all' | 'instock' | 'outofstock') => { setFilterStock(status); setCurrentPage(1); // Reset to first page }; const handlePageChange = (page: number) => { setCurrentPage(page); window.scrollTo({ top: 0, behavior: 'smooth' }); }; // Loading state (for future use) const [isLoading, setIsLoading] = useState(false); if (products.length === 0) { return (

{locale === 'de' ? 'Keine Produkte gefunden' : 'No products found'}

{locale === 'de' ? 'Es wurden keine Produkte für diese Kategorie gefunden.' : 'No products were found for this category.'}

); } return (
{/* Controls */} {(enableFiltering || enableSorting) && (
{enableFiltering && (
)}
{enableSorting && ( )}
)} {/* Results count */}
{locale === 'de' ? `${filteredProducts.length} Produkte gefunden` : `${filteredProducts.length} products found`}
{/* Product Grid */} {paginatedProducts.map((product) => ( ))} {/* Pagination */} {enablePagination && totalPages > 1 && (
{Array.from({ length: totalPages }, (_, i) => i + 1).map((page) => ( ))}
)}
); }