Skip to content

Commit 848c133

Browse files
committed
Configured logic to show user how many items are in the cart
1 parent c29c41d commit 848c133

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

specialtea/src/components/UI/Buttons/CartButton/CartButton.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1-
import CartIcon from '../../../Cart/CartIcon/CartIcon';
1+
import { useContext } from 'react';
22

33
import classes from './CartButton.module.css';
44

5+
import CartIcon from '../../../Cart/CartIcon/CartIcon';
6+
import CartContext from '../../../../store/CartContext/cart-context';
7+
58
const CartButton = props => {
9+
const context = useContext(CartContext);
10+
11+
const numOfCartItems = context.items.reduce((current, item) => {
12+
return current + item.amount;
13+
}, 0);
14+
615
return (
716
<button className={classes.button} onClick={props.onShowCart}>
817
<span className={classes.icon}>
918
<CartIcon />
1019
</span>
1120
<span>Cart</span>
12-
<span className={classes.badge}>3</span>
21+
<span className={classes.badge}>{numOfCartItems}</span>
1322
</button>
1423
);
1524
};

0 commit comments

Comments
 (0)