-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllproducts.js
230 lines (184 loc) · 9.53 KB
/
Allproducts.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
const all = document.getElementById('all-productss');
const searchinput = document.getElementById('searching');
const singleProductContentId = document.getElementById('singlecontantid');
const cart = document.getElementById('cart');
const imgContent = document.getElementById('img-content');
const closeIcon=document.createElement('i');
closeIcon.classList.add('fas', 'fa-times');
closeIcon.classList.add('close-icon');
closeIcon.style.cursor = 'pointer';
let output = [];
searchinput.addEventListener("input", (event) => {
const value = event.target.value.toLowerCase();
output.forEach(matches => {
const isVisible = matches.product_name.toLowerCase().includes(value);
matches.Element.classList.toggle("hide", !isVisible);
});
});
fetch('https://dummyjson.com/products')
.then(res => res.json())
.then(data => {
const allproductss = data.products;
output = allproductss.map(product => {
const allproductDiv = document.createElement('div');
allproductDiv.className = 'productBox';
all.appendChild(allproductDiv);
const allimg = document.createElement('img');
allimg.src = product.thumbnail;
allimg.alt = product.title;
allproductDiv.appendChild(allimg);
const alltitle = document.createElement('h3');
alltitle.className = 'title';
alltitle.textContent = product.title;
allproductDiv.appendChild(alltitle);
const allprice = document.createElement('p');
allprice.className = 'price';
allprice.textContent = `$${product.price}`;
const addToCart = document.createElement('button');
addToCart.id = "addToCartAllproducts"
addToCart.textContent = 'Add to Cart';
addToCart.classList.add('addtocartButton');
const productDetais = document.createElement('button');
productDetais.id = "addToCartAllproducts"
productDetais.textContent = 'See More';
productDetais.classList.add('addtocartButton');
const cartObj = {
img: product.thumbnail,
title: product.title,
price: product.price,
rating: product.rating,
description: product.description
}
addToCart.addEventListener('click', () => {
cart.style.display = "block";
// Create card data element
const cardData = document.createElement('div');
cardData.classList.add('cart-item'); // Add a class for styling
cardData.innerHTML = `
<img src="${cartObj.img}" alt="${cartObj.title}" />
<div>
<h4>${cartObj.title}</h4>
<p>$${cartObj.price}</p>
<p>Rating: ${cartObj.rating}</p>
<p>${cartObj.description}</p>
</div>
<div class="containerOfTrash">
<i class="fas fa-trash"></i>
</div>
`;
singleProductContentId.appendChild(cardData);
singleProductContentId.appendChild(closeIcon);
// Add event listener to the trash icon
const trashIcon = cardData.querySelector('.fas.fa-trash');
trashIcon.addEventListener('click', () => {
cardData.remove();
});
closeIcon.style.color='white';
closeIcon.fontSize='19px';
closeIcon.addEventListener('click',() => {
const cart=document.querySelector('.cart');
cart.style.display='none'
})
});
allproductDiv.appendChild(allprice);
allproductDiv.appendChild(addToCart);
allproductDiv.appendChild(productDetais);
// Display the mini cart for a single product.
productDetais.addEventListener('click', () => {
fetch(`https://dummyjson.com/products/${product.id}`)
.then(res => res.json())
.then(data => {
// create div container (parent)
const popUpElementContainer = document.createElement('div');
popUpElementContainer.classList.add('popUpElementContainerStyle');
// create div container (child)
const popUpElement = document.createElement('div');
popUpElement.classList.add('popElementStyle');
popUpElementContainer.appendChild(popUpElement);
// create div singleProduct
const singleProductImgContainer = document.createElement('div');
singleProductImgContainer.classList.add('single-product-container');
// create img
const singleProductImg = document.createElement('img');
singleProductImg.setAttribute("src", `${data.images[0]}`);
singleProductImgContainer.appendChild(singleProductImg);
// create div for data of image
const singleProductDetailsContainer = document.createElement('div');
// create h2 title
const singleProductTitle=document.createElement('h2');
const singleProductPrice = document.createElement('h2');
singleProductPrice.className = "price";
singleProductTitle.innerHTML=data.title;
singleProductPrice.innerHTML = `Price : ${data.price}$`;
const rating=document.createElement('h2');
rating.className="Popuprating";
rating.innerHTML=`Rating : ${data.rating}`;
singleProductDetailsContainer.appendChild(singleProductTitle);
singleProductDetailsContainer.appendChild(singleProductPrice);
singleProductDetailsContainer.appendChild(rating);
popUpElement.appendChild(singleProductImgContainer);
popUpElement.appendChild(singleProductDetailsContainer);
// create h1 see more
const SeeMoreicon = document.createElement('h1');
SeeMoreicon.innerHTML = `View Details <i class="fa-solid fa-arrow-right rightArrow"></i>`;
SeeMoreicon.className = "seeMore";
popUpElement.appendChild(closeIcon);
popUpElement.appendChild(SeeMoreicon);
SeeMoreicon.addEventListener('click', () => {
window.location.href = `singleProduct.html?id=${data.id}`;
});
closeIcon.addEventListener('click',() => {
popUpElement.style.display="none";
popUpElementContainer.style.display="none";
})
document.body.appendChild(popUpElementContainer);
});
});
return { product_img: product.thumbnail,product_category:product.category, product_name: product.title, product_price: `$${product.price}`, Element: allproductDiv };
});
const b = document.querySelector('.cbeauty');
b.addEventListener('click', () => {
output.forEach(product => {
const isVisible = (product.product_category === 'beauty');
product.Element.classList.toggle('hide', !isVisible);
});
});
const f = document.querySelector('.cFragrance');
f.addEventListener('click', () => {
output.forEach(product => {
const isVisible = (product.product_category === 'fragrances');
product.Element.classList.toggle('hide', !isVisible);
});
});
const fu = document.querySelector('.cFurniture');
fu.addEventListener('click', () => {
output.forEach(product => {
const isVisible = (product.product_category === 'furniture');
product.Element.classList.toggle('hide', !isVisible);
});
});
const g= document.querySelector('.cGroceries');
g.addEventListener('click', () => {
output.forEach(product => {
const isVisible = (product.product_category === 'groceries');
product.Element.classList.toggle('hide', !isVisible);
});
});
})
.catch(error => console.error('Error fetching data:', error));
function showSidebar(){
const Sidebar =document.querySelector('.sidebar');
Sidebar.style.display='flex'
}
function hideSidebar(){
const Sidebar =document.querySelector('.sidebar');
Sidebar.style.display='none'
}
function displaycat(){
const Sidebar =document.querySelector('.dropdown1');
Sidebar.style.display='block'
}
function displaycat2(){
const Sidebar =document.querySelector('.dropdown1-1');
Sidebar.style.display='block'
}