-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcartpage.js
79 lines (61 loc) · 3.22 KB
/
cartpage.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
// CART PAGE
function Cart() {
const cartContent = document.querySelector(".text");
fetch("http://localhost:3000/cart")
.then((response) => response.json())
.then((data) => {
console.log(data.Cart);
cartContent.innerHTML = ''
if (data) {
let total = 0
for (let i = 0; i < data.Cart.length; i++) {
const hour = String(
new Date(data.Cart[i].trip.date).getHours()).padStart(2, "0");
const minute = String(
new Date(data.Cart[i].trip.date).getMinutes()).padStart(2, "0");
cartContent.innerHTML +=
`<div class="travelsList" id=${data.Cart[i].trip._id}>
<div class="travel">${data.Cart[i].trip.departure} > ${data.Cart[i].trip.arrival}</div>
<div class="travel-time">${hour} : ${minute}</div>
<div class="travel-price" value='${data.Cart[i].trip.price}'>${data.Cart[i].trip.price}€</div>
<button class="delete-btn" type='button' value=${data.Cart[i].trip._id}>X</button>
</div>`
total += data.Cart[i].trip.price;
document.querySelector('#count').textContent = total;
}
let deleteBtn = document.querySelectorAll(".delete-btn")
for (let i = 0; i < deleteBtn.length; i++) {
deleteBtn[i].addEventListener('click',
function () {
this.parentNode.remove();
fetch(`http://localhost:3000/cart`, {
method: 'DELETE',
headers: { 'Content-Type': 'application/json' },
})
.then((response) => response.json())
.then(() => {
console.log(data.Cart[i].trip.price)
console.log(total)
total -= data.Cart[i].trip.price;
console.log(total)
document.querySelector('#count').textContent = total;
})
}
)
}
document.querySelector('#purchase-btn').addEventListener('click',
function () {
fetch(`http://localhost:3000/booking`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({})
}).then((response) => response.json())
.then(data => {
console.log(data)
window.location.assign('booking.html')
})
})
}
})
}
Cart()