-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (139 loc) · 4.61 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
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
let url = `https://specialized.onrender.com/products`
const fetchdata =async (path)=>{
try {
let res = await fetch(`${path}`)
res = await res.json();
return res
} catch (error) {
console.log(error);
}
}
const main = async ()=>{
let data = await fetchdata(url)
displaydata(data);
localStorage.setItem("product-list",JSON.stringify(data))
}
main()
let wishArr = JSON.parse(localStorage.getItem("wishlistitem")) || []
function catPage(){
window.location.href ="category/category.html"
}
function displaydata(data){
document.querySelector(".h_bcycles").textContent=""
data.map(function(ele,i){
let box = document.createElement("div")
let img = document.createElement("img")
let title = document.createElement("p")
title.addEventListener("click",()=>{
localStorage.setItem("item",ele.id);
window.location.href="itempage.html"
})
if(i==0 || i==11){
img.src="./logos/s-works1.png"
title.textContent = "S-Works Shiv TT Disc"
}else if(i==1 || i==29){
img.src="./logos/s-work2.png"
title.textContent = "S-works tarmac sl7"
}else if(i==2 || i==12){
img.src="./logos/s-work3.png"
title.textContent = "Turbo Levo Pro"
}else{
img.src=ele.img
title.textContent=ele.productdescriptionname
}
let price = document.createElement("p")
price.textContent= `€ ${ele.price}`
price.setAttribute("id","price")
if(ele.mrp){
let mrp = document.createElement("p")
mrp.textContent= ele.mrp
mrp.style.textDecoration = "line-through"
}
let button_box = document.createElement("div")
let wish_btn =document.createElement("button")
wish_btn.innerHTML=`<i class="fa-regular fa-heart fa-lg"></i>`
wish_btn.style.color="black"
wish_btn.addEventListener("click",()=>{
if(wish_btn.style.color=="black"){
wish_btn.style.color="red"
wishArr.push(ele)
localStorage.setItem("wishlistitem",JSON.stringify(wishArr))
}else if(wish_btn.style.color=="red"){
wish_btn.style.color="black"
wishArr.splice(i,1);
localStorage.setItem("wishlistitem",JSON.stringify(wishArr))
}
})
let compare_btn = document.createElement("button")
compare_btn.innerHTML=`<i class="fa-solid fa-code-compare"></i>`
compare_btn.style.color="black"
compare_btn.addEventListener("click",()=>{
compare_btn.style.color= compare_btn.style.color=="black"? "green":"black"
})
button_box.append(wish_btn,compare_btn)
box.append(img,title,price,button_box)
if(i<3){
document.querySelector(".h_bcycles").append(box)
}else if(i==29 || i>10&&i<13) {
document.querySelector("#bikesDis").append(box)
}
})
}
// slider
let slider = document.getElementsByName("slider")
let slide_c=0
setInterval(()=>{
slider[slide_c].checked=true
if(slide_c==slider.length-1){
slide_c=0;
}else{
slide_c++;
}
},4000)
// _____________Search_box____________
document.getElementById("search").addEventListener("keyup",search)
let timer
function search(event){
clearTimeout(timer)
let query =document.getElementById("search").value
console.log(event.key);
timer = setTimeout(async ()=>{
// console.log(query);
let data =await fetchdata(`https://specialized.onrender.com/products?q=${query}`)
console.log(data);
suggetion_box(data)
if(event.key=="Enter")
location.href="category/category.html"
localStorage.setItem("product-list",JSON.stringify(data))
},400)
}
// __________suggetion box__________-
function suggetion_box(data){
document.getElementById("sugge_box").textContent=""
document.getElementById("sugge_box").style.display="block"
let boxe = document.createElement("div")
boxe.textContent= "No result!"
data.length? data.map(function(ele,i){
if(i<=5){
let box = document.createElement("div")
box.textContent= ele.productdescriptionname
box.addEventListener("click",()=>{
localStorage.setItem("item",ele.id)
window.location.href="itempage.html"
})
document.getElementById("sugge_box").append(box)
}
}) : document.getElementById("sugge_box").append(boxe)
}
document.querySelector("body").addEventListener("click",()=>{
document.getElementById("sugge_box").style.display="none"
})
function notification(){
let show = document.getElementById("notification")
if(show)
show.setAttribute("id","notification_pop")
}
function logout() {
localStorage.clear("logged");
window.location.href="index.html"
}