Skip to content

Commit 2934ac6

Browse files
committed
Add Cart Qty To Bag
1 parent 3cadff4 commit 2934ac6

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

pages/cart.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const Cart = () => {
2+
return <h1>Cart</h1>;
3+
}
4+
5+
export default Cart

src/components/layout/header/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import Head from 'next/head';
22
import Link from 'next/link';
3-
import { useState } from 'react';
3+
import { useContext, useState } from 'react';
44
import { isEmpty } from 'lodash';
55

66
import { BurgerIcon, TailwindIcon, Bag, User, Wishlist } from '../../icons';
7+
import { AppContext } from '../../context';
78

89
const Header = ( { header } ) => {
910

11+
const [ cart, setCart ] = useContext( AppContext );
1012
const { headerMenuItems, siteDescription, siteLogoUrl, siteTitle, favicon } = header || {};
1113

1214
const [ isMenuVisible, setMenuVisibility ] = useState( false );
@@ -41,12 +43,13 @@ const Header = ( { header } ) => {
4143
</div>
4244
<div className="block lg:hidden">
4345
<button
44-
onClick={() => setMenuVisibility( ! isMenuVisible )}
46+
onClick={ () => setMenuVisibility( ! isMenuVisible ) }
4547
className="flex items-center px-3 py-2 border rounded text-black border-black hover:text-black hover:border-black">
4648
<BurgerIcon className="fill-current h-3 w-3"/>
4749
</button>
4850
</div>
49-
<div className={`${ isMenuVisible ? 'max-h-full' : 'h-0' } overflow-hidden w-full lg:h-full block flex-grow lg:flex lg:items-center lg:w-auto`}>
51+
<div
52+
className={ `${ isMenuVisible ? 'max-h-full' : 'h-0' } overflow-hidden w-full lg:h-full block flex-grow lg:flex lg:items-center lg:w-auto` }>
5053
<div className="text-sm font-medium uppercase lg:flex-grow">
5154
{ ! isEmpty( headerMenuItems ) && headerMenuItems.length ? headerMenuItems.map( menuItem => (
5255
<Link key={ menuItem?.ID } href={ menuItem?.url ?? '/' }>
@@ -70,13 +73,14 @@ const Header = ( { header } ) => {
7073
Wishlist
7174
</span>
7275
</a>
73-
<a className="flex mt-4 lg:inline-block lg:mt-0 text-black hover:text-black mr-10"
74-
href="/cart/">
76+
<Link href="/cart">
77+
<a className="flex mt-4 lg:inline-block lg:mt-0 text-black hover:text-black mr-10">
7578
<span className="flex flex-row items-center lg:flex-col">
7679
<Bag className="mr-1 lg:mr-0"/>
77-
Bag
80+
<span className="ml-1">Bag{ cart?.totalQty ? `(${cart?.totalQty})` : null }</span>
7881
</span>
79-
</a>
82+
</a>
83+
</Link>
8084
</div>
8185
</div>
8286
</div>

src/utils/cart/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const getFormattedCartData = ( cartData ) => {
6969
const cartTotal = calculateCartQtyAndPrice( cartData || [] );
7070
return {
7171
cartItems: cartData || [],
72-
cartTotal,
72+
...cartTotal,
7373
};
7474
};
7575

0 commit comments

Comments
 (0)