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: () => { }, removeOneFromCart: () => { },
deleteFromCart: () => { }, deleteFromCart: () => { },
getTotalCost: () => { }, getTotalCost: () => { },
addToCart: () => { } addToCart: () => { },
emptyCart: () => { }
}); });
export function CartProvider({ children }) { export function CartProvider({ children }) {
@ -27,7 +28,7 @@ export function CartProvider({ children }) {
}, []); }, []);
useEffect(() => { useEffect(() => {
cookies.set('cart', cartProducts, { path: '/', SameSite: 'strict', secure: true, maxAge: 2592000 }) cookies.set('cart', cartProducts, { path: '/', SameSite: 'strict', secure: true, maxAge: 2592000 });
}, [cartProducts]); }, [cartProducts]);
@ -101,6 +102,11 @@ export function CartProvider({ children }) {
return totalCost; return totalCost;
}; };
function emptyCart() {
cookies.remove('cart', { path: '/', domain: "localhost"});
setCartProducts([]);
}
const contextValue = { const contextValue = {
items: cartProducts, items: cartProducts,
getProductQuantity, getProductQuantity,
@ -109,7 +115,8 @@ export function CartProvider({ children }) {
removeOneFromCart, removeOneFromCart,
deleteFromCart, deleteFromCart,
getTotalCost, getTotalCost,
addToCart addToCart,
emptyCart
}; };
return ( return (

View File

@ -6,7 +6,7 @@ import { Row, Col, Button } from "react-bootstrap";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import Swal from "sweetalert2"; import Swal from "sweetalert2";
import withReactContent from "sweetalert2-react-content"; import withReactContent from "sweetalert2-react-content";
import CartProvider from "../components/Cart"; import { CartContext } from "../components/Cart";
const ReviewInvoice = () => { const ReviewInvoice = () => {
@ -14,9 +14,9 @@ const ReviewInvoice = () => {
const navigate = useNavigate(); const navigate = useNavigate();
const mySwal = withReactContent(Swal); const mySwal = withReactContent(Swal);
const cart = useContext(CartProvider); const cart = useContext(CartContext);
//const [products, setProducts] = useState([]); const [products, setProducts] = useState([]);
const [thisInvoice, setThisInvoice] = useState({ const [thisInvoice, setThisInvoice] = useState({
firstName: "", firstName: "",
@ -55,7 +55,7 @@ const ReviewInvoice = () => {
} }
}) })
//var prods = []; // var prods = [];
// Object.keys(thisInvoice.prodQuant).map((k) => { // Object.keys(thisInvoice.prodQuant).map((k) => {
// fetch(`https://localhost:7292/api/Product?id=${k}`) // fetch(`https://localhost:7292/api/Product?id=${k}`)
// .then(async (response) => { // .then(async (response) => {
@ -135,9 +135,12 @@ const ReviewInvoice = () => {
title: 'Commande envoyée avec succès!', title: 'Commande envoyée avec succès!',
timer: 2000, timer: 2000,
icon: "success" icon: "success"
}) }).then(
cart.items = []; () => {
navigate('./Morceaux'); cart.emptyCart();
navigate('/morceaux');
}
)
} }
else { else {
console.log("Erreur de creation la commande #" + thisInvoice.id); console.log("Erreur de creation la commande #" + thisInvoice.id);