Repository navigation
Expand file tree
/
Copy pathinventory.js
More file actions
327 lines (210 loc) · 8.43 KB
/
Copy pathinventory.js
File metadata and controls
327 lines (210 loc) · 8.43 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
const inventoryPage = document.querySelector('#inventory-page')
const searchBox = document.querySelector('#search-box')
const brandCheckBoxes = document.querySelectorAll('.brand-filters>input[type=checkbox]')
const categoryCheckBoxes = document.querySelectorAll('.category-filters>input[type=checkbox]')
const searchBTN = document.querySelector('#search-btn')
const resetBTN = document.querySelector('#reset')
const topBrandFilter = document.querySelector('#top-brands')
const topCategoryFilter = document.querySelector('#top-categories')
const topPriceFilter = document.querySelector('#top-prices')
const allBrandsCheckBox = document.querySelector('input[value="All brands"]')
const allCategoriesCheckBox = document.querySelector('input[value="All categories"]')
console.log(allBrandsCheckBox)
const showCarsContainer = document.querySelector('#show-cars')
const carCards = (list) => {
showCarsContainer.innerHTML = ''
list.forEach((car) => {
const galleryCard =
`
<div class="gallery-cards" id="${car.name}">
<div class="image-container"><img src="./Sorces/pngimages/${car.image}" alt=""></div>
<div class="car-description">
<h4> ${car.name}</h4>
<p>${car.year}</p>
<h3>${car.price}</h3>
<div class="extraCar-info">
<span>
<i class="fa-solid fa-gas-pump"></i>
<p>${car.fuel}</p>
</span>
<span>
<i class="fa-sharp fa-regular fa-transmission"></i>
<p>${car.gear}</p>
</span>
<span>
<i class="fa-etch fa-solid fa-gauge"></i>
<p>${car.KM}</p>
</span>
</div>
</div>
</div>
`
showCarsContainer.innerHTML += galleryCard
})
}
carCards(carsData)
const inventoryCards = document.querySelectorAll('.gallery-cards')
showCarsContainer.addEventListener('click', (event) => {
const clickedCard = event.target.closest('.gallery-cards')
if (clickedCard) {
// console.log(localStorage.getItem('currentUser'))
if (localStorage.getItem('currentUser') === null) {
let singing = confirm('⚠ please sign up or login to set order!')
if(singing) {
inventoryPage.style.display = 'none'
signPage.style.display = 'flex'
}
return;
}
let cardName = clickedCard.id
let selectedCar = carsData.find(car => car.name === cardName)
console.log(selectedCar)
if (selectedCar) {
orderPage.innerHTML = `
<section id="orderCar">
<img src="./Sorces/pngimages/${selectedCar.image}" alt="">
<h3>${selectedCar.brand} ${selectedCar.name}</h3>
<button id="orderBTN">Set Order</button>
</section>
<section id="carDetails">
<div class="orderTitle">
<h1>${selectedCar.name}</h1>
<p>${selectedCar.year}</p>
</div>
<div class="orderTitle" id="orderPrice">
<h4>${selectedCar.price}</h4>
</div>
<div class="orderInfo">
<h4>Engine</h4>
<p>${selectedCar.Engine}</p>
</div>
<div class="orderInfo">
<h4>Transmission</h4>
<p> ${selectedCar.gear}</p>
</div>
<div class="orderInfo">
<h4>fuel Type</h4>
<p>${selectedCar.fuel}</p>
</div>
<div class="orderInfo">
<h4>Mileage</h4>
<p>${selectedCar.KM}</p>
</div>
</section>
`
const orderBtn = document.querySelector('#orderBTN')
orderBtn.addEventListener('click', () => {
alert(`Success! your order for: " ${selectedCar.brand} ${selectedCar.name}" has been registered. Our team will contact you soon!`)
})
}
inventoryPage.style.display = 'none'
orderPage.style.display = 'flex'
}
})
const categoryList = ['All categories', 'SUV', 'Sedan', 'Coupe']
const getUniqeFilters = () => {
const brands = ['All brands', ...new Set(carsData.map(car => car.brand))]
console.log(brands)
return brands
}
const selectOptionsRender = () => {
const uniqeBrands = getUniqeFilters()
topBrandFilter.innerHTML = ''
uniqeBrands.forEach((brand) => {
topBrandFilter.innerHTML += `<option>${brand}</option>`
})
topCategoryFilter.innerHTML = ''
categoryList.forEach((category) => {
topCategoryFilter.innerHTML += `<option>${category}</option>`
})
}
selectOptionsRender()
const filter = () => {
let currentBrand = topBrandFilter.value
let currentCategory = topCategoryFilter.value
let currentPrice = topPriceFilter.value
let result = carsData.filter((car) => {
let isBrandMatch = (currentBrand == 'All brands') || (currentBrand == car.brand)
let isCategoryMatch = (currentCategory == 'All categories') || (currentCategory == car.category)
return isBrandMatch && isCategoryMatch
})
if (currentPrice === 'Max Price') {
result.sort((a, b) => {
let PriceA = Number(a.price.replaceAll('$', ''))
let PriceB = Number(b.price.replaceAll('$', ''))
return PriceB - PriceA
})
}
else if (currentPrice === 'Min Price') {
result.sort((a, b) => {
let PriceA = Number(a.price.replaceAll('$', ''))
let PriceB = Number(b.price.replaceAll('$', ''))
return PriceA - PriceB
})
}
carCards(result)
}
let sideCheckedBoxes = () => {
let checkedBrandFilters = []
brandCheckBoxes.forEach((checkbox) => {
if (checkbox.checked == true) {
checkedBrandFilters.push(checkbox.value)
}
})
// console.log(checkedBrandFilters)
let checkedCategoryFilters = []
categoryCheckBoxes.forEach((checkbox) => {
if (checkbox.checked == true) {
checkedCategoryFilters.push(checkbox.value)
}
})
// console.log(checkedCategoryFilters)
if (checkedBrandFilters.length == 0) {
allBrandsCheckBox.checked = true
}
else {
allBrandsCheckBox.checked = false
}
if (checkedCategoryFilters.length == 0) {
allCategoriesCheckBox.checked = true
}
else {
allCategoriesCheckBox.checked = false
}
let result = carsData.filter((car) => {
let brandMatch = (checkedBrandFilters.length === 0) || (checkedBrandFilters.includes(car.brand))
let categoryMatch = (checkedCategoryFilters.length == 0) || (checkedCategoryFilters.includes(car.category))
return brandMatch && categoryMatch
})
carCards(result)
}
brandCheckBoxes.forEach((checkbox) => {
checkbox.addEventListener('change', () => {
sideCheckedBoxes()
})
})
categoryCheckBoxes.forEach((checkbox) => {
checkbox.addEventListener('change', () => {
sideCheckedBoxes()
})
})
resetBTN.addEventListener('click', () => {
topBrandFilter.value = 'All brands'
topCategoryFilter.value = 'All categories'
topPriceFilter.value = 'All Prices'
brandCheckBoxes.forEach(checkbox => checkbox.checked = false)
categoryCheckBoxes.forEach(checkbox => checkbox.checked = false)
allBrandsCheckBox.checked = true
allCategoriesCheckBox.checked = true
carCards(carsData)
})
topBrandFilter.addEventListener('change', filter)
topCategoryFilter.addEventListener('change', filter)
topPriceFilter.addEventListener('change', filter)
carCards(carsData)
getUniqeFilters()
let showMessage = () => {
alert('This option will be available in the next version! please use from sideBar or top filters for now.')
}
searchBox.addEventListener('click', showMessage)
searchBTN.addEventListener('click', showMessage)