-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestingapp.js
131 lines (113 loc) · 3.32 KB
/
testingapp.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
let label = document.getElementById("label");
let ShoppingCart = document.getElementById("shopping-cart");
let basket = JSON.parse(localStorage.getItem("data")) || [];
let calculation = () => {
let cartIcon = document.getElementById("cartAmount");
cartIcon.innerHTML = basket.map((x) => x.item).reduce((x, y) => x + y, 0);
};
calculation();
let generateCartItems = () => {
if (basket.length !== 0) {
return (ShoppingCart.innerHTML = basket
.map((x) => {
let { id, item } = x;
let search = shopItemsData.find((y) => y.id === id) || [];
return `
<div class="cart-item">
<img width="100" src=${search.img} alt="" />
<div class="details">
<div class="title-price-x">
<h4 class="title-price">
<p>${search.name}</p>
<p class="cart-item-price">$ ${search.price}</p>
</h4>
<i onclick="removeItem(${id})" class="bi bi-x-lg"></i>
</div>
<div class="buttons">
<i onclick="decrement(${id})" class="bi bi-dash-lg"></i>
<div id=${id} class="quantity">${item}</div>
<i onclick="increment(${id})" class="bi bi-plus-lg"></i>
</div>
<h3>$ ${item * search.price}</h3>
</div>
</div>
`;
})
.join(""));
} else {
ShoppingCart.innerHTML = ``;
label.innerHTML = `
<h2>Cart is Empty</h2>
<a href="index.html">
<button class="HomeBtn">Back to home</button>
</a>
`;
}
};
generateCartItems();
let increment = (id) => {
let selectedItem = id;
let search = basket.find((x) => x.id === selectedItem.id);
if (search === undefined) {
basket.push({
id: selectedItem.id,
item: 1,
});
} else {
search.item += 1;
}
generateCartItems();
update(selectedItem.id);
localStorage.setItem("data", JSON.stringify(basket));
};
let decrement = (id) => {
let selectedItem = id;
let search = basket.find((x) => x.id === selectedItem.id);
if (search === undefined) return;
else if (search.item === 0) return;
else {
search.item -= 1;
}
update(selectedItem.id);
basket = basket.filter((x) => x.item !== 0);
generateCartItems();
localStorage.setItem("data", JSON.stringify(basket));
};
let update = (id) => {
let search = basket.find((x) => x.id === id);
// console.log(search.item);
document.getElementById(id).innerHTML = search.item;
calculation();
TotalAmount();
};
let removeItem = (id) => {
let selectedItem = id;
// console.log(selectedItem.id);
basket = basket.filter((x) => x.id !== selectedItem.id);
generateCartItems();
TotalAmount();
localStorage.setItem("data", JSON.stringify(basket));
};
let clearCart = () => {
basket = [];
generateCartItems();
localStorage.setItem("data", JSON.stringify(basket));
};
let TotalAmount = () => {
if (basket.length !== 0) {
let amount = basket
.map((x) => {
let { item, id } = x;
let search = shopItemsData.find((y) => y.id === id) || [];
return item * search.price;
})
.reduce((x, y) => x + y, 0);
// console.log(amount);
label.innerHTML = `
<h2>Total Bill : $ ${amount}</h2>
<button class="checkout">Checkout</button>
<button onclick="clearCart()" class="removeAll">Clear Cart</button>
`;
} else return;
};
TotalAmount();