Skip to content
Snippets Groups Projects

Finalisation du panier

Merged Princelle Maxime requested to merge develop into main
Viewing commit e288e7d0
Prev
Show latest version
5 files
+ 150
86
Preferences
Compare changes
Files
5
+ 9
2
@@ -9,7 +9,7 @@ export function addToCart(cart: Product[], dispatch: Dispatch<ActionType>, produ
if (existingProduct) {
existingProduct.quantity = existingProduct.quantity ? existingProduct.quantity + 1 : 1;
existingProduct.stockPrice = existingProduct.stockPrice ? existingProduct.stockPrice + existingProduct.price : existingProduct.price;
existingProduct.stockPrice = existingProduct.price * existingProduct.quantity;
return setCart(newCart, dispatch);
}
@@ -24,11 +24,12 @@ export function removeFromCart(cart: Product[], dispatch: Dispatch<ActionType>,
const existingProduct = newCart.find(p => p.name === product.name);
if (existingProduct) {
existingProduct.quantity = existingProduct.quantity ? existingProduct.quantity - 1 : 0;
existingProduct.stockPrice = existingProduct.stockPrice ? existingProduct.stockPrice - existingProduct.price : existingProduct.price;
existingProduct.stockPrice = existingProduct.price * existingProduct.quantity;
// If quantity is 0, remove product from cart
if (existingProduct.quantity === 0) {
return setCart(newCart.filter(p => p.name !== product.name), dispatch);
}
return setCart(newCart, dispatch);
}
return setCart(cart, dispatch);
@@ -41,6 +42,12 @@ export function updateProductQuantity(cart: Product[], dispatch: Dispatch<Action
const existingProduct = newCart.find(p => p.name === product.name);
if (existingProduct) {
existingProduct.quantity = quantity;
existingProduct.stockPrice = existingProduct.price * quantity;
if (existingProduct.quantity === 0) {
return setCart(newCart.filter(p => p.name !== product.name), dispatch);
}
return setCart(newCart, dispatch);
}
return setCart(newCart, dispatch);