-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
54 lines (46 loc) · 1.74 KB
/
main.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
const API_URL = `https://fakestoreapi.com/products`;
let data = [];
let cardsContainer = document.querySelector(".cards-container");
async function fetchProducts() {
let response = await fetch(API_URL);
data = await response.json();
}
document.addEventListener("DOMContentLoaded", async function () {
await fetchProducts();
let htmlString = ``;
let data2 = [data[0], data[1]];
//data = data[0];
data.forEach((product, index) => {
const filledStarsCount = parseInt(product.rating.rate);
let unfilledStarsCount = 5 - filledStarsCount;
let tmpDiv = document.createElement("div");
for (let i = 0; i < filledStarsCount; i++) {
tmpDiv.innerHTML += `<span class="start__filled">★</span>`;
}
for (let i = 0; i < unfilledStarsCount; i++) {
tmpDiv.innerHTML += `<span class="start__unfilled">☆</span>`;
}
console.log(index, tmpDiv.innerHTML);
cardsContainer.innerHTML += `<div class="card-prod">
<div class="image-container">
<img src="${product.image}" class="product-image">
</div>
<div class="product-details">
<span class="product-title">${product.title}</span>
<!-- category -->
<span class="product-category">${product.category}</span>
<!-- rating -->
<div class="product-rating">
${tmpDiv.innerHTML}
</div>
<!-- rating count -->
<div>Rating Count: <span class="product-rating-count">${product.rating.count}</span> </div>
</div>
<div class="product-price">
<button class="price-btn">${product.price}</button>
</div>
</div>`;
tmpDiv.innerHTML = ``;
return product;
});
});