@@ -371,6 +371,16 @@ const createFooter = () => {
371
371
372
372
createFooter ( )
373
373
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
+
374
384
// Función para restablecer los filtros
375
385
const resetFilters = ( ) => {
376
386
createProducts ( products )
@@ -385,31 +395,41 @@ const filterBySelect = (e) => {
385
395
const productByType = CopyProducts . filter ( ( product ) => {
386
396
return product . type === selected
387
397
} )
388
- createProducts ( productByType )
398
+ if ( productByType . length === 0 ) {
399
+ noProductsMessage ( )
400
+ } else {
401
+ createProducts ( productByType )
402
+ }
389
403
}
390
404
391
405
// Función para filtrar por marca ingresada en el input
392
406
const filterByBrandInput = ( ) => {
393
407
const CopyProducts = [ ...products ]
394
408
const selectedType = document . querySelector ( '#select' ) . value
395
409
const selectedBrand = document . querySelector ( '#brandInput' ) . value
396
-
397
410
if ( selectedType !== '' ) {
398
411
const productByType = CopyProducts . filter ( ( product ) => {
399
412
return (
400
413
product . type === selectedType &&
401
414
( selectedBrand === '' || product . brand === selectedBrand )
402
415
)
403
416
} )
404
-
405
- cardsArticle$$ . innerHTML = ''
406
- createProducts ( productByType )
417
+ if ( productByType . length === 0 ) {
418
+ noProductsMessage ( )
419
+ } else {
420
+ cardsArticle$$ . innerHTML = ''
421
+ createProducts ( productByType )
422
+ }
407
423
} else {
408
424
const productByBrand = CopyProducts . filter ( ( product ) => {
409
425
return product . brand === selectedBrand
410
426
} )
411
- cardsArticle$$ . innerHTML = ''
412
- createProducts ( productByBrand )
427
+ if ( productByBrand . length === 0 ) {
428
+ noProductsMessage ( )
429
+ } else {
430
+ cardsArticle$$ . innerHTML = ''
431
+ createProducts ( productByBrand )
432
+ }
413
433
}
414
434
}
415
435
@@ -424,7 +444,6 @@ const filterByAll = () => {
424
444
const CopyProducts = [ ...products ]
425
445
const selectedType = document . querySelector ( '#select' ) . value
426
446
const selectedBrand = document . querySelector ( '#brandInput' ) . value
427
-
428
447
if ( selectedType !== '' ) {
429
448
const productByPriceAndType = CopyProducts . filter ( ( product ) => {
430
449
return (
@@ -433,18 +452,25 @@ const filterByAll = () => {
433
452
( selectedBrand === '' || product . brand === selectedBrand )
434
453
)
435
454
} )
436
-
437
- cardsArticle$$ . innerHTML = ''
438
- createProducts ( productByPriceAndType )
455
+ if ( productByPriceAndType . length === 0 ) {
456
+ noProductsMessage ( )
457
+ } else {
458
+ cardsArticle$$ . innerHTML = ''
459
+ createProducts ( productByPriceAndType )
460
+ }
439
461
} else {
440
462
const productByPrice = CopyProducts . filter ( ( product ) => {
441
463
return (
442
464
product . price <= inputValue &&
443
465
( selectedBrand === '' || product . brand === selectedBrand )
444
466
)
445
467
} )
446
- cardsArticle$$ . innerHTML = ''
447
- createProducts ( productByPrice )
468
+ if ( productByPrice . length === 0 ) {
469
+ noProductsMessage ( )
470
+ } else {
471
+ cardsArticle$$ . innerHTML = ''
472
+ createProducts ( productByPrice )
473
+ }
448
474
}
449
475
}
450
476
@@ -453,6 +479,7 @@ const selectOpt = document.querySelector('#select')
453
479
const brandInput$$ = document . querySelector ( '#brandInput' )
454
480
const filterButton = document . querySelector ( '#priceFilter' )
455
481
const resetButton = document . querySelector ( '#resetButton' )
482
+
456
483
selectOpt . addEventListener ( 'change' , filterBySelect )
457
484
brandInput$$ . addEventListener ( 'input' , filterByBrandInput )
458
485
filterButton . addEventListener ( 'click' , filterByAll )
0 commit comments