-
Notifications
You must be signed in to change notification settings - Fork 2
/
cart.js
238 lines (179 loc) · 6.42 KB
/
cart.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import { navbar} from "./function.js";
import { footer } from "./footer.js";
console.log(footer)
let userLoggedIn = localStorage.getItem("user-logged-in");
let cartProducts = JSON.parse(localStorage.getItem("cart-products")) || [];
const cart = document.getElementById("cart");
const cartBill = document.getElementById("cart-bill");
getCartProducts();
async function getCartProducts(){
try {
let res = await fetch("http://localhost:3000/api/cartproducts");
let data = await res.json();
displayCartProducts(data);
totalItems(data.length)
} catch (error) {
console.log(error)
}
}
billsection();
function billsection(){
cartBill.innerHTML = `
<div id="promo-code">
<details>
<summary> <b> Add a Promo Code </b></summary>
<span>
<input id="code" placeholder="Enter Promo Code" type="text">
<button id="promo-btn" >Apply</button>
</span>
</details>
</div>
<div id="total-bill">
<table>
<tr>
<td><b id="total-items" >(0) Items : </b></td>
<td><b>INR </b></td>
</tr>
<tr>
<td><b>Subtotal:</b></td>
<td><b id="sub-total" >INR 0</b></td>
</tr>
<tr>
<td>Promo code:</td>
<td id="promo-val" > - INR 0</td>
</tr>
<tr>
<td><h3>Your Total:</h3></td>
<td><h3 id="total" >INR</h3></td>
</tr>
</table>
<button id="checkout-btn"><img width="16px" src="assets/lock.svg" alt=""> Check Out</button>
</div>
`
const promoBtn = document.getElementById("promo-btn");
promoBtn.addEventListener("click", ()=>{
let total = document.getElementById("total");
let promoVal = document.getElementById("promo-val");
let subT = +document.getElementById("sub-total").innerText;
let code = document.getElementById("code").value;
if(code==="masai20"){
let dis = subT*0.20
promoVal.innerText = "- INR "+ dis.toFixed(2);
total.innerText = eval(subT-dis);
} else {
alert("Invalid Promo code")
}
})
const checkoutBtn = document.getElementById("checkout-btn");
checkoutBtn.addEventListener("click", ()=>{
let total = document.getElementById("total").innerText;
localStorage.setItem("cart-total", total);
window.location.href = "dee_Billing_Address.html";
});
}
function displayCartProducts(cartProductsapi){
if(cartProductsapi.length===0){
cart.innerHTML = `<div>
<h2>Your Shopping cart is empty.</h2>
<p>Started a cart already? Sign in to get your item!</p>
<button id="signin-btn">Sign In</button>
<p>New to Furniture Stock? <a href="register-login.html">Create an account</a></p>
</div>
`
const btn1 = document.querySelector("#cart button");
btn1.addEventListener("click", ()=>{
window.location.href = "register-login.html";
});
} else {
cart.style.border = "none";
cart.style.padding = 0;
let a = cartProductsapi.reduce((a, b)=>{
return a + Number(b.price) * Number(b.quantity);
}, 0);
document.getElementById("sub-total").innerText = a;
document.getElementById("total").innerText = a
cartProductsapi.forEach((elem) => {
let div = document.createElement("div");
div.id = "add-cart"
let img = document.createElement("img");
img.src = elem.img;
let span1 = document.createElement("span");
let name = document.createElement("h2");
name.innerText = elem.item;
let price = document.createElement("p");
price.innerText = "Price: INR"+" "+elem.price;
let span2 = document.createElement("span");
let p = document.createElement("p");
p.innerText = "Quantity";
let quant = document.createElement("select");
quant.innerHTML = ` <select id="quantity-select">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
</select>`;
quant.value=elem.quantity;
quant.addEventListener("change", ()=>{
console.log(elem.id, +quant.value);
changeQuant(elem.id, +quant.value);
});
let removeBtn = document.createElement("button");
removeBtn.id = "remove-btn";
removeBtn.innerText = "Remove";
removeBtn.addEventListener("click", ()=>{
removeElem(elem.id);
});
span2.append(p, quant);
span1.append(name, price, span2, removeBtn)
div.append(img, span1);
cart.append(div);
});
}
}
async function removeElem(id){
try {
fetch(`http://localhost:3000/api/cartproducts/${id}`, {
method: "DELETE"
})
} catch (error) {
console.log(error);
}
}
function totalItems(n){
document.getElementById("total-items").innerText = `(${n}) Items : `;
}
async function changeQuant(id, val){
try {
fetch(`http://localhost:3000/api/cartproducts/${id}`, {
method: "PATCH",
headers:{
'Content-Type': 'application/json'
},
body: JSON.stringify({quantity: val})
})
} catch (error) {
console.log(error);
}
}
let navtags = document.querySelector('#navbars')
navtags.innerHTML= navbar();
// // footer
let footertag = document.querySelector('#footerarea')
// // console.log(footer)
footertag.innerHTML= footer();