-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
171 lines (151 loc) · 4.4 KB
/
main.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
let carts = document.querySelectorAll('.add-cart');
let products = [
{
name: 'Antique Floral Clock',
tag: 'clock2',
price: 7000,
incart: 0
},
{
name: 'Antique Artistic Clock',
tag: 'clock3',
price: 6500,
incart: 0
},
{
name: 'Antique Pocket Watch',
tag: 'clock4',
price: 4000,
incart: 0
},
{
name: 'Antique Center Chair',
tag: 'furn1',
price: 7500,
incart: 0
},
{
name: 'Antique Templesmac',
tag: 'furn2',
price: 15500,
incart: 0
},
{
name: 'Antique Bookshelf',
tag: 'furn5',
price: 17000,
incart: 0
},
{
name: 'Antique Brass Fan',
tag: 'brass1',
price: 5500,
incart: 0
},
{
name: 'Antique Brass Buddha',
tag: 'brass2',
price: 9500,
incart: 0
}
];
for(let i=0; i < carts.length; i++) {
carts[i].addEventListener('click', () => {
cartNumbers(products[i]);
totalCost(products[i])
})
}
function onLoadCartNumbers(){
let productNumbers = localStorage.getItem('cartNumbers');
if(productNumbers){
document.querySelector('.cart span').textContent = productNumbers;
}
}
function cartNumbers(products) {
console.log("The product clicked is", products);
let productNumbers = localStorage.getItem('cartNumbers');
productNumbers = parseInt(productNumbers);
if(productNumbers){
localStorage.setItem('cartNumbers', productNumbers + 1);
document.querySelector('.cart span').textContent = productNumbers + 1;
}
else{
localStorage.setItem('cartNumbers', 1);
document.querySelector('.cart span').textContent = 1;
}
setItems(products);
}
function setItems(products){
let cartItems = localStorage.getItem('productsincart');
cartItems = JSON.parse(cartItems);
if(cartItems != null) {
if(cartItems[products.tag] == undefined) {
cartItems = {
...cartItems,
[products.tag]: products
}
}
cartItems[products.tag].incart += 1;
}
else{
products.incart = 1;
cartItems = {
[products.tag]: products
}
}
localStorage.setItem("productsincart", JSON.stringify(cartItems));
}
function totalCost(products){
let cartCost = localStorage.getItem("totalCost");
console.log("My cartCost is", cartCost);
console.log(typeof cartCost);
if(cartCost != null){
cartCost = parseInt(cartCost);
localStorage.setItem("totalCost", cartCost + products.price);
}
else{
localStorage.setItem("totalCost", products.price);
}
}
function displayCart(){
let cartItems = localStorage.getItem("productsincart");
cartItems = JSON.parse(cartItems);
let productContainer = document.querySelector
(".products");
let cartCost = localStorage.getItem("totalCost");
console.log(cartItems);
if(cartItems && productContainer){
productContainer.innerHTML = '';
Object.values(cartItems).map(item => {
productContainer.innerHTML += `
<div class = "products">
<ion-icon name="close-circle"></ion-icon>
<img src="./images/${item.tag}.jpg">
<span class="name">${item.name}</span>
</div>
<div class = "others">
<div class = "price">₹${item.price}</div></div>
<div class="other"><div class = "quantity">
<ion-icon class="decrease" name= "arrow-dropleft-circle"></ion-icon>
<span>${item.incart}</span>
<ion-icon class="increase" name= "arrow-dropright-circle"></ion-icon>
</div>
<div class= "total">
₹${item.incart * item.price}
</div>
</div>
`;
});
productContainer.innerHTML += `
<div class= "basketTotalContainer">
<h4 class="basketTotalTitle">
Basket Total
</h4>
<h4 class= "basketTotal">
₹${cartCost}
</h4>
`;
}
}
onLoadCartNumbers();
displayCart();