From dbb43e47744986ffe6cb9b8acf0b353973557414 Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Sat, 21 Feb 2026 09:34:12 +0330 Subject: [PATCH] stock --- src/app/product/components/Cart.tsx | 10 ++++-- src/app/product/components/OfflineCart.tsx | 27 ++++++++++++---- src/app/product/components/OnlineCart.tsx | 32 ++++++++++++------- .../product/components/ShopInformation.tsx | 8 ++--- 4 files changed, 52 insertions(+), 25 deletions(-) diff --git a/src/app/product/components/Cart.tsx b/src/app/product/components/Cart.tsx index 209925a..97b98a7 100644 --- a/src/app/product/components/Cart.tsx +++ b/src/app/product/components/Cart.tsx @@ -4,16 +4,20 @@ import { useSharedStore } from '@/share/store/sharedStore' import OnlineCart from './OnlineCart' import OfflineCart from './OfflineCart' -const Cart = () => { +interface CartProps { + stock: number +} + +const Cart = ({ stock }: CartProps) => { const { variantId } = useProductStore() const { isLogin } = useSharedStore() return (
{isLogin ? ( - + ) : ( - + )}
) diff --git a/src/app/product/components/OfflineCart.tsx b/src/app/product/components/OfflineCart.tsx index 52c3d37..5c078b0 100644 --- a/src/app/product/components/OfflineCart.tsx +++ b/src/app/product/components/OfflineCart.tsx @@ -8,9 +8,10 @@ import { useLocalCart } from '../hooks/useLocalCart' interface OfflineCartProps { variantId: string + stock: number } -const OfflineCart: React.FC = ({ variantId }) => { +const OfflineCart: React.FC = ({ variantId, stock }) => { const { id } = useParams() // Local cart hooks for non-logged in users @@ -24,7 +25,11 @@ const OfflineCart: React.FC = ({ variantId }) => { const [isInCart, setIsInCart] = useState(false) const [quantity, setQuantity] = useState(1) + const isOutOfStock = stock <= 0 + const handleAddToCart = () => { + if (isOutOfStock) return + addToLocalCart(Number(id), variantId, 1) setIsInCart(true) setQuantity(1) @@ -48,8 +53,9 @@ const OfflineCart: React.FC = ({ variantId }) => { return } - setQuantity(number) - updateLocalCartItem(Number(id), variantId, number) + const clampedQuantity = Math.min(number, stock) + setQuantity(clampedQuantity) + updateLocalCartItem(Number(id), variantId, clampedQuantity) } const handleEmptyCart = () => { @@ -59,21 +65,30 @@ const OfflineCart: React.FC = ({ variantId }) => { setQuantity(0) } + if (isOutOfStock) { + return ( + + ) + } + return (
{ !isInCart ? - : -
+
diff --git a/src/app/product/components/OnlineCart.tsx b/src/app/product/components/OnlineCart.tsx index 42fc11f..60c529c 100644 --- a/src/app/product/components/OnlineCart.tsx +++ b/src/app/product/components/OnlineCart.tsx @@ -9,9 +9,10 @@ import { extractErrorMessage } from '@/helpers/errorUtils' interface OnlineCartProps { variantId: string + stock: number } -const OnlineCart: React.FC = ({ variantId }) => { +const OnlineCart: React.FC = ({ variantId, stock }) => { const { id } = useParams() // API hooks for logged in users @@ -23,7 +24,11 @@ const OnlineCart: React.FC = ({ variantId }) => { const [isInCart, setIsInCart] = useState(false) const [quantity, setQuantity] = useState(1) + const isOutOfStock = stock <= 0 + const handleAddToCart = () => { + if (isOutOfStock) return + addToCart({ productId: Number(id), variantId: variantId, @@ -40,14 +45,8 @@ const OnlineCart: React.FC = ({ variantId }) => { } useEffect(() => { - // Check API cart for logged in users - console.log('cart', cart); - console.log('variantId', variantId); - const item = cart?.results?.cart?.items?.find(item => item.variant?._id === variantId) - console.log('item', item); - if (item) { setIsInCart(true) setQuantity(item.quantity) @@ -62,13 +61,14 @@ const OnlineCart: React.FC = ({ variantId }) => { return } + const clampedQuantity = Math.min(number, stock) const oldQuantity = quantity - setQuantity(number) + setQuantity(clampedQuantity) updateCart({ productId: Number(id), variantId: variantId, - quantity: number, + quantity: clampedQuantity, }, { onError: (error: Error) => { toast(extractErrorMessage(error), 'error') @@ -93,22 +93,30 @@ const OnlineCart: React.FC = ({ variantId }) => { }) } + if (isOutOfStock) { + return ( + + ) + } + return (
{ !isInCart && !isPending ? - : -
+
diff --git a/src/app/product/components/ShopInformation.tsx b/src/app/product/components/ShopInformation.tsx index 6ec2426..c11aa22 100644 --- a/src/app/product/components/ShopInformation.tsx +++ b/src/app/product/components/ShopInformation.tsx @@ -1,6 +1,6 @@ 'use client' import { Separator } from '@/components/ui/separator' -import { BoxTick, I3DRotate } from 'iconsax-react' +import { I3DRotate } from 'iconsax-react' import React from 'react' import { Product } from '@/types/product.types' import Image from 'next/image' @@ -20,10 +20,10 @@ const ShopInformation = ({ product }: ShopInformationProps) => { ? product.variants.find((variant) => variant._id === variantId) || product.default_variant : product.default_variant - const shop = selectedVariant.shop + // const shop = selectedVariant.shop const price = selectedVariant.price const warranty = selectedVariant.warranty - const postingTime = selectedVariant.postingTime + // const postingTime = selectedVariant.postingTime const formatPrice = (price: number) => { return price.toLocaleString('fa-IR') @@ -107,7 +107,7 @@ const ShopInformation = ({ product }: ShopInformationProps) => {
- + {/*