-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (47 loc) Β· 1.55 KB
/
index.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
let bagItems;
onLoad();
function onLoad() {
let bagItemsStr = localStorage.getItem('bagItems');
bagItems = bagItemsStr ? JSON.parse(bagItemsStr) : [];
displayItemsOnHomePage();
displayBagIcon();
}
function addToBag(itemId) {
bagItems.push(itemId);
localStorage.setItem('bagItems', JSON.stringify(bagItems));
displayBagIcon();
}
function displayBagIcon() {
let bagItemCountElement = document.querySelector('.bag-item-count');
if (bagItems.length > 0) {
bagItemCountElement.style.visibility = 'visible';
bagItemCountElement.innerText = bagItems.length;
} else {
bagItemCountElement.style.visibility = 'hidden';
}
}
function displayItemsOnHomePage() {
let itemsContainerElement = document.querySelector('.items-container');
if (!itemsContainerElement) {
return;
}
let innerHtml = '';
items.forEach(item => {
innerHtml += `
<div class="item-container">
<img class="item-image" src="${item.image}" alt="item image">
<div class="rating">
${item.rating.stars} β | ${item.rating.count}
</div>
<div class="company-name">${item.company}</div>
<div class="item-name">${item.item_name}</div>
<div class="price">
<span class="current-price">Rs ${item.current_price}</span>
<span class="original-price">Rs ${item.original_price}</span>
<span class="discount">(${item.discount_percentage}% OFF)</span>
</div>
<button class="btn-add-bag" onclick="addToBag(${item.id})">Add to Bag</button>
</div>`
});
itemsContainerElement.innerHTML = innerHtml;
}