From 5fe9d5a8a4956efc7ebc17f4b6c2243c2092c68b Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Sun, 28 Sep 2025 11:19:00 +0330 Subject: [PATCH] cart item --- src/app/blogs/[id]/page.tsx | 2 +- .../brand/components/BrandFilterSection.tsx | 2 +- src/app/compare/page.tsx | 23 +++- src/share/components/Cart.tsx | 109 +++++++++++++++--- src/share/hooks/useOfflineCartProducts.ts | 89 ++++++++++++++ 5 files changed, 202 insertions(+), 23 deletions(-) create mode 100644 src/share/hooks/useOfflineCartProducts.ts diff --git a/src/app/blogs/[id]/page.tsx b/src/app/blogs/[id]/page.tsx index a42c71e..0c5d17d 100644 --- a/src/app/blogs/[id]/page.tsx +++ b/src/app/blogs/[id]/page.tsx @@ -120,7 +120,7 @@ const BlogDetail: NextPage = () => { {/* Sidebar */}
- +
diff --git a/src/app/brand/components/BrandFilterSection.tsx b/src/app/brand/components/BrandFilterSection.tsx index 9d88498..910edf0 100644 --- a/src/app/brand/components/BrandFilterSection.tsx +++ b/src/app/brand/components/BrandFilterSection.tsx @@ -55,7 +55,7 @@ const BrandFilterSection: FC = ({
id && onCategoryChange(id)} /> diff --git a/src/app/compare/page.tsx b/src/app/compare/page.tsx index 8d4747a..3e92c4f 100644 --- a/src/app/compare/page.tsx +++ b/src/app/compare/page.tsx @@ -5,7 +5,7 @@ import Layout from '@/hoc/Layout' import { NextPage } from 'next' import Image from 'next/image' import { useSearchParams } from 'next/navigation' -import { useEffect, useState } from 'react'; +import { useEffect, useState, Suspense } from 'react'; import { useGetCompare, useSearchCompareProduct } from './hooks/useCompareData' import { useLocalCart } from '@/app/product/hooks/useLocalCart' import { toast } from '@/components/Toast' @@ -13,7 +13,7 @@ import { CompareProduct } from './types/Types' import { useSharedStore } from '@/share/store/sharedStore' import DefaulModal from '@/components/DefaulModal' -const ComparePage: NextPage = () => { +const ComparePageContent: NextPage = () => { const [productIds, setProductIds] = useState([]); const [isCompareModalOpen, setIsCompareModalOpen] = useState(false); @@ -282,4 +282,23 @@ const ComparePage: NextPage = () => { ) } +const ComparePage: NextPage = () => { + return ( + +
+
+
+
+

در حال بارگذاری...

+
+
+
+ + }> + +
+ ) +} + export default ComparePage \ No newline at end of file diff --git a/src/share/components/Cart.tsx b/src/share/components/Cart.tsx index bd77459..4cd52b8 100644 --- a/src/share/components/Cart.tsx +++ b/src/share/components/Cart.tsx @@ -7,29 +7,60 @@ import { import { useGetCart } from '@/app/cart/hooks/useCartData' import { useRemoveFromCart } from '@/app/cart/hooks/useCartData' import { CartItemType } from '@/app/cart/types/Types' +import { LocalCartItem } from '@/app/product/store/LocalCartStore' + +// Union type for all possible cart items +type CartItem = CartItemType | EnrichedOfflineCartItem import { NumberFormat } from '@/config/func' import Image from 'next/image' import Link from 'next/link' import { Button } from '@/components/ui/button' +import { useSharedStore } from '@/share/store/sharedStore' +import { useLocalCart } from '@/app/product/hooks/useLocalCart' +import { useOfflineCartProducts, EnrichedOfflineCartItem } from '@/share/hooks/useOfflineCartProducts' const Cart = () => { + const { isLogin } = useSharedStore() const { data: cartData, isLoading } = useGetCart() const removeFromCartMutation = useRemoveFromCart() + // Local cart hooks for offline users + const { + localCartItems, + removeFromLocalCart, + getLocalCartItemsCount + } = useLocalCart() + + // Get enriched product data for offline cart items + const { + enrichedCartItems, + isLoading: isLoadingOfflineProducts + } = useOfflineCartProducts(localCartItems) + const handleRemoveItem = async (productId: number, variantId: string) => { - try { - await removeFromCartMutation.mutateAsync({ - productId, - variantId - }) - } catch (error) { - console.error('Error removing item from cart:', error) + if (isLogin) { + try { + await removeFromCartMutation.mutateAsync({ + productId, + variantId + }) + } catch (error) { + console.error('Error removing item from cart:', error) + } + } else { + removeFromLocalCart(productId, variantId) } } + // Get cart data based on login status const cart = cartData?.results?.cart - const items = cart?.items || [] - const itemsCount = cart?.items_count || 0 + const onlineItems = cart?.items || [] + const onlineItemsCount = cart?.items_count || 0 + + // Use appropriate data based on login status + const items = isLogin ? onlineItems : enrichedCartItems + const itemsCount = isLogin ? onlineItemsCount : getLocalCartItemsCount() + const isItemsLoading = isLogin ? isLoading : isLoadingOfflineProducts return ( @@ -56,14 +87,18 @@ const Cart = () => { {/* Cart Items */}
- {isLoading ? ( + {isItemsLoading ? (
در حال بارگذاری...
) : items.length > 0 ? ( - items.slice(0, 3).map((item: CartItemType) => ( + items.slice(0, 3).map((item: CartItem, index: number) => ( handleRemoveItem(item.product._id, item.variant._id)} + isOnline={isLogin} + onRemove={() => handleRemoveItem( + isLogin ? (item as CartItemType).product._id : (item as LocalCartItem).productId, + isLogin ? (item as CartItemType).variant._id : (item as LocalCartItem).variantId + )} /> )) ) : ( @@ -86,14 +121,46 @@ const Cart = () => { } // Cart Item Card Component -const CartItemCard = ({ item, onRemove }: { item: CartItemType, onRemove: () => void }) => { +const CartItemCard = ({ + item, + isOnline, + onRemove +}: { + item: CartItem, + isOnline: boolean, + onRemove: () => void +}) => { + // Handle both online and offline items with enriched data + const productTitle = isOnline + ? (item as CartItemType).product.title_fa + : (item as EnrichedOfflineCartItem).product?.title_fa || 'در حال بارگذاری...' + + const productImage = isOnline + ? (item as CartItemType).product.imagesUrl.cover + : (item as EnrichedOfflineCartItem).product?.imagesUrl?.cover || '/images/logo.png' + + const productCategory = isOnline + ? ((item as CartItemType).product.category?._id || 'دسته‌بندی') + : ((item as EnrichedOfflineCartItem).product?.category?._id || 'دسته‌بندی') + + const productPrice = isOnline + ? (item as CartItemType).variant.price.selling_price + : (item as EnrichedOfflineCartItem).variant?.price?.selling_price || 0 + + const quantity = isOnline + ? (item as CartItemType).quantity + : (item as LocalCartItem).quantity + + // Check if product data is still loading for offline items + const isProductLoading = !isOnline && !(item as EnrichedOfflineCartItem).product + return (
{/* Product Image */}
{item.product.title_fa} {/* Product Details */}
- {item.product.category?._id || 'دسته‌بندی'} + {productCategory}
- {item.product.title_fa} + {isProductLoading ? 'در حال بارگذاری...' : productTitle}
- {NumberFormat(item.variant.price.selling_price)} تومان + {isProductLoading ? ( + `${quantity} عدد` + ) : ( + `${NumberFormat(productPrice)} تومان` + )}