@@ -15,12 +15,10 @@ class Photo {
15
15
Object . assign ( this , json ) ;
16
16
}
17
17
showModal ( ) {
18
- galleryItem . addEventListener ( 'click' , ( ) => {
19
- Swal . fire ( {
20
- imageUrl : this . src ,
21
- imageHeight : 500 ,
22
- imageAlt : this . alt
23
- } ) ;
18
+ Swal . fire ( {
19
+ imageUrl : this . src ,
20
+ imageHeight : 500 ,
21
+ imageAlt : this . alt
24
22
} ) ;
25
23
}
26
24
}
@@ -81,6 +79,7 @@ const createNewGalleryItem = (photoObject) => {
81
79
let newGalleryItem = document . createElement ( 'article' ) ;
82
80
// Le asignamos la clase que define los estilos que poseerán las fotos.
83
81
newGalleryItem . classList . add ( 'gallery__item' ) ;
82
+ newGalleryItem . setAttribute ( 'photoId' , `${ photo . id } ` )
84
83
// Y le ponemos una etiqueta img con los datos del objeto para que se muestren.
85
84
newGalleryItem . innerHTML = `<img src="${ photo . src } " alt="${ photo . alt } "></img>` ;
86
85
// Y por último le agregamos ese nuevo elemento a la galería. Utilicé el 'prepend' ya que quiero que se agregue al principio, para respetar el orden que simulo en el array.
@@ -97,7 +96,10 @@ const filterPhotos = (search) => {
97
96
*/
98
97
if ( filteredPhotos . length === 0 ) {
99
98
// Mostramos que ocurrió un error mediante un h2.
100
- gallery . innerHTML = `<h2>No se pudo encontrar ninguna foto relacionada a "${ search } ".</h2>` ;
99
+ let h2 = document . createElement ( 'h2' )
100
+ h2 . innerText = `No se pudo encontrar ninguna foto relacionada a "${ search } ".` ;
101
+ gallery . insertBefore ( h2 , galleryContainer ) ;
102
+ removeAllChild ( galleryContainer )
101
103
} else {
102
104
// Eliminamos todas las fotos de la gelería.
103
105
removeAllChild ( galleryContainer ) ;
@@ -125,9 +127,13 @@ searchForm.addEventListener('submit', (e) => {
125
127
// Botón para limpiar el input del form.
126
128
clearInputButton . addEventListener ( 'click' , ( ) => {
127
129
document . getElementById ( 'searchInput' ) . value = '' ;
128
- removeAllChild ( galleryContainer ) ;
129
- getInitialRandomPhotos ( apiPhotos ) ;
130
- errorAlert . innerText = '' ;
130
+ if ( gallery . childNodes . length == 3 ) {
131
+ removeAllChild ( galleryContainer ) ;
132
+ getInitialRandomPhotos ( apiPhotos ) ;
133
+ } else {
134
+ gallery . childNodes [ 1 ] . remove ( ) ;
135
+ getInitialRandomPhotos ( apiPhotos ) ;
136
+ }
131
137
} ) ;
132
138
133
139
@@ -146,12 +152,15 @@ const getSearchedPhotos = (e) => {
146
152
errorAlert . innerText = '' ;
147
153
} else {
148
154
// Cuando sea 'false', avisamos el error.
149
- errorAlert . innerText = 'Debe introducir una palabra mayor a 3 letras' ;
155
+ errorAlert . innerText = 'Debe introducir una palabra mayor a 3 letras.' ;
156
+ setTimeout ( ( ) => {
157
+ errorAlert . innerText = '' ;
158
+ } , 1500 ) ;
150
159
}
151
160
}
152
161
153
162
// Cuando se carge la ventana, llamamos a la función que nos carga 8 fotos simulando que son fotos aleatorias.
154
- window . addEventListener ( 'load ' , ( ) => {
163
+ window . addEventListener ( 'DOMContentLoaded ' , ( ) => {
155
164
getInitialRandomPhotos ( apiPhotos ) ;
156
165
} ) ;
157
166
0 commit comments