Skip to content

Commit

Permalink
Add add to cart functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVanDerSchans committed Apr 7, 2024
1 parent 3cce471 commit bbf4a13
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
32 changes: 16 additions & 16 deletions src/components/Icons/Icons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ export function AddToCartIcon() {
return (
<svg
xmlns='http://www.w3.org/2000/svg'
width='24'
height='24'
width='30'
height='30'
viewBox='0 0 24 24'
strokeWidth='1'
stroke='currentColor'
strokeWidth='1.5'
stroke='#fff'
fill='none'
strokeLinecap='round'
strokeLinejoin='round'
Expand All @@ -25,11 +25,11 @@ export function RemoveFromCartIcon() {
return (
<svg
xmlns='http://www.w3.org/2000/svg'
width='24'
height='24'
width='30'
height='30'
viewBox='0 0 24 24'
strokeWidth='1'
stroke='currentColor'
strokeWidth='1.5'
stroke='#fff'
fill='none'
strokeLinecap='round'
strokeLinejoin='round'
Expand All @@ -49,11 +49,11 @@ export function ClearCartIcon() {
return (
<svg
xmlns='http://www.w3.org/2000/svg'
width='24'
height='24'
width='30'
height='30'
viewBox='0 0 24 24'
strokeWidth='1'
stroke='currentColor'
strokeWidth='1.5'
stroke='#fff'
fill='none'
strokeLinecap='round'
strokeLinejoin='round'
Expand All @@ -72,11 +72,11 @@ export function CartIcon() {
return (
<svg
xmlns='http://www.w3.org/2000/svg'
width='24'
height='24'
width='30'
height='30'
viewBox='0 0 24 24'
strokeWidth='1'
stroke='currentColor'
strokeWidth='1.5'
stroke='#fff'
fill='none'
strokeLinecap='round'
strokeLinejoin='round'
Expand Down
38 changes: 23 additions & 15 deletions src/components/Products/Products.jsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
import { useCart } from '../../hooks/useCart'
import { AddToCartIcon } from '../Icons/Icons'
import { AddToCartIcon, RemoveFromCartIcon } from '../Icons/Icons'
import './Products.css'

export function Products({ products }) {
const { addToCart } = useCart()
const { addToCart, cart } = useCart()

const checkProductInCart = (product) => {
return cart.some((item) => item.id === product.id)
}

return (
<main className='products'>
<ul>
{products.map((product) => (
<li key={product.id}>
<img src={product.thumbnail} alt={product.title} />
{products.map((product) => {
const isProductInCart = checkProductInCart(product)

return (
<li key={product.id}>
<img src={product.thumbnail} alt={product.title} />

<div>
<strong>{product.title}</strong> - {product.price}
</div>
<div>
<strong>{product.title}</strong> - {product.price}
</div>

<div>
<button onClick={() => addToCart(product)}>
<AddToCartIcon />
</button>
</div>
</li>
))}
<div>
<button onClick={() => addToCart(product)}>
{isProductInCart ? <RemoveFromCartIcon /> : <AddToCartIcon />}
</button>
</div>
</li>
)
})}
</ul>
</main>
)
Expand Down

0 comments on commit bbf4a13

Please sign in to comment.