@@ -29,6 +29,7 @@ const HEADERS = {
29
29
}
30
30
}
31
31
32
+ // Definimos los elementos del DOM que necesitaremos.
32
33
let loadMoreBtn = document . getElementById ( 'loadMoreBtn' ) ;
33
34
let errorAlert = document . getElementById ( 'errorAlert' ) ;
34
35
let clearInputButton = document . getElementById ( 'clearInput' ) ;
@@ -45,10 +46,10 @@ const removeAllChild = (parent) => {
45
46
}
46
47
}
47
48
49
+ // Definimos algunas variables globales que necesitaremos.
48
50
let pageIndex = 1 ;
49
51
let searchValue ;
50
52
51
-
52
53
// Creamos una función que nos permita realizar el fetch a partir de una URL y retornar la 'data' para asi trabajarla como querramos.
53
54
const fetchPhotos = async ( baseURL ) => {
54
55
const res = await fetch ( baseURL , HEADERS ) ;
@@ -104,28 +105,40 @@ const validateErrors = (data, searchValue) => {
104
105
// Creamos una función que nos devuelva las fotos a partir de lo ingresado por el usuario en la barra de búsqueda.
105
106
const getSearchedPhotos = async ( e ) => {
106
107
e . preventDefault ( ) ;
108
+ // Capturamos el valor del input mediante el evento.
107
109
searchValue = e . target . querySelector ( 'input' ) . value ;
110
+ // Y lo validamos.
108
111
if ( validateSearchValue ( searchValue ) === true ) {
112
+ // Realizamos el fetch de la petición.
109
113
const data = await fetchPhotos ( `https://api.pexels.com/v1/search?query=${ searchValue } &per_page=16` ) ;
114
+ // Validamos los errores si los hay.
110
115
validateErrors ( data , searchValue ) ;
116
+ // Vaciamos el contenedor de la galería.
111
117
removeAllChild ( galleryContainer ) ;
118
+ // Cargamos los nuevos items de la galería.
112
119
generateHTML ( data , 'search' ) ;
113
120
}
114
121
}
115
122
116
123
// Creamos una función que nos devuelva otras páginas de fotos respecto de la búsqueda anterior.
117
124
const getMoreSearchedPhotos = async ( index ) => {
125
+ // Realizamos el fetch de la petición.
118
126
const data = await fetchPhotos ( `https://api.pexels.com/v1/search?query=${ searchValue } &per_page=16&page=${ index } ` ) ;
127
+ // Validamos los errores si los hay.
119
128
validateErrors ( data , searchValue ) ;
129
+ // Cargamos los nuevos items de la galería.
120
130
generateHTML ( data , 'search' ) ;
121
131
}
122
132
123
133
// Creamos una función que nos devuelva fotos aleatorias.
124
134
const getInitialRandomPhotos = async ( index ) => {
135
+ // Realizamos el fetch de la petición.
125
136
const data = await fetchPhotos ( `https://api.pexels.com/v1/curated?per_page=16&page=${ index } ` ) ;
137
+ // Verificamos si no tiene otra página de paginación para no mostrar más el botón.
126
138
if ( ! data . next_page ) {
127
139
loadMoreBtn . style . display = 'none'
128
140
}
141
+ // Cargamos los nuevos items de la galería.
129
142
generateHTML ( data , 'curated' ) ;
130
143
}
131
144
@@ -134,49 +147,54 @@ const loadMorePhotos = () => {
134
147
let index = ++ pageIndex ;
135
148
let galleryItem = document . querySelector ( 'article' ) ;
136
149
let dataPhoto = galleryItem . getAttribute ( 'data-photo' )
150
+ // Según el valor del 'data-photo' se llama a la función que corresponde.
137
151
if ( dataPhoto == 'curated' ) {
138
- getInitialRandomPhotos ( index ) ;
152
+ getInitialRandomPhotos ( index ) ; // Función para fotos random.
139
153
} else {
140
- getMoreSearchedPhotos ( index ) ;
154
+ getMoreSearchedPhotos ( index ) ; // Función para fotos buscadas.
141
155
}
142
156
}
143
157
144
-
145
- /**** EVENTOS ****/
146
-
147
- // Evento submit del form, donde se llama a una función que trae las fotos a partir de la búsqueda.
148
- searchForm . addEventListener ( 'submit' , ( e ) => getSearchedPhotos ( e ) ) ;
149
- // Evento click del botón para cargar más fotos mediante las paginaciones de la API.
150
- loadMoreBtn . addEventListener ( 'click' , ( ) => loadMorePhotos ( ) ) ;
151
- // Cuando se carge la ventana, llamamos a la función que nos carga 8 fotos simulando que son fotos aleatorias.
152
- document . addEventListener ( 'DOMContentLoaded' , getInitialRandomPhotos ( pageIndex ) ) ;
153
-
154
- // Botón para limpiar el input del form.
155
- clearInputButton . addEventListener ( 'click' , ( ) => {
158
+ // Creamos una función que nos permita limpiar el 'value' del input además de limpiar la galería según su contenido.
159
+ const clearInputAndGallery = ( ) => {
156
160
let pageIndex = 1 ;
157
161
// Limpiamos el input.
158
162
document . getElementById ( 'searchInput' ) . value = '' ;
159
- // Cuando la galería tiene 3 hijos es porque no se tuvo que crear el h2 para error. Sino, si se tuvo que crear el h2 por lo que tiene más hijos.
160
- if ( gallery . childNodes . length === 3 ) {
163
+ // Cuando la galería tiene 5 hijos es porque no se tuvo que crear el h2 para error. Sino, si se tuvo que crear el h2 por lo que tiene más hijos.
164
+ if ( gallery . childNodes . length === 5 ) {
161
165
// Borra los hijos del contenedor de la galería.
162
166
removeAllChild ( galleryContainer ) ;
163
167
// Carga las fotos aleatorias del principio.
164
168
getInitialRandomPhotos ( pageIndex ) ;
165
169
} else {
166
170
// Borra el h2 con el error.
167
171
gallery . childNodes [ 0 ] . remove ( ) ;
172
+ // Borra los hijos del contenedor de la galería.
173
+ removeAllChild ( galleryContainer ) ;
168
174
// Carga las fotos aleatorias del principio.
169
175
getInitialRandomPhotos ( pageIndex ) ;
170
176
}
177
+ // Mostramos el botón de cargar más fotos.
171
178
loadMoreBtn . style . display = 'flex' ;
172
- } ) ;
179
+ }
173
180
174
181
182
+ /**** EVENTOS ****/
183
+
184
+ // Cuando se carge la ventana, llamamos a la función que nos carga 8 fotos simulando que son fotos aleatorias.
185
+ document . addEventListener ( 'DOMContentLoaded' , getInitialRandomPhotos ( pageIndex ) ) ;
186
+ // Evento submit del form, donde se llama a una función que trae las fotos a partir de la búsqueda.
187
+ searchForm . addEventListener ( 'submit' , ( e ) => getSearchedPhotos ( e ) ) ;
188
+ // Evento click del botón para cargar más fotos mediante las paginaciones de la API.
189
+ loadMoreBtn . addEventListener ( 'click' , ( ) => loadMorePhotos ( ) ) ;
190
+ // Evento click del botón para limpiar el input y la galería.
191
+ clearInputButton . addEventListener ( 'click' , ( ) => clearInputAndGallery ( ) ) ;
175
192
193
+ /*******************/
176
194
177
195
178
196
179
- // Toggle theme functionality
197
+ /**** TOGGLE THEME FUNCIONALITY ****/
180
198
181
199
let toggleTheme = document . getElementById ( 'toggleTheme' ) ;
182
200
let ball = document . getElementById ( 'ball' ) ;
@@ -205,3 +223,4 @@ toggleTheme.addEventListener('click', () => {
205
223
theme != 'darkTheme' ? enableDarkTheme ( ) : disableDarkTheme ( ) ;
206
224
} ) ;
207
225
226
+ /*************************************/
0 commit comments