@@ -15,30 +15,45 @@ export function notificationsController(container) { // container is ".notificat
1515 newNotification . classList . add ( type ) // <-- Here we add the class of the container we pass to "type".
1616 newNotification . innerHTML = buildNotification ( message , type ) // <-- If "type" is not added, the default will be “error”.
1717
18- container . appendChild ( newNotification ) // <-- Here we insert our HTML ready to use.
19-
2018 const closeButton = newNotification . querySelector ( "button" ) ; // <-- It will select only the button element of the container that we pass it.
2119 closeButton . addEventListener ( "click" , ( ) => {
2220 removeNotification ( newNotification ) ;
2321 } ) ;
2422
23+ container . appendChild ( newNotification ) // <-- Here we insert our HTML ready to use.
24+
25+ // Obtener todas las notificaciones actuales después de agregar la nueva
26+ let currentNotifications = Array . from ( container . querySelectorAll ( '.notification' ) ) ;
27+
28+ // Si hay más de 3, elimina las más antiguas
29+ while ( currentNotifications . length > 3 ) {
30+ const oldest = currentNotifications . shift ( ) ; // remueve la primera (más vieja)
31+ removeNotification ( oldest ) ;
32+ }
33+
34+ // Control de duración dependiendo del tipo
2535 if ( type === 'error' ) {
26- // Searches for all error notifications, excluding the new one.
27- const oldErrors = Array . from ( container . querySelectorAll ( '.notification.error' ) )
28- . filter ( n => n !== newNotification ) ;
36+ // Errores antiguos (excepto el nuevo)
37+ const oldErrors = currentNotifications . filter ( n =>
38+ n !== newNotification && n . classList . contains ( 'error' )
39+ ) ;
2940
30- // The previous ones are automatically deleted.
41+ // Los errores antiguos duran 1 segundo
3142 oldErrors . forEach ( n => {
32- setTimeout ( ( ) => removeNotification ( n ) , 500 ) ;
43+ setTimeout ( ( ) => removeNotification ( n ) , 1000 ) ;
3344 } ) ;
3445
35- // The new (most recent) one is not automatically deleted.
46+ // El nuevo error dura 5 segundos
47+ setTimeout ( ( ) => {
48+ removeNotification ( newNotification ) ;
49+ } , 5000 ) ;
50+ } else {
51+ // Notificaciones que no son de tipo 'error' también desaparecen a los 5s
52+ setTimeout ( ( ) => {
53+ removeNotification ( newNotification ) ;
54+ } , 5000 ) ;
3655 }
37-
38- if ( type !== 'error' ) {
39- setTimeout ( ( ) => { removeNotification ( newNotification ) } , 5000 ) ;
40- }
41- }
56+ } ;
4257
4358 return {
4459 showNotification
0 commit comments