Skip to content

Commit

Permalink
Add cart functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rayc2045 committed May 10, 2024
1 parent c285962 commit 50e7f6c
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,29 @@ const cart = {
storeName: 'cart-items',
items: localStore.get(this.storeName) || [],
addItem(item) {
this.items.push(item);
let isInCart = false;
for (const cartItem of this.items)
if (cartItem.name === item.name && cartItem.color === item.color) {
isInCart = true;
cartItem.num++;
break;
}
if (!isInCart) this.items.push({ ...item, num: 1 });
localStore.set(this.storeName, cart.items);
},
deleteItem(item) {
this.items.splice(
this.items.findIndex(cartItem => cartItem.name === item.name && cartItem.color === item.color),
1
);
this.items.length
? localStore.set(this.storeName, cart.items)
: localStore.remove(this.storeName);
},
deleteAll() {
this.items.length = 0;
localStore.remove(this.storeName);
},
};

const user = {
Expand Down

0 comments on commit 50e7f6c

Please sign in to comment.