From 816b27828305d44e4812e5313a484b4be6415945 Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Sat, 27 Jun 2026 16:09:32 +0330 Subject: [PATCH] badge cart --- src/components/navigation/BottomNavBar.tsx | 27 +++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/components/navigation/BottomNavBar.tsx b/src/components/navigation/BottomNavBar.tsx index 199faa4..6f703d7 100644 --- a/src/components/navigation/BottomNavBar.tsx +++ b/src/components/navigation/BottomNavBar.tsx @@ -1,12 +1,15 @@ "use client"; +import { useGetCartItems } from "@/app/[name]/(Main)/cart/hooks/useCartData"; import { useGetProfile } from "@/app/[name]/(Profile)/profile/hooks/userProfileData"; import { glassSurfaceNav } from "@/lib/styles/glassSurface"; +import { useReceiptStore } from "@/zustand/receiptStore"; import clsx from "clsx"; import type { Icon } from "iconsax-react"; import { Bag2, Heart, Home2, InfoCircle, Reserve } from "iconsax-react"; import Link from "next/link"; import { useParams, usePathname, useRouter } from "next/navigation"; +import { useMemo } from "react"; import { toastLoginRequired } from "../Toast"; type BottomNavBarProps = { @@ -19,12 +22,22 @@ type NavItemProps = { onClick?: (e: React.MouseEvent) => void; onButtonClick?: () => void; Icon: Icon; + badge?: number; }; -function NavItem({ href, isActive, onClick, onButtonClick, Icon }: NavItemProps) { +function NavItem({ href, isActive, onClick, onButtonClick, Icon, badge }: NavItemProps) { const className = clsx("flex-1 flex items-center justify-center h-full rounded-full", isActive && "bg-primary/12"); - const icon = ; + const icon = ( + + + {badge != null && badge > 0 && ( + + {badge > 99 ? "99+" : badge} + + )} + + ); if (onButtonClick) { return ( @@ -47,6 +60,14 @@ function BottomNavBar({ onPagerClick }: BottomNavBarProps) { const pathname = usePathname(); const router = useRouter(); const { isSuccess } = useGetProfile(); + const { data: cartData } = useGetCartItems(isSuccess); + const localItems = useReceiptStore((state) => state.items); + + const guestItemCount = useMemo( + () => Object.values(localItems).reduce((sum, item) => sum + (item?.quantity ?? 0), 0), + [localItems], + ); + const cartItemCount = isSuccess ? (cartData?.data?.totalItems ?? 0) : guestItemCount; const isHomeActive = pathname === `/${name}`; const isFavoriteActive = pathname.startsWith(`/${name}/favorite`); @@ -70,7 +91,7 @@ function BottomNavBar({ onPagerClick }: BottomNavBarProps) { {onPagerClick ? : } - + ); }