This commit is contained in:
Tyrel Souza 2022-09-26 13:45:11 -04:00
parent 5f88a979a7
commit 2c39f2b8d8
No known key found for this signature in database
GPG Key ID: F3614B02ACBE438E

View File

@ -1,18 +1,39 @@
import { useContext } from "react" import { useContext, useEffect, useState } from "react"
import CartIcon from "../Cart/CartIcon" import CartIcon from "../Cart/CartIcon"
import classes from "./HeaderCartButton.module.css" import classes from "./HeaderCartButton.module.css"
import CartContext from "../../store/cart-context" import CartContext from "../../store/cart-context"
function HeaderCartButton(props) { function HeaderCartButton(props) {
const [btnIsHighlighted, setBtnIsHighlighted] = useState(false)
const cartCtx = useContext(CartContext) const cartCtx = useContext(CartContext)
const { items } = cartCtx
const numberOfCartItems = cartCtx.items.reduce((curNumber, item) => { const numberOfCartItems = items.reduce((curNumber, item) => {
return curNumber + item.amount return curNumber + item.amount
}, 0) }, 0)
const btnClasses = `${classes.button} ${
btnIsHighlighted ? classes.bump : ""
}`
useEffect(() => {
if (items.length === 0) {
return
}
setBtnIsHighlighted(true)
const timer = setTimeout(() => {
setBtnIsHighlighted(false)
}, 300)
return () => {
clearTimeout(timer)
}
}, [items])
return ( return (
<button className={classes.button} onClick={props.onClick}> <button className={btnClasses} onClick={props.onClick}>
<span className={classes.icon}> <span className={classes.icon}>
<CartIcon /> <CartIcon />
</span> </span>