Skip to content

Commit

Permalink
feat: added cart items quantity to cart icon
Browse files Browse the repository at this point in the history
  • Loading branch information
altomizawa committed Jun 3, 2024
1 parent eefc3a5 commit 81f5bc7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
16 changes: 16 additions & 0 deletions Frontend/src/components/Header.css
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@
color: white;
}

.header__cart-quantity{
color: white;
position: absolute;
font-weight: bold;
font-size: 12px;
bottom: -5px;
right:-5px;
background-color: black;
width: 20px;
height: 20px;
border-radius: 1000px;
display: flex;
justify-content: center;
align-items: center;
}

@media screen and (max-width: 880px) {
.header__hamburguer {
display: flex;
Expand Down
14 changes: 12 additions & 2 deletions Frontend/src/components/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ function Header() {
const handleLogin = async () => {
try {
const data = await checkToken(token);
console.log(data);
login(data);
} catch (err) {
console.log(err);
Expand All @@ -58,6 +57,7 @@ function Header() {
};

const toggleCart = () => {
console.log(calculateTotalItems(cartItems))
setIsCartOpen((prevState) => !prevState);
};

Expand All @@ -68,6 +68,15 @@ function Header() {
navigate('/');
};

// CALCULATE TOTAL ITEMS IN CART
function calculateTotalItems(cartItems) {
let totalItems = 0;
for (const item of cartItems) {
totalItems += item.quantity;
}
return totalItems;
}

return (
<div className='header'>
<div className='header__top'>
Expand All @@ -82,12 +91,13 @@ function Header() {
{!isLoginPage && !isRegisterPage && isLoggedIn && (
<>
{' '}
<button onClick={toggleCart}>
<button onClick={toggleCart} style={{position: 'relative'}}>
<img
src='/assets/shopping_bag.svg'
alt='account icon'
className='account-icon'
/>
{calculateTotalItems(cartItems) !==0 && <div className='header__cart-quantity'><p>{calculateTotalItems(cartItems)}</p></div>}
</button>
<button
className='header__user-button'
Expand Down

0 comments on commit 81f5bc7

Please sign in to comment.