diff --git a/src/pages/home/components/Header/Header.tsx b/src/pages/home/components/Header/Header.tsx index 8234030..4fd4b46 100644 --- a/src/pages/home/components/Header/Header.tsx +++ b/src/pages/home/components/Header/Header.tsx @@ -6,6 +6,7 @@ import logo from '../../../../assets/img/logo.png' import Search from 'antd/es/input/Search' import { useSelector } from 'react-redux' import type { StoreType } from '../../../../stores' +import type { CartItem } from '../../../../types/cart.type' export default function Header() { const navigate = useNavigate() const menu = [ @@ -28,6 +29,12 @@ export default function Header() { const userStore = useSelector((store: StoreType) => { return store.user }) + + function countCartItem() { + return userStore.cart?.reduce((cur: number, next: CartItem) => { + return cur + next.quantity + }, 0) + } return (
@@ -52,9 +59,11 @@ export default function Header() { ) }) } -
+
{ + window.location.href = "/cart" + }} className='item'> -

0

+

{countCartItem()}

Giỏ

Hàng

diff --git a/src/stores/slices/user.slice.ts b/src/stores/slices/user.slice.ts index cf0df4c..c9f0bbc 100644 --- a/src/stores/slices/user.slice.ts +++ b/src/stores/slices/user.slice.ts @@ -7,7 +7,7 @@ import type { CartItem } from "../../types/cart.type"; interface UserState { data: User | null, loading: boolean; - cart: CartItem[] | [] + cart: CartItem[] } const InitUserState: UserState = {