Skip to content

Commit 0b01bff

Browse files
authored
Update main.js
1 parent 43bf309 commit 0b01bff

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

main.js

+40-13
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,16 @@ const createFooter = () => {
371371

372372
createFooter()
373373

374+
// Función para mostrar un mensaje cuando no se encuentren productos
375+
const noProductsMessage = () => {
376+
const message = document.createElement('p')
377+
message.textContent =
378+
'NO SE ENCONTRARÓN PRODUCTOS QUE COINCIDAN CON LA BÚSQUEDA.'
379+
message.id = 'noProductsMsg'
380+
cardsArticle$$.innerHTML = ''
381+
cardsArticle$$.appendChild(message)
382+
}
383+
374384
// Función para restablecer los filtros
375385
const resetFilters = () => {
376386
createProducts(products)
@@ -385,31 +395,41 @@ const filterBySelect = (e) => {
385395
const productByType = CopyProducts.filter((product) => {
386396
return product.type === selected
387397
})
388-
createProducts(productByType)
398+
if (productByType.length === 0) {
399+
noProductsMessage()
400+
} else {
401+
createProducts(productByType)
402+
}
389403
}
390404

391405
// Función para filtrar por marca ingresada en el input
392406
const filterByBrandInput = () => {
393407
const CopyProducts = [...products]
394408
const selectedType = document.querySelector('#select').value
395409
const selectedBrand = document.querySelector('#brandInput').value
396-
397410
if (selectedType !== '') {
398411
const productByType = CopyProducts.filter((product) => {
399412
return (
400413
product.type === selectedType &&
401414
(selectedBrand === '' || product.brand === selectedBrand)
402415
)
403416
})
404-
405-
cardsArticle$$.innerHTML = ''
406-
createProducts(productByType)
417+
if (productByType.length === 0) {
418+
noProductsMessage()
419+
} else {
420+
cardsArticle$$.innerHTML = ''
421+
createProducts(productByType)
422+
}
407423
} else {
408424
const productByBrand = CopyProducts.filter((product) => {
409425
return product.brand === selectedBrand
410426
})
411-
cardsArticle$$.innerHTML = ''
412-
createProducts(productByBrand)
427+
if (productByBrand.length === 0) {
428+
noProductsMessage()
429+
} else {
430+
cardsArticle$$.innerHTML = ''
431+
createProducts(productByBrand)
432+
}
413433
}
414434
}
415435

@@ -424,7 +444,6 @@ const filterByAll = () => {
424444
const CopyProducts = [...products]
425445
const selectedType = document.querySelector('#select').value
426446
const selectedBrand = document.querySelector('#brandInput').value
427-
428447
if (selectedType !== '') {
429448
const productByPriceAndType = CopyProducts.filter((product) => {
430449
return (
@@ -433,18 +452,25 @@ const filterByAll = () => {
433452
(selectedBrand === '' || product.brand === selectedBrand)
434453
)
435454
})
436-
437-
cardsArticle$$.innerHTML = ''
438-
createProducts(productByPriceAndType)
455+
if (productByPriceAndType.length === 0) {
456+
noProductsMessage()
457+
} else {
458+
cardsArticle$$.innerHTML = ''
459+
createProducts(productByPriceAndType)
460+
}
439461
} else {
440462
const productByPrice = CopyProducts.filter((product) => {
441463
return (
442464
product.price <= inputValue &&
443465
(selectedBrand === '' || product.brand === selectedBrand)
444466
)
445467
})
446-
cardsArticle$$.innerHTML = ''
447-
createProducts(productByPrice)
468+
if (productByPrice.length === 0) {
469+
noProductsMessage()
470+
} else {
471+
cardsArticle$$.innerHTML = ''
472+
createProducts(productByPrice)
473+
}
448474
}
449475
}
450476

@@ -453,6 +479,7 @@ const selectOpt = document.querySelector('#select')
453479
const brandInput$$ = document.querySelector('#brandInput')
454480
const filterButton = document.querySelector('#priceFilter')
455481
const resetButton = document.querySelector('#resetButton')
482+
456483
selectOpt.addEventListener('change', filterBySelect)
457484
brandInput$$.addEventListener('input', filterByBrandInput)
458485
filterButton.addEventListener('click', filterByAll)

0 commit comments

Comments
 (0)