-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathscript.js
More file actions
75 lines (69 loc) · 2.39 KB
/
script.js
File metadata and controls
75 lines (69 loc) · 2.39 KB
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
function getItems(){
db.collection("items").get().then((querySnapshot) => {
let items = [];
querySnapshot.forEach((doc) => {
items.push({
id: doc.id,
image: doc.data().image,
name: doc.data().name,
make: doc.data().make,
rating: doc.data().rating,
price: doc.data().price
})
});
generateItems(items)
});
}
function addToCart(item){
let cartItem = db.collection("cart-items").doc(item.id);
cartItem.get()
.then(function(doc){
if(doc.exists){
cartItem.update({
quantity: doc.data().quantity + 1
})
} else {
cartItem.set({
image: item.image,
make: item.make,
name: item.name,
price: item.price,
rating: item.rating,
quantity: 1
})
}
})
}
function generateItems(items) {
let itemsHTML = "";
items.forEach((item) => {
let doc = document.createElement("div");
doc.classList.add("main-product", "mr-5");
doc.innerHTML = `
<div class="product-image w-48 h-52 bg-white rounded-lg p-4">
<img class="w-full h-full object-contain" src="${item.image}">
</div>
<div class="product-name text-gray-700 font-bold mt-2 text-sm">
${item.name}
</div>
<div class="product-make text-green-700 font-bold">
${item.make}
</div>
<div class="product-rating text-yellow-300 font-bold my-1">
⭐⭐⭐⭐⭐ ${item.rating}
</div>
<div class="product-price font-bold text-gray-700 text-lg">
${numeral(item.price).format('$0,0.00')}
</div>
`
let addToCartEl = document.createElement("div");
addToCartEl.classList.add("hover:bg-yellow-600", "cursor-pointer", "product-add", "h-8", "w-28", "rounded", "bg-yellow-500", "text-white", "text-md", "flex", "justify-center", "items-center");
addToCartEl.innerText = "Add to cart";
addToCartEl.addEventListener("click", function(){
addToCart(item)
})
doc.appendChild(addToCartEl);
document.querySelector(".main-section-products").appendChild(doc);
})
}
getItems();