-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
180 lines (149 loc) · 5.54 KB
/
script.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"use strict;"
fetchProducts();
// To fetch list of products from makeup API and display them
async function fetchProducts(products) {
try {
const container = document.querySelector(".container");
const loader = document.querySelector("#loading");
if (!products) {
sessionStorage.clear();
loader.classList.add("display");
const data = await fetch(
"https://makeup-api.herokuapp.com/api/v1/products.json"
);
products = await data.json();
loader.classList.remove("display");
}
let currentPage = sessionStorage.getItem("currentPage");
if (!currentPage) {
currentPage = 1;
sessionStorage.setItem("currentPage", currentPage);
}
products
.slice((currentPage - 1) * 9, currentPage * 9)
.forEach((product) => {
const card = `
<div class="card">
<img class="product-image" src='${product["api_featured_image"]}' alt='${product["name"]}'>
<div class="card-content">
<p class="product-brand">${product["brand"]}</p>
<h3 class="product-name">${product["name"]}</h3>
<div class="product-description"><p>${product["description"]}</p></div>
<div class="card-footer">
<p class="product-price">${product["price"]}</p>
<a class="button" href='${product["product_link"]}' target="_blank">BUY</a>
</div>
</div>
</div>
`;
container.insertAdjacentHTML("beforeend", card);
const cardEventListener = document
.querySelector(".card:last-child")
.addEventListener("click", function () {
showModal(product);
});
const button = document.querySelector(".card:last-child .button");
button.onclick = (event) => {
event.stopPropagation();
return true;
};
});
const navigation = document.createElement("div");
navigation.className = "navigation";
const previous = document.createElement("button");
previous.classList.add("button", "previous");
previous.innerHTML = "« Previous";
previous.addEventListener("click", function () {
previousPage(products);
});
const next = document.createElement("button");
next.classList.add("button", "next");
next.innerHTML = "Next »";
next.addEventListener("click", function () {
nextPage(products);
});
navigation.append(previous, next);
document.body.append(navigation);
} catch (error) {
console.log("Reloading Page. Error occurred: " + error);
location.reload();
}
}
// To move to next page
function nextPage(products) {
const next = document.querySelector(".next");
const currentPage = Number(sessionStorage.getItem("currentPage"));
const totalPages = Math.ceil(products.length / 9);
if (currentPage < totalPages) {
clearPage();
sessionStorage.setItem("currentPage", currentPage + 1);
fetchProducts(products);
topFunction();
} else {
disableButton(next);
}
}
// To move to previous page
function previousPage(products) {
const previous = document.querySelector(".previous");
const currentPage = Number(sessionStorage.getItem("currentPage"));
if (currentPage > 1) {
clearPage();
sessionStorage.setItem("currentPage", currentPage - 1);
fetchProducts(products);
topFunction();
} else {
disableButton(previous);
}
}
// To clear the container and remove navigation buttons
function clearPage() {
const container = document.querySelector(".container");
const navigation = document.querySelector(".navigation");
container.innerHTML = "";
navigation.remove();
}
// When the user clicks on the button, scroll to the top of the document
function topFunction() {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}
// To disable hover effect and changes opacity of button element
function disableButton(button) {
button.style.pointerEvents = "none";
button.style.opacity = "0.4";
}
// To pop up the modal on screen
function showModal(product) {
const backdrop = document.querySelector(".backdrop");
const modalElement = `
<div class="modal">
<button class="modal-close">X</button>
<div class="modal-product-image">
<img src='${product["api_featured_image"]}' alt='${product["name"]}'>
</div>
<div class="modal-product-info">
<p class="product-brand">${product["brand"]}</p>
<h1 class="product-name">${product["name"]}</h1>
<p class="product-description">${product["description"]}</p>
<div class="modal-footer">
<h1 class="product-price">${product["price"]}</h1>
<a href='${product["product_link"]}' target="_blank" class="button">BUY</a>
</div>
</div>
</div>`;
backdrop.insertAdjacentHTML("afterend", modalElement);
const modal = document.querySelector(".modal");
const modalCloseEventListener = document.querySelector(".modal-close");
backdrop.classList.add("display");
modal.classList.add("display");
backdrop.addEventListener("click", closeModal);
modalCloseEventListener.addEventListener("click", closeModal);
}
// To close the modal on screen
function closeModal() {
const backdrop = document.querySelector(".backdrop");
const modal = document.querySelector(".modal");
backdrop.classList.remove("display");
modal.remove();
}