-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathproduct1.js
202 lines (152 loc) · 4.77 KB
/
product1.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// this is for show userName in navbar and signout functionality
const loginName = document.querySelector("#PUname");
const PrUserName = sessionStorage.getItem('c4raUser') || "LogIn";
loginName.innerHTML = ' ' + PrUserName;
let cartproducts = JSON.parse(localStorage.getItem("cart")) || [];
// Fetching api from the JSON Server :-
// append spinner
// let productgrid=document.getElementsByClassName("container");
function showdata() {
document.querySelector(".container").innerHTML = `
<div id="loading" style=" background-color:white; height:100px; width:100%"; >
<img src="./Spinner-5.gif" alt="error">
<p>Please wait page is loading ...</p>
</div>
`
}
let bag = [];
// let url="https://fakestoreapi.com/products";
let url = "https://636f9027f2ed5cb047e01947.mockapi.io/Project_2_Products";
fetch(url)
.then((res) => res.json())
.then((data) => {
bag = data;
showdata()
// console.log(data);
setTimeout(() => {
displayCard(data);
}, 1000)
});
// Sorting functionaly
function handleSort() {
let selected = document.querySelector("select").value;
if (selected == "LTH") {
bag.sort((a, b) => a.price - b.price);
}
if (selected == "HTL") {
bag.sort((a, b) => b.price - a.price);
}
console.log(bag);
displayCard(bag);
}
// Filter Functionality
function filterByCategory() {
let selected = document.querySelector("#filtered").value;
console.log(selected)
if (selected == "ALL") {
displayCard(bag);
} else {
let filteredData = bag.filter(function (elem) {
return elem.category == selected;
});
console.log(filteredData)
displayCard(filteredData);
}
}
// Search functionality
function search() {
let q = document.querySelector("#Search").value;
console.log(q);
let newData = bag.filter(function (elem) {
return elem.title.toLowerCase().includes(q.toLowerCase());
});
console.log(newData);
displayCard(newData);
}
search()
// appending on DOM
function displayCard(data) {
document.querySelector(".container").innerHTML = "";
data.forEach(elem => {
let div = document.createElement("div");
let imageProduct = document.createElement("img");
imageProduct.setAttribute("src", elem.image)
let Category = document.createElement("h3");
Category.innerText = elem.category;
let title = document.createElement("p");
title.innerText = elem.title.substring(0, 20);
let Price = document.createElement("h4");
Price.innerText = "Rs." + elem.price;
let btn = document.createElement("button");
btn.className = "show-modal"; // className added
btn.innerText = `Add to cart `
btn.addEventListener("click", function () {
cartproducts.push(elem);
localStorage.setItem("cart", JSON.stringify(cartproducts));
// alert("Product added to cart");
showfeed()
//add to user data for cart page
addToCart(elem)
});
div.append(imageProduct, Category, title, Price, btn);
document.querySelector(".container").append(div);
});
}
// feedback functionality - day-3
const section = document.querySelector("section"),
showBtn = document.querySelector(".show-modal"),
overlay = document.querySelector(".overlay"),
closeBtn = document.querySelector(".close-btn");
function addtoBag() {
}
console.log(showBtn)
showBtn.addEventListener("click", showfeed);
function showfeed() {
section.classList.add("active")
}
overlay.addEventListener("click", addedtoBag);
function addedtoBag() {
section.classList.remove("active");
// window.location.href="cart.html";
}
closeBtn.addEventListener("click", cancel);
function cancel() {
section.classList.remove("active");
// window.location.href="cart.html";
}
// drop down functionalities
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function dropdownBrand() {
document.getElementById("Brand").classList.toggle("show");
}
function dropdownGender() {
document.getElementById("Gender").classList.toggle("show");
}
// sticky navbar Properties
window.onscroll = function () { myFunction() };
var navbar = document.querySelector(".top-nav");
var sticky = navbar.offsetTop;
function myFunction() {
if (window.pageYOffset >= sticky) {
navbar.classList.add("sticky")
} else {
navbar.classList.remove("sticky");
}
}
///add to cart functionality
function addToCart(item) {
let larr = JSON.parse(localStorage.getItem("userdata")) || [];
}
//***************************** */
//local storage usage for cart page
function addToCart(item) {
let larr = JSON.parse(localStorage.getItem("userdata"));
//***************************** */
console.log(item)
item["count"] = 1;
larr.cartItems.push(item)
console.log(JSON.stringify(larr, null, 2))
//***************************** */
localStorage.setItem("userdata", JSON.stringify(larr))
}