From 63cc4bdeff73a785bd14f26b655194aceda65e88 Mon Sep 17 00:00:00 2001 From: bender Date: Mon, 9 Mar 2026 08:22:21 +0000 Subject: [PATCH] Update src/components/ecommerce/productCatalog/ProductCatalog.tsx --- .../productCatalog/ProductCatalog.tsx | 206 +++++------------- 1 file changed, 59 insertions(+), 147 deletions(-) diff --git a/src/components/ecommerce/productCatalog/ProductCatalog.tsx b/src/components/ecommerce/productCatalog/ProductCatalog.tsx index fc04961..b0841a0 100644 --- a/src/components/ecommerce/productCatalog/ProductCatalog.tsx +++ b/src/components/ecommerce/productCatalog/ProductCatalog.tsx @@ -1,156 +1,68 @@ -"use client"; +import React, { useState, useEffect } from "react"; +import { Product } from "@/lib/api/product"; -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 { - 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; +interface CatalogProduct { + id: string; + name: string; + price: string; + imageSrc: string; + imageAlt?: string; + rating?: number; + reviewCount?: string; + category?: string; + onProductClick?: () => void; } -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(); +interface ProductCatalogProps { + products?: Product[]; + loading?: boolean; + error?: string; +} - const handleProductClick = useCallback((productId: string) => { - router.push(`/shop/${productId}`); - }, [router]); +export const ProductCatalog: React.FC = ({ + products = [], + loading = false, + error = ""}) => { + const [catalogProducts, setCatalogProducts] = useState([]); - 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... -

-
- ); + useEffect(() => { + if (!loading) { + const transformed = products.map((product) => ({ + id: product.id, + name: product.name, + price: String(product.price), + imageSrc: product.imageSrc || "/placeholder.jpg", imageAlt: product.imageAlt || product.name, + rating: product.rating, + reviewCount: product.reviewCount, + category: product.category, + brand: product.brand, + onProductClick: () => {}, + })); + setCatalogProducts(transformed); } + }, [products, loading]); - return ( -
- {(onSearchChange || (filters && filters.length > 0)) && ( -
- {onSearchChange && ( - - )} - {filters && filters.length > 0 && ( -
- {filters.map((filter) => ( - - ))} -
- )} -
- )} + if (error) { + return
Error: {error}
; + } - {products.length === 0 ? ( -

- {emptyMessage} -

- ) : ( -
- {products.map((product) => ( - - ))} -
- )} -
- ); + if (loading) { + return
Loading...
; + } + + return ( +
+
+ {catalogProducts.map((product) => ( +
+ {product.imageAlt} +

{product.name}

+

${product.price}

+ {product.rating &&
{product.rating} stars
} + {product.reviewCount &&
{product.reviewCount} reviews
} +
+ ))} +
+
+ ); }; - -ProductCatalog.displayName = "ProductCatalog"; - -export default memo(ProductCatalog); \ No newline at end of file