From 809ce36a1ebb0d91bddcd46674268daef80a7f40 Mon Sep 17 00:00:00 2001 From: DavidBelisle <79233327+DavidBelisle@users.noreply.github.com> Date: Tue, 8 Nov 2022 00:34:01 -0500 Subject: [PATCH] no cookies or api call... --- .../src/components/Cart.js | 26 ++++++++--- .../src/components/CartButton.js | 5 +- .../src/components/CartCard.js | 6 +-- .../src/pages/MorceauDetail.js | 2 +- .../src/stylesheets/site.css | 46 ++++++++++++------- 5 files changed, 57 insertions(+), 28 deletions(-) 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 0e96b11..0be537f 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/stylesheets/site.css +++ b/GrossesMitaines/grosses-mitaines-ui/src/stylesheets/site.css @@ -599,9 +599,7 @@ a { margin-top: 25px; } -.detail-status-available { - -} +.detail-status-available {} .detail-status-backorder {} @@ -621,34 +619,34 @@ a { } .detail-container-image { - margin:auto; - margin-top:25px; + margin: auto; + margin-top: 25px; width: 35%; height: 100%; } .detail-container-info { - margin:auto; + margin: auto; width: 35%; height: 100%; } .detail-container-controls { - margin:auto; - display:inline-flex; - width:20%; - height:100%; + margin: auto; + display: inline-flex; + width: 20%; + height: 100%; } .qty-select { width: fit-content; - height:fit-content; - margin:auto; + height: fit-content; + margin: auto; } -.add-to-cart{ - width:auto; +.add-to-cart { + width: auto; margin-left: 10px; } @@ -657,7 +655,7 @@ a { height: auto; } -.detail-price{ +.detail-price { font-weight: bold; } @@ -670,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; @@ -726,11 +738,11 @@ a { } -.invoice-list-container{ +.invoice-list-container { border: beige 2px double; } -.invoice-item-container{ +.invoice-item-container { border: black 1px solid; }