diff --git a/GrossesMitaines/grosses-mitaines-ui/src/components/Cart.js b/GrossesMitaines/grosses-mitaines-ui/src/components/Cart.js index 51cce21..e58db6e 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/components/Cart.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/components/Cart.js @@ -10,7 +10,8 @@ export const CartContext = createContext({ removeOneFromCart: () => { }, deleteFromCart: () => { }, getTotalCost: () => { }, - addToCart: () => { } + addToCart: () => { }, + emptyCart: () => { } }); export function CartProvider({ children }) { @@ -27,7 +28,7 @@ export function CartProvider({ children }) { }, []); useEffect(() => { - cookies.set('cart', cartProducts, { path: '/', SameSite: 'strict', secure: true, maxAge: 2592000 }) + cookies.set('cart', cartProducts, { path: '/', SameSite: 'strict', secure: true, maxAge: 2592000 }); }, [cartProducts]); @@ -101,6 +102,11 @@ export function CartProvider({ children }) { return totalCost; }; + function emptyCart() { + cookies.remove('cart', { path: '/', domain: "localhost"}); + setCartProducts([]); + } + const contextValue = { items: cartProducts, getProductQuantity, @@ -109,7 +115,8 @@ export function CartProvider({ children }) { removeOneFromCart, deleteFromCart, getTotalCost, - addToCart + addToCart, + emptyCart }; return ( diff --git a/GrossesMitaines/grosses-mitaines-ui/src/pages/ReviewInvoice.js b/GrossesMitaines/grosses-mitaines-ui/src/pages/ReviewInvoice.js index 2dbbefa..d832088 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/pages/ReviewInvoice.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/pages/ReviewInvoice.js @@ -6,7 +6,7 @@ import { Row, Col, Button } from "react-bootstrap"; import { useNavigate } from "react-router-dom"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; -import CartProvider from "../components/Cart"; +import { CartContext } from "../components/Cart"; const ReviewInvoice = () => { @@ -14,9 +14,9 @@ const ReviewInvoice = () => { const navigate = useNavigate(); const mySwal = withReactContent(Swal); - const cart = useContext(CartProvider); + const cart = useContext(CartContext); - //const [products, setProducts] = useState([]); + const [products, setProducts] = useState([]); const [thisInvoice, setThisInvoice] = useState({ firstName: "", @@ -55,7 +55,7 @@ const ReviewInvoice = () => { } }) - //var prods = []; + // var prods = []; // Object.keys(thisInvoice.prodQuant).map((k) => { // fetch(`https://localhost:7292/api/Product?id=${k}`) // .then(async (response) => { @@ -135,9 +135,12 @@ const ReviewInvoice = () => { title: 'Commande envoyée avec succès!', timer: 2000, icon: "success" - }) - cart.items = []; - navigate('./Morceaux'); + }).then( + () => { + cart.emptyCart(); + navigate('/morceaux'); + } + ) } else { console.log("Erreur de creation la commande #" + thisInvoice.id);