Skip to content

Commit

Permalink
✨ Agregando productos al carrito
Browse files Browse the repository at this point in the history
  • Loading branch information
Klauditha committed Nov 26, 2023
1 parent e60d3b7 commit e08bf34
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
23 changes: 16 additions & 7 deletions src/Components/Card/Card.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,36 @@ import { useContext } from "react";
import { ShoppingCartContext } from "../../Context/Context";

const Card = (data) => {

const context = useContext(ShoppingCartContext);

const showProduct = (productDetail) => {
context.openProductDetails();
context.setProductToShow(productDetail);
}
};

const addProductsToCart = (productData) => {
context.setCount(context.count + 1);
context.setCartProducts([...context.cartProducts, productData]);
console.log('CART: ', context.cartProducts);
};
return (
<div className="bg-white cursor-pointer w-56 h-60 rounded-lg" onClick={() => showProduct(data.data)}>
<div
className="bg-white cursor-pointer w-56 h-60 rounded-lg"
onClick={() => showProduct(data.data)}
>
<figure className="relative mb-2 w-full h-4/5">
<span className="absolute bottom-0 left-0 bg-white/60 rounded-lg text-black text-xm m-2 px-3 py-0.5">
{data.data.category.name}
</span>
<img
className="w-full h-full object-cover rounded-lg"
src={data.data.images[0] }
src={data.data.images[0]}
alt={data.data.title}
/>
<div className="absolute top-0 right-0 flex justify-center items-center bg-white w-6 h-6 rounded-full m-2 p-1"
onClick={() => context.setCount(context.count+1)}>
<div
className="absolute top-0 right-0 flex justify-center items-center bg-white w-6 h-6 rounded-full m-2 p-1"
onClick={() => addProductsToCart(data.data)}
>
<PlusIcon className="h-6 w-6 text-black" />
</div>
</figure>
Expand All @@ -32,7 +41,7 @@ const Card = (data) => {
<span className="text-lg font-medium">$ {data.data.price}</span>
</p>
</div>
)
);
};

export default Card;
2 changes: 1 addition & 1 deletion src/Components/ProductDetail/ProductDetail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const ProductDetail = () => {
<figure className="px-6">
<img
className="w-full h-full rounded-lg"
src={context.productToShow.images[0]}
src={context.productToShow.images}
alt={context.productToShow.title}
/>
</figure>
Expand Down
5 changes: 5 additions & 0 deletions src/Context/Context.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export const ShoppingCartProvider = ({ children }) => {

//Product detail: show product details
const [productToShow, setProductToShow] = useState({});

// Shopping cart - Add product
const [cartProducts, setCartProducts] = useState([]);
return (
<ShoppingCartContext.Provider
value={{
Expand All @@ -24,6 +27,8 @@ export const ShoppingCartProvider = ({ children }) => {
isProductDetailsOpen,
productToShow,
setProductToShow,
cartProducts,
setCartProducts,
}}
>
{children}
Expand Down

0 comments on commit e08bf34

Please sign in to comment.