emptymycart

This commit is contained in:
DavidBelisle 2022-11-23 15:41:49 -05:00
parent 6120bf544a
commit a33c04b71f
2 changed files with 20 additions and 10 deletions

View File

@ -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 (

View File

@ -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);