-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
48 lines (40 loc) · 1.33 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function cart() {
return {
cartItems: [],
totalPrice: 0.00,
init() {
this.cartItems = JSON.parse(localStorage.getItem('cartItems')) || [];
this.totalPrice = this.cartItems.reduce((total, item) => Number.parseFloat(total + item.price).toFixed(2), 0);
},
addItem(item) {
console.log(item);
this.cartItems.push(item);
this.totalPrice += item.price;
localStorage.setItem('cartItems', JSON.stringify(this.cartItems));
},
clearCart() {
this.cartItems = [];
this.totalPrice = 0.00;
localStorage.removeItem('cartItems');
},
removeItem(index) {
if (index > -1) {
this.totalPrice -= this.cartItems[index].price;
this.cartItems.splice(index, 1);
localStorage.setItem('cartItems', JSON.stringify(this.cartItems));
}
},
}
}
function getItems() {
return {
cards: [],
search: '',
async fetchItems() {
await fetch('data.json').then(response => response.json()).then(json => this.cards = json.items);
},
get filterItems() {
return this.cards.filter(item => item.title.toLowerCase().includes(this.search.toLowerCase()));
}
}
}