-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcartItem.js
155 lines (112 loc) · 3.88 KB
/
cartItem.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
const logo = document.querySelector("#logo")
// console.log(logo)
logo.addEventListener("click", () => {
window.location.assign("index.html")
})
function decreaseProductQuantity(product) {
// console.log(product)
let cartItem = db.collection("cart-items").doc(product)
cartItem.get()
.then(doc => {
if (doc.exists) {
if (doc.data().quantity > 1) {
cartItem.update({
quantity: doc.data().quantity - 1
})
}
}
})
}
function increaseProductQuantity(product) {
let cartItem = db.collection("cart-items").doc(product)
cartItem.get()
.then(doc => {
if (doc.exists) {
if (doc.data().quantity > 0) {
cartItem.update({
quantity: doc.data().quantity + 1
})
}
}
})
}
function cancelProduct(product) {
db.collection("cart-items").doc(product).delete()
}
function sendTotalCost(totalCost) {
const totalAmount = document.querySelector(".totalAmount")
const checkoutAmount = document.querySelector(".totat-checkout")
totalAmount.innerText = `$${totalCost}`
checkoutAmount.innerHTML = `$${totalCost}`
}
function countTotalCost() {
db.collection("cart-items").onSnapshot(snapshot => {
let totalCost = 0
snapshot.forEach(doc => {
totalCost += doc.data().quantity * doc.data().price
})
console.log(totalCost)
sendTotalCost(totalCost)
})
}
countTotalCost()
function getAllProductFromCartCollection() {
const allCartProduct = document.querySelector(".all-cart-product")
db.collection("cart-items").onSnapshot((snapshot) => {
let cartProduct = ""
snapshot.forEach((doc) => {
// console.log(doc.id)
// console.log(doc.id)
cartProduct +=
`
<div class="cart-product">
<div class="product">
<img src="${doc.data().image}"
alt="${doc.data().name}" />
<h3>${doc.data().name}</h3>
</div>
<div class="quantity-of-product">
<span class="decrease" data-id="${doc.id}">
<i class="fas fa-chevron-left"></i>
</span>
<span>${doc.data().quantity}</span>
<span class="increase" data-id="${doc.id}">
<i class="fas fa-chevron-right"></i>
</span>
</div>
<div class="cost">
<h3>$${(doc.data().quantity)*(doc.data().price)}</h3>
</div>
<div class="cancel" data-id="${doc.id}">
<i class="fas fa-times"></i>
</div>
</div>
`
});
allCartProduct.innerHTML = cartProduct
takeCareOfButtonInCartItemArea()
});
}
function takeCareOfButtonInCartItemArea() {
const decrease = document.querySelectorAll(".decrease")
const increase = document.querySelectorAll(".increase")
const cancel = document.querySelectorAll(".cancel")
// console.log(decrease, increase, cancel)
decrease.forEach(button => {
button.addEventListener("click", () => {
console.log(button)
decreaseProductQuantity(button.dataset.id)
})
})
increase.forEach(button => {
button.addEventListener("click", () => {
increaseProductQuantity(button.dataset.id)
})
})
cancel.forEach(button => {
button.addEventListener("click", () => {
cancelProduct(button.dataset.id)
})
})
}
getAllProductFromCartCollection()