Skip to content

Commit

Permalink
✨ Suma total carrito
Browse files Browse the repository at this point in the history
  • Loading branch information
Klauditha committed Nov 29, 2023
1 parent 2aabec2 commit 6b8a701
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Components/CheckoutSideMenu/CheckoutSideMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import "./CheckoutSideMenu.css";
import { useContext } from "react";
import { ShoppingCartContext } from "../../Context/Context";
import OrderCard from "../OrderCard/OrderCard";
import { totalPrice } from "../../utils/Utils";

const CheckoutSideMenu = () => {
const context = useContext(ShoppingCartContext);

const handleDelete = (id) => {
const filteredProducts = context.cartProducts.filter(product => product.id !== id);
const filteredProducts = context.cartProducts.filter(
(product) => product.id !== id
);
context.setCartProducts(filteredProducts);
}
};
return (
<aside
className={`${
Expand Down Expand Up @@ -38,6 +41,12 @@ const CheckoutSideMenu = () => {
/>
))}
</div>
<div className="px-6">
<p className="flex justify-between items-center">
<span className="font-light">Total:</span>
<span className="font-medium text-2xl">${totalPrice(context.cartProducts)}</span>
</p>
</div>
</aside>
);
};
Expand Down
10 changes: 10 additions & 0 deletions src/utils/Utils.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* This function calculates total price of a new order
* @param {Array} products cartProduct: Array of Objects
* @returns {number} Total price
*/
export const totalPrice = (products) => {
let sum = 0;
products.forEach((product) => (sum += product.price));
return sum;
};

0 comments on commit 6b8a701

Please sign in to comment.