From ea2ee7f7152c770d0fadfbcc482d9b8db2974584 Mon Sep 17 00:00:00 2001 From: bender Date: Wed, 4 Mar 2026 19:07:08 +0000 Subject: [PATCH] Switch to version 1: modified src/components/ecommerce/productCatalog/ProductCatalog.tsx --- .../productCatalog/ProductCatalog.tsx | 186 ++++++++++++++---- 1 file changed, 147 insertions(+), 39 deletions(-) diff --git a/src/components/ecommerce/productCatalog/ProductCatalog.tsx b/src/components/ecommerce/productCatalog/ProductCatalog.tsx index 47e5501..fc04961 100644 --- a/src/components/ecommerce/productCatalog/ProductCatalog.tsx +++ b/src/components/ecommerce/productCatalog/ProductCatalog.tsx @@ -1,48 +1,156 @@ -import React, { useState, useEffect } from 'react'; +"use client"; -interface Product { - id: string; - name: string; - price: string; - imageSrc: string; -} - -interface ProductCatalogState { - products: Product[]; - loading: boolean; - error: any; -} +import { memo, useMemo, useCallback } from "react"; +import { useRouter } from "next/navigation"; +import Input from "@/components/form/Input"; +import ProductDetailVariantSelect from "@/components/ecommerce/productDetail/ProductDetailVariantSelect"; +import type { ProductVariant } from "@/components/ecommerce/productDetail/ProductDetailCard"; +import { cls } from "@/lib/utils"; +import { useProducts } from "@/hooks/useProducts"; +import ProductCatalogItem from "./ProductCatalogItem"; +import type { CatalogProduct } from "./ProductCatalogItem"; interface ProductCatalogProps { - onProductsLoaded?: (products: Product[]) => void; + layout: "page" | "section"; + products?: CatalogProduct[]; + searchValue?: string; + onSearchChange?: (value: string) => void; + searchPlaceholder?: string; + filters?: ProductVariant[]; + emptyMessage?: string; + className?: string; + gridClassName?: string; + cardClassName?: string; + imageClassName?: string; + searchClassName?: string; + filterClassName?: string; + toolbarClassName?: string; } -export const ProductCatalog: React.FC = ({ onProductsLoaded }) => { - const [state, setState] = useState({ - products: [], - loading: false, - error: null, - }); +const ProductCatalog = ({ + layout, + products: productsProp, + searchValue = "", + onSearchChange, + searchPlaceholder = "Search products...", + filters, + emptyMessage = "No products found", + className = "", + gridClassName = "", + cardClassName = "", + imageClassName = "", + searchClassName = "", + filterClassName = "", + toolbarClassName = "", +}: ProductCatalogProps) => { + const router = useRouter(); + const { products: fetchedProducts, isLoading } = useProducts(); - useEffect(() => { - // Initialize or fetch products - setState(prev => ({ ...prev, loading: false })); - }, []); + const handleProductClick = useCallback((productId: string) => { + router.push(`/shop/${productId}`); + }, [router]); - return ( -
- {state.loading &&
Loading...
} - {state.error &&
Error: {state.error}
} -
- {state.products.map(product => ( -
-

{product.name}

-

{product.price}

-
- ))} -
-
- ); + const products: CatalogProduct[] = useMemo(() => { + if (productsProp && productsProp.length > 0) { + return productsProp; + } + + if (fetchedProducts.length === 0) { + return []; + } + + return fetchedProducts.map((product) => ({ + id: product.id, + name: product.name, + price: product.price, + imageSrc: product.imageSrc, + imageAlt: product.imageAlt || product.name, + rating: product.rating || 0, + reviewCount: product.reviewCount, + category: product.brand, + onProductClick: () => handleProductClick(product.id), + })); + }, [productsProp, fetchedProducts, handleProductClick]); + + if (isLoading && (!productsProp || productsProp.length === 0)) { + return ( +
+

+ Loading products... +

+
+ ); + } + + return ( +
+ {(onSearchChange || (filters && filters.length > 0)) && ( +
+ {onSearchChange && ( + + )} + {filters && filters.length > 0 && ( +
+ {filters.map((filter) => ( + + ))} +
+ )} +
+ )} + + {products.length === 0 ? ( +

+ {emptyMessage} +

+ ) : ( +
+ {products.map((product) => ( + + ))} +
+ )} +
+ ); }; -export default ProductCatalog; +ProductCatalog.displayName = "ProductCatalog"; + +export default memo(ProductCatalog); \ No newline at end of file