-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.js
129 lines (119 loc) · 4.26 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
// function gatCartItems
function getCartItems(){
db.collection("cart-items").onSnapshot((snapshot) => {
let cartItems = [];
snapshot.docs.forEach((doc) => {
cartItems.push({
id: doc.id,
...doc.data()
})
})
generatedCartItems(cartItems);
getTotalCost(cartItems);
})
}
// function getCartItems ends
// function decreaseCount
function decreaseCount(itemId){
let cartItem = db.collection("cart-items").doc(itemId);
cartItem.get().then(function(doc) {
if(doc.exists) {
if(doc.data().quantity > 1){
cartItem.update({
quantity: doc.data().quantity - 1
})
}
}
})
}
// function decreaseCount ends
// function increaseCount
function increaseCount(itemId){
let cartItem = db.collection("cart-items").doc(itemId);
cartItem.get().then(function(doc) {
if(doc.exists) {
if(doc.data().quantity > 0){
cartItem.update({
quantity: doc.data().quantity + 1
})
}
}
})
}
// function increaseCount ends
// function deleteItem
function deleteItem(itemId){
db.collection("cart-items").doc(itemId).delete();
}
// function deleteItem ends
// function getTotalCost
function getTotalCost(items){
let totalCost = 0;
items.forEach((item) => {
totalCost += item.price * item.quantity;
})
document.querySelector(".total-cost-number").innerText = `₹${totalCost}.00`;
}
// function getTotalCost ends
// function generatedCartItems
function generatedCartItems(cartItems){
let itemsHTML = "";
cartItems.forEach((item) => {
itemsHTML += `
<div class="cart-item flex items-center pb-4 border-b border-gray-100">
<div class="cart-item-img w-40 h-24 bg-white rounded-lg p-4">
<img class="w-full h-full object-contain" src="${item.image}" alt="">
</div>
<div class="cart-item-details flex-grow">
<div class="item-title font-bold text-sm text-gray-600">
${item.name}
</div>
<div class="cart-item-brand text-small text-gray-400">
${item.make}
</div>
</div>
<div class="cart-item-counter w-48 flex item-center">
<div data-id="${item.id}" class="cart-item-decrease text-gray-400 bg-gray-100 cursor-pointer rounded h-6 w-6 flex justify-center items-center hover:bg-gray-200 mr-2">
<i class="fas fa-chevron-left fa-xs"></i>
</div>
<h4 class="text-gray-400">x${item.quantity}</h4>
<div data-id="${item.id}" class="cart-item-increase text-gray-400 bg-gray-100 cursor-pointer rounded h-6 w-6 flex justify-center items-center hover:bg-gray-200 ml-2">
<i class="fas fa-chevron-right fa-xs"></i>
</div>
</div>
<div class="cart-item-total-cost w-48 font-bold text-gray-400">
₹${item.price * item.quantity}.00
</div>
<div data-id="${item.id}" class="cart-item-delete w-10 font-bold text-gray-300 cursor-pointer hover:text-gray-400">
<i class="fas fa-times"></i>
</div>
</div>
`
})
document.querySelector(".cart-items").innerHTML = itemsHTML;
createEventListeners();
}
// function generatedCartItems ends
// function createEventListeners
function createEventListeners() {
let decreaseButtons = document.querySelectorAll(".cart-item-decrease");
let increaseButtons = document.querySelectorAll(".cart-item-increase");
let deleteButtons = document.querySelectorAll(".cart-item-delete");
decreaseButtons.forEach((button) => {
button.addEventListener("click", function(){
decreaseCount(button.dataset.id);
})
})
increaseButtons.forEach((button) => {
button.addEventListener("click", function(){
increaseCount(button.dataset.id);
})
})
deleteButtons.forEach((button) => {
button.addEventListener("click", function() {
deleteItem(button.dataset.id);
})
})
}
// function createEventListeners ends
getCartItems();