privacy + return policy
This commit is contained in:
@@ -4,7 +4,7 @@ import {
|
||||
PopoverContent,
|
||||
PopoverTrigger,
|
||||
} from "@/components/ui/popover"
|
||||
import { useGetCart } from '@/app/cart/hooks/useCartData'
|
||||
import { useGetCart, useUpdateCart } 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'
|
||||
@@ -18,35 +18,64 @@ 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'
|
||||
import { useEffect, useRef } from 'react'
|
||||
|
||||
const Cart = () => {
|
||||
const { isLogin } = useSharedStore()
|
||||
const { data: cartData, isLoading } = useGetCart()
|
||||
const removeFromCartMutation = useRemoveFromCart()
|
||||
const updateCartMutation = useUpdateCart()
|
||||
|
||||
// Local cart hooks for offline users
|
||||
const {
|
||||
localCartItems,
|
||||
removeFromLocalCart,
|
||||
getLocalCartItemsCount
|
||||
getLocalCartItemsCount,
|
||||
clearLocalCart
|
||||
} = useLocalCart()
|
||||
|
||||
// Track previous login state to detect login events
|
||||
const prevIsLoginRef = useRef(isLogin)
|
||||
|
||||
// Get enriched product data for offline cart items
|
||||
const {
|
||||
enrichedCartItems,
|
||||
isLoading: isLoadingOfflineProducts
|
||||
} = useOfflineCartProducts(localCartItems)
|
||||
|
||||
// Sync local cart to server when user logs in
|
||||
useEffect(() => {
|
||||
const prevIsLogin = prevIsLoginRef.current
|
||||
prevIsLoginRef.current = isLogin
|
||||
|
||||
// Check if user just logged in and has local cart items
|
||||
if (!prevIsLogin && isLogin && localCartItems.length > 0) {
|
||||
// Add each local cart item to server cart
|
||||
const syncPromises = localCartItems.map(async (item) => {
|
||||
try {
|
||||
await updateCartMutation.mutateAsync({
|
||||
productId: item.productId,
|
||||
variantId: item.variantId,
|
||||
quantity: item.quantity
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Failed to sync cart item:', item, error)
|
||||
}
|
||||
})
|
||||
|
||||
// Clear local cart after sync attempt
|
||||
Promise.all(syncPromises).finally(() => {
|
||||
clearLocalCart()
|
||||
})
|
||||
}
|
||||
}, [isLogin, localCartItems, updateCartMutation, clearLocalCart])
|
||||
|
||||
const handleRemoveItem = async (productId: number, variantId: string) => {
|
||||
if (isLogin) {
|
||||
try {
|
||||
await removeFromCartMutation.mutateAsync({
|
||||
productId,
|
||||
variantId
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error removing item from cart:', error)
|
||||
}
|
||||
await removeFromCartMutation.mutateAsync({
|
||||
productId,
|
||||
variantId
|
||||
})
|
||||
} else {
|
||||
removeFromLocalCart(productId, variantId)
|
||||
}
|
||||
@@ -102,7 +131,16 @@ const Cart = () => {
|
||||
/>
|
||||
))
|
||||
) : (
|
||||
<div className='text-center py-4 text-gray-500'>سبد خرید شما خالی است</div>
|
||||
<div className='text-center py-4'>
|
||||
<div className='text-gray-500 mb-2'>
|
||||
{!isLogin ? 'سبد خرید شما خالی است' : 'سبد خرید شما خالی است'}
|
||||
</div>
|
||||
{!isLogin && (
|
||||
<div className='text-xs text-gray-400'>
|
||||
آیتمهای اضافه شده به سبد خرید در مرورگر شما ذخیره میشوند
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -114,6 +152,20 @@ const Cart = () => {
|
||||
</Button>
|
||||
</Link>
|
||||
)}
|
||||
|
||||
{/* Login encouragement message for offline users */}
|
||||
{!isLogin && (
|
||||
<div className='mt-4 p-3 bg-blue-50 border border-blue-200 rounded-lg text-center'>
|
||||
<div className='text-xs text-blue-700 mb-2'>
|
||||
برای ذخیره سبد خرید و همگامسازی در همه دستگاهها، وارد حساب کاربری شوید
|
||||
</div>
|
||||
<Link href="/auth" className='inline-block'>
|
||||
<Button size="sm" variant="outline" className='text-blue-600 border-blue-300 hover:bg-blue-100'>
|
||||
ورود به حساب
|
||||
</Button>
|
||||
</Link>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
@@ -133,7 +185,7 @@ const CartItemCard = ({
|
||||
// Handle both online and offline items with enriched data
|
||||
const productTitle = isOnline
|
||||
? (item as CartItemType).product.title_fa
|
||||
: (item as EnrichedOfflineCartItem).product?.title_fa || 'در حال بارگذاری...'
|
||||
: (item as EnrichedOfflineCartItem).product?.title_fa || 'در حال بارگذاری محصول...'
|
||||
|
||||
const productImage = isOnline
|
||||
? (item as CartItemType).product.imagesUrl.cover
|
||||
@@ -153,6 +205,7 @@ const CartItemCard = ({
|
||||
|
||||
// Check if product data is still loading for offline items
|
||||
const isProductLoading = !isOnline && !(item as EnrichedOfflineCartItem).product
|
||||
const hasError = !isOnline && !isProductLoading && !(item as EnrichedOfflineCartItem).variant
|
||||
|
||||
return (
|
||||
<div className='bg-white rounded-lg p-3 flex gap-3 shadow-sm border border-gray-100'>
|
||||
@@ -173,12 +226,14 @@ const CartItemCard = ({
|
||||
{productCategory}
|
||||
</div>
|
||||
<div className='text-sm text-gray-800 mb-2 line-clamp-2'>
|
||||
{isProductLoading ? 'در حال بارگذاری...' : productTitle}
|
||||
{hasError ? 'خطا در بارگذاری محصول' : isProductLoading ? 'در حال بارگذاری محصول...' : productTitle}
|
||||
</div>
|
||||
<div className='flex items-center justify-between'>
|
||||
<div className='text-sm font-semibold text-gray-900'>
|
||||
{isProductLoading ? (
|
||||
{hasError ? (
|
||||
`${quantity} عدد`
|
||||
) : isProductLoading ? (
|
||||
'در حال بارگذاری...'
|
||||
) : (
|
||||
`${NumberFormat(productPrice)} تومان`
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user