Repository navigation
Expand file tree
/
Copy pathproduct.js
More file actions
191 lines (152 loc) · 4.91 KB
/
Copy pathproduct.js
File metadata and controls
191 lines (152 loc) · 4.91 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
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
181
182
183
184
185
186
187
188
189
190
191
// This page will provide all the necessary js for product.html and the function mentioned on it.
let backBTN=(localStorage.getItem("backBTN")) || 0;
// reade note
let query=(localStorage.getItem("query") )|| "";
// JSOn requred hai back button k liye
main(query);
let data;
// function "main" for fetching data using api link with "query" argument;
async function main(query) {
try {
let res = await fetch(`http://makeup-api.herokuapp.com/api/v1/products.json?product_type=${query}`)
// let res = await fetch(`https://makeup-api.herokuapp.com/api/v1/products.json`)
data = await res.json()
console.log(data);
displayData(data) //calling display data (dom)
} catch (err) {
console.log("Not found")
console.log("err",err)
}
}
// availables tags(Blush,Bronzer,Eyebrow,Eyeshadow,Foundation,Lip_liner,Lipstick,Mascara,Nail_polish)
// main(query)
//function for button (onlick function)
function pressedbtn(ele)
{
console.log(ele.id)
//caling data using available tabs
categoryTitle=ele.id
//heading of category
localStorage.setItem("query",JSON.stringify(categoryTitle));
document.getElementById("category-title").innerText=categoryTitle;
main(ele.id)
}
let stpg = 0;
let edpg = 18
function nextPagination(){
if(stpg>=0&&edpg<data.length){
stpg = stpg + 18;
edpg = edpg + 18;
displayData(data);
window.location.href = "#category-title-div";
console.log(stpg, edpg)
}
}
function prevPagination(){
if(stpg>=0&&edpg>18){
stpg = stpg - 18;
edpg =edpg - 18;
displayData(data);
window.location.href = "#category-title-div";
console.log(stpg, edpg)
}
}
//display data function (DOM)
function displayData(data)
{
let query=(localStorage.getItem("query")) || "";
if(backBTN==1)
{
document.getElementById("category-title").innerText=query;
localStorage.setItem("backBTN",JSON.stringify("0"));
}
else
{
document.getElementById("category-title").innerText=query;
}
let container=document.getElementById("product-contianer")
container.innerHTML="";
//applying highorder function
// data.forEach(function(ele,index){
// console.log(ele)
for(let j=stpg; j<edpg; j++){
let ele = data[j];
let maindiv = document.createElement('div');
let img = document.createElement('img');
img.src=ele.api_featured_image;
let title = document.createElement('h3');
title.innerText=ele.name;
let desc = document.createElement('p');
desc.setAttribute("id","Pro_desc")
if(ele.brand==null || ele.brand=="")
{
ele.brand=`Brand: - SUGAR`;
}
else{
desc.innerText=`Brand : ${ele.brand}`;
}
//creating div for display shade
let shade_div = document.createElement('div');
let shade_icon = document.createElement('img');
//importing image from storage (image folder)
shade_icon.src="./images/plus-icon.png";
let shade_count = document.createElement('h3');
shade_count.innerText=`${ele.product_colors.length} SHADES`;
//if price zero then out of stock code
const price = document.createElement('h2');
if(ele.price==0 || ele.price==null)
{
price.innerText="Outof Stock";
price.style.color="red";
}
else{
price.innerText=`₹${Math.floor(+(ele.price)*30)}`;
}
//appenf all div's here
shade_div.append(shade_icon,shade_count)
maindiv.append(img,title,desc,shade_div,price);
container.append(maindiv)
//collecting product details data in local storage key "productDetail"
maindiv.addEventListener("click",function(){
if(ele.brand==null || ele.brand=="")
{
ele["brand"]="SUGAR";
}
localStorage.setItem("productDetail",JSON.stringify(ele));
window.location.href="./productDetail.html";
})
}
// });
}
//search quaery function
function searchquery()
{
let cat_title;
let temp = document.querySelector('#search').value;
console.log(temp);
if(temp=="")
{
temp="Eyeshadow";
cat_title= document.getElementById("category-title")
localStorage.setItem("query",JSON.stringify(temp));
main(temp)
}
else
{
cat_title=document.getElementById("category-title");
cat_title.innerText=`Search Result for "${temp}"`;
localStorage.setItem("query",JSON.stringify(temp));
main(temp)
}
}
// creating debouncing function for search functionality
let id;
function debounce(fun,time){
if(id)
{
clearTimeout(id);
}
id = setTimeout(function () {
fun()
}, time)
}