-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcheckout.html
383 lines (358 loc) · 13.2 KB
/
checkout.html
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Favicon Icon -->
<link rel="icon" type="image/x-icon" href="/assets/brand-icon.svg">
<!-- Font Awesome CDN -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<!-- Links To All Style Files -->
<link rel="stylesheet" href="./main.css">
<title>Every Sunday | Cart</title>
<style>
/* CSS here */
.cart-section{
padding-block: 8rem;
}
h1 span{
color:var(--highlight);
}
table {
border-collapse: collapse;
width: 100%;
margin: auto;
margin-top: 50px;
text-align: center;
}
table td,
table th {
border: 1px solid var(--white);
padding: 0.75rem 1rem;
color:var(--white);
text-align: center;
}
table tr:nth-child(even) {
background-color: var(--background);
}
table tr:hover {
background-color: var(--background-light);
}
table th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: var(--background-dark);
color: white;
text-align: center;
}
#append select{
padding:0.25rem 0.5rem;
background-color: transparent !important;
border: 2px solid var(--white-light);
color: var(--white-light);
cursor: pointer;
border-radius: 8px;
}
#append select option{
background-color: var(--background-light);
color:var(--white);
cursor: pointer;
}
.checkout-container{
padding-block: 3rem;
display: flex;
align-items: center;
justify-content: space-between;
border:1px solid var(--white);
/* margin-top: 2rem; */
border-top: none;
padding:2rem;
}
#total{
font-size: var(--h3);
color:var(--highlight);
margin-left: 1rem;
}
.secondary-btn{
background-color: transparent !important;
background-image: none;
border:2px solid var(--primary);
color:var(--primary);
margin-right: 1rem;
}
</style>
</head>
<body>
<header>
<nav class="container">
<img src="./assets/brand-logo.svg" alt="Every Sunday Logo">
<ul>
<li><a href="/index.html">Home</a></li>
<li><a href="/destination.html">Destination</a></li>
<li><a href="/feedback.html">Feedback</a></li>
<li><a href="/prices.html">Pricing</a></li>
</ul>
<!-- <div id="toggle">
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
</div> -->
<div id="login-container">
<a href="./login.html" id="loginBtn"><i class="fa-solid fa-user" id="login-btn"></i></a>
<div style="display: none;">
<a href='./checkout.html'><i class="fa-solid fa-cart-shopping" style="margin-right:1rem"></i></a>
<a href="/" id="logout-btn" class='btn-small' style='border-radius:50px;color:var(--white)'>Log Out</a>
</div>
</div>
</nav>
</header>
<main>
<section class="container cart-section">
<!-- -->
<h1>Your <span>Cart</span></h1>
<table id="table">
<thead class="thead">
<th>City</th>
<th>People</th>
<th>Days</th>
<th>Tier</th>
<th>Remove Item</th>
</thead>
<tbody class="append" id="append">
<!-- Date Appended Here -->
</tbody>
</table>
<div class="checkout-container">
<h4>Your Total is: <span id="total"></span> <i class="fa-solid fa-sack-dollar" style="color:#cc8e35"></i></h4>
<div>
<button id="save-cart" class="btn secondary-btn">Save Cart</button>
<button id="buy-now" class="btn">Buy Now</button>
</div>
</div>
</section>
</main>
<footer>
<section class="articles"></section>
<div class="container">
<h1>Get started and early <span>Update</span> about our package</h1>
<!-- Copy this searchbox If you want to use it somewhere else -->
<div class="searchbox">
<input type="text" placeholder="Enter your email">
<button class="btn" id="cta">Get Started</button>
</input>
</div>
<hr>
<div class="footer">
<img src="./assets/brand-logo.svg" alt="Every Sunday Logo">
<ul>
<li><a href="/">Home</a></li>
<li><a href="/">Destination</a></li>
<li><a href="/">Feedback</a></li>
<li><a href="/">Pricing</a></li>
</ul>
<p><i class="fa-regular fa-copyright"></i> 2023 Every Sunday</p>
</div>
<div class="socials">
<div class="social">
<a href="https://www.instagram.com/" target="_blank"><i class="fa-brands fa-instagram"></i></a>
</div>
<div class="social">
<a href="https://twitter.com/" target="_blank"><i class="fa-brands fa-twitter"></i></a>
</div>
<div class="social">
<a href="https://www.facebook.com/" target="_blank"><i class="fa-brands fa-facebook"></i></a>
</div>
</div>
</div>
</footer>
</body>
</html>
<script src="main.js"></script>
<script>
// JS code goes here
let tbody = document.querySelector('#append');
let total = document.querySelector('#total');
let saveCart=document.querySelector('#save-cart');
let buyBtn=document.querySelector('#buy-now');
let table = document.querySelector('#table');
let cartItems=[];
let user;
let totalValue=0;
window.addEventListener('load', getUser);
saveCart.addEventListener('click',()=>{
postCart(cartItems);
getTotal();
});
buyBtn.addEventListener('click',()=>{
displayMessage("Payment Successful!",'success');
});
async function getUser() {
let id=JSON.parse(localStorage.getItem('userId'));
let url = `https://mock-every-sunday-server.onrender.com/users?id=${id}`;
try {
let res = await fetch(url);
let data= await res.json();
// console.log(data[0].cart);
user=data[0];
cartItems=data[0].cart;
if(cartItems.length==0){
table.innerHTML='<h2>Nothing In Cart</h2>';
document.querySelector('.checkout-container').style.display="none";
return;
}
displayCart(cartItems);
getTotal();
} catch (e) {
console.log(e);
}
}
function displayCart(cart){
tbody.innerHTML="";
cart.forEach((item,i)=>{
let price = item.price;
let row = document.createElement('tr');
let city=document.createElement('td');
city.innerText = item.city;
let numPeople=document.createElement('td');
// numPeople.style.display = 'flex';
let peopleCont=document.createElement('div');
peopleCont.style.display = 'flex';
peopleCont.style.justifyContent = 'center';
let peopleValue= document.createElement('p');
peopleValue.style.marginInline = '0.5rem';
peopleValue.innerText =item.numOfPeople;
let minus1=document.createElement('button');
minus1.innerText='-';
minus1.classList.add('btn-small');
minus1.addEventListener('click',()=>{
if(peopleValue.innerText>1){
item.numOfPeople=+peopleValue.innerText-1;
peopleValue.innerText=+peopleValue.innerText-1;
// console.log(cartItems);
getTotal();
}
})
let plus1=document.createElement('button');
plus1.innerText='+';
plus1.classList.add('btn-small');
plus1.addEventListener('click',()=>{
item.numOfPeople=+peopleValue.innerText+1;
peopleValue.innerText=+peopleValue.innerText+1;
// console.log(cartItems);
getTotal();
})
peopleCont.append(minus1,peopleValue,plus1);
numPeople.append(peopleCont);
let numDays=document.createElement('td');
let daysCont=document.createElement('div');
daysCont.style.display = 'flex';
daysCont.style.justifyContent = 'center';
// numDays.style.display = 'flex';
let daysValue= document.createElement('p');
daysValue.style.marginInline = '0.5rem';
daysValue.innerText =item.numOfDays;
let minus2=document.createElement('button');
minus2.innerText='-';
minus2.classList.add('btn-small');
minus2.addEventListener('click',()=>{
if(daysValue.innerText>1){
item.numOfDays=+daysValue.innerText-1;
daysValue.innerText=+daysValue.innerText-1;
// console.log(cartItems);
getTotal();
}
})
let plus2=document.createElement('button');
plus2.innerText='+';
plus2.classList.add('btn-small');
plus2.addEventListener('click',()=>{
item.numOfDays=+daysValue.innerText+1;
daysValue.innerText=+daysValue.innerText+1;
// console.log(cartItems);
getTotal();
})
daysCont.append(minus2,daysValue,plus2);
numDays.append(daysCont);
let tierCont=document.createElement('td');
let tier = document.createElement("select");
let options = ["Basic", "Premium", "Luxury"];
for (let i = 0; i < options.length; i++) {
let optionElement = document.createElement("option");
optionElement.text = options[i];
optionElement.value = options[i];
tier.append(optionElement);
}
if(item.tier=='Basic'){
tier.value='Basic';
}else if(item.tier=='Premium'){
tier.value='Premium';
}else if(item.tier=='Luxury'){
tier.value='Luxury';
}
tier.addEventListener("change",()=>{
item.tier = tier.value;
// console.log(cartItems);
getTotal();
});
console.log(tier.value);
tierCont.append(tier);
let td=document.createElement('td');
let delBtn=document.createElement('button');
delBtn.innerText="Delete";
delBtn.classList.add('btn-small');
delBtn.style.backgroundImage='none';
delBtn.style.backgroundColor='var(--failure)';
delBtn.addEventListener('click',()=>{
cartItems=cartItems.filter(el=>el.city!=item.city);
// console.log(cartItems);
displayCart(cartItems);
getTotal();
});
td.append(delBtn)
row.append(city,numPeople,numDays,tierCont,td);
tbody.append(row);
})
}
function getTotal(){
totalValue=0;
cartItems.forEach((item)=>{
// console.log(item.basePrice);
if(item.tier=='Basic'){
totalValue+=+item.basePrice;
}else if(item.tier=='Premium'){
totalValue+=+item.basePrice*2;
}else if(item.tier=='Luxury'){
totalValue+=+item.basePrice*3;
}
totalValue=totalValue*(+item.numOfDays);
totalValue=totalValue*(+item.numOfPeople);
// console.log(totalValue);
console.log(totalValue);
})
total.innerText=totalValue;
}
async function postCart(userCart) {
let id= JSON.parse(localStorage.getItem('userId'));
console.log(id);
let url = `https://mock-every-sunday-server.onrender.com/users/${id}`;
try {
let res = await fetch(url, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
cart: userCart,
}),
});
let data = await res.json();
// console.log(data);
displayMessage("New Cart Saved!", "success");
} catch (e) {
console.log(e);
displayMessage("Something Went Wrong! :/", "warning");
}
}
</script>