cleanout some code

This commit is contained in:
Andreas Zweili 2021-05-05 16:56:29 +02:00
parent 000139b9ae
commit 787b44dc13
3 changed files with 9 additions and 9 deletions

View File

@ -1,6 +1,10 @@
export default { export default {
quantity(state) { quantity(state) {
return state.qty; var itemsInCart = 0;
state.items.forEach(item => {
itemsInCart = itemsInCart + item.qty;
});
return itemsInCart;
}, },
items(state) { items(state) {
return state.items; return state.items;
@ -8,7 +12,9 @@ export default {
total(state) { total(state) {
var total = 0; var total = 0;
state.items.forEach(item => { state.items.forEach(item => {
total = total + item.price; var itemTotal = 0;
itemTotal = item.price * item.qty;
total = total + itemTotal;
}); });
return total.toFixed(2); return total.toFixed(2);
} }

View File

@ -6,8 +6,7 @@ export default {
namespaced: true, namespaced: true,
state() { state() {
return { return {
items: [], items: []
qty: 0
}; };
}, },
actions: cartActions, actions: cartActions,

View File

@ -16,17 +16,12 @@ export default {
}; };
state.items.push(newItem); state.items.push(newItem);
} }
state.qty++;
state.total += productData.price;
}, },
removeProductFromCart(state, prodId) { removeProductFromCart(state, prodId) {
const productInCartIndex = state.items.findIndex( const productInCartIndex = state.items.findIndex(
cartItem => cartItem.productId === prodId cartItem => cartItem.productId === prodId
); );
const prodData = state.items[productInCartIndex];
state.items.splice(productInCartIndex, 1); state.items.splice(productInCartIndex, 1);
state.qty -= prodData.qty;
state.total -= prodData.price * prodData.qty;
} }
}; };