diff --git a/GrossesMitaines/grosses-mitaines-ui/src/components/Cart.js b/GrossesMitaines/grosses-mitaines-ui/src/components/Cart.js index 2c0fd78..c62a30e 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/components/Cart.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/components/Cart.js @@ -1,8 +1,10 @@ import { createContext, useState } from "react"; +import { useCookies } from "react-cookie"; export const CartContext = createContext({ items: [], getProductQuantity: () => { }, + getProductRemaining: () => { }, addOneToCart: () => { }, removeOneFromCart: () => { }, deleteFromCart: () => { }, @@ -12,11 +14,12 @@ export const CartContext = createContext({ export function CartProvider({ children }) { + const [cookies, setCookie] = useCookies(['cart']); const [cartProducts, setCartProducts] = useState([]); function addToCart(product, qty) { - setCartProducts([...cartProducts, {...product, quantity: parseFloat(qty)}]); + setCartProducts([...cartProducts, { ...product, quantity: parseFloat(qty), remaining: parseFloat(product.quantity) }]); } function getProductQuantity(id) { @@ -28,10 +31,20 @@ export function CartProvider({ children }) { return quantity; }; - function addOneToCart(id, qty) { - const currentQty = getProductQuantity(id); + function getProductRemaining(id) { + const remaining = cartProducts.find(product => product.id === id)?.remaining; + if (remaining === undefined) { + return 0; + } - if (qty > currentQty) { + return remaining; + }; + + function addOneToCart(id) { + const currentQty = getProductQuantity(id); + const remaining = getProductRemaining(id); + + if (remaining > currentQty) { setCartProducts( cartProducts.map( product => product.id === id @@ -46,7 +59,7 @@ export function CartProvider({ children }) { const currentQty = getProductQuantity(id); if (currentQty === 1) - deleteFromCart(); + deleteFromCart(id); else setCartProducts( cartProducts.map( @@ -68,7 +81,7 @@ export function CartProvider({ children }) { function getTotalCost() { let totalCost = 0; cartProducts.map((cartItem) => { - totalCost += (cartItem.Price * cartItem.quantity); + totalCost += (parseFloat(cartItem.price) * parseFloat(cartItem.quantity)); }); return totalCost; @@ -77,6 +90,7 @@ export function CartProvider({ children }) { const contextValue = { items: cartProducts, getProductQuantity, + getProductRemaining, addOneToCart, removeOneFromCart, deleteFromCart, diff --git a/GrossesMitaines/grosses-mitaines-ui/src/components/CartButton.js b/GrossesMitaines/grosses-mitaines-ui/src/components/CartButton.js index eb00b78..946ded2 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/components/CartButton.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/components/CartButton.js @@ -28,8 +28,11 @@ const CartButton = () => { {productsCount > 0 ? <> -

{cart.items.map((item) => )} +
+ Total: {cart.getTotalCost().toFixed(2)}$ CAD + +
:

C'est vide! Rempli le hi hi!

diff --git a/GrossesMitaines/grosses-mitaines-ui/src/components/CartCard.js b/GrossesMitaines/grosses-mitaines-ui/src/components/CartCard.js index 7b4b891..b5639f1 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/components/CartCard.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/components/CartCard.js @@ -24,12 +24,12 @@ export function CartCard({ product }) {
- {product.Title} - {product.Price}$ CA + {product.title} + {product.price}$ CA
Dans l'carosse: {productQuantity} - +
diff --git a/GrossesMitaines/grosses-mitaines-ui/src/pages/MorceauDetail.js b/GrossesMitaines/grosses-mitaines-ui/src/pages/MorceauDetail.js index d29bb08..25aeaff 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/pages/MorceauDetail.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/pages/MorceauDetail.js @@ -149,7 +149,7 @@ const MorceauDetail = () => {
Dans l'carosse: {inCartQuantity} diff --git a/GrossesMitaines/grosses-mitaines-ui/src/stylesheets/site.css b/GrossesMitaines/grosses-mitaines-ui/src/stylesheets/site.css index 4833e23..e415c08 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/stylesheets/site.css +++ b/GrossesMitaines/grosses-mitaines-ui/src/stylesheets/site.css @@ -668,8 +668,22 @@ a { text-decoration: line-through; } +.total { + margin: 5%; + color: green; + font-weight: bold; +} +.payer { + display: flex; + margin: 5%; + margin-left: auto; +} +.carosse { + border-color: purple; + border-width: 2%; +} .btn-fermer { background-color: red !important;