-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
215 lines (181 loc) · 7.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
function comprobarFormulario() {
event.preventDefault();
let flag = true;
let error = "Surgieron los siguentes errores: \n";
const nombre = document.getElementById("nombre").value;
const apellidos = document.getElementById("apellidos").value;
const telefono = document.getElementById("telefono").value;
const fecha_nacimiento = document.getElementById("fecha_nacimiento").value;
const email = document.getElementById("email").value;
// Validación nombre
if (nombre == "") {
error += "- FALTA RELLENAR EL NOMBRE \n";
flag = false;
}
// Validación apellidos
if (apellidos == "") {
error += "- FALTAN RELLENAR LOS APELLIDOS \n";
flag = false;
}
// Validación telefono
if (telefono == "") {
error += "- FALTA RELLENAR EL TELÉFONO \n";
flag = false;
} else if (telefono.length < 10) {
error += "- TELÉFONO INVÁLIDO \n";
flag = false;
}
// Validación fecha de nacimiento
if (fecha_nacimiento === "") {
error += "- FALTA RELLENAR LA FECHA DE NACIMIENTO \n";
flag = false;
} else if (esMenorEdad(fecha_nacimiento)) {
error += "- LA PERSONA DEBE DE SER MAYOR DE EDAD \n";
flag = false;
}
// Validación email
if (email == "") {
error += "- FALTA RELLENAR EL EMAIL \n";
flag = false;
} else if (!email.includes("@")) {
error += "- EMAIL INVÁLIDO \n";
flag = false;
}
// Validación aceptar terminos y condiciones
if (!document.getElementById("aceptar_terminos_condiciones").checked) {
error += "- FALTA ACEPTAR TERMINOS Y CONDICIONES \n";
flag = false;
}
const enviarBtn = document.getElementById("enviar_form").querySelector("button");
// Enviar formulario
if (flag) {
alert("¡FORMULARIO ENVIADO CON ÉXITO!");
document.getElementById("formulario").submit();
} else {
enviarBtn.classList.add("error");
alert(error);
// Luego de 3 segundos, se elimina la clase de error
setTimeout(function() {
enviarBtn.classList.remove("error");
}, 3000);
};
}
function esMenorEdad(fecha_nacimiento) {
const anios =
//Año actual - año de nacimiento
new Date().getFullYear() - new Date(fecha_nacimiento).getFullYear();
return (
anios < 18 ||
(anios === 18 &&
//Mes actual < mes de nacimiento
new Date().getMonth() + 1 < fecha_nacimiento.substring(5, 7))
);
}
// <-- Cargar Noticias -->
// Función para cargar y mostrar las noticias desde el JSON
let noticias = null; //? Hago la variable aparte para no hacer varios fetch
async function mostrarNoticias() {
try {
const response = await fetch('news.json'); // Cargar el archivo JSON
noticias = await response.json(); // Convertir respuesta a JSON
const noticiasContainer = document.getElementById('noticias'); // Contenedor donde se agregarán las noticias
let Contador = 0; // Contador de tarjetas creadas
let row, col, article, card, img, cardBody, titulo, descripcion, btnNoticia, enlace;
noticias.forEach(noticia => {
if (Contador % 12 === 0) {
// Crear una nueva fila cada 12 Tarjetas
row = document.createElement('div');
row.classList.add('row', 'noticias');
noticiasContainer.appendChild(row);
}
// Crear Carta para cada Noticia
col = document.createElement('div');
col.classList.add('col');
article = document.createElement('article');
card = document.createElement('div');
card.classList.add('card');
card.style.width = '18rem';
img = document.createElement('img');
img.src = noticia.imagen;
img.classList.add('card-img-top');
img.alt = noticia.titulo;
img.title = noticia.titulo;
cardBody = document.createElement('div');
cardBody.classList.add('card-body');
titulo = document.createElement('h3');
titulo.textContent = noticia.titulo;
titulo.classList.add('card-title'); // Añadir clase 'card-title' para el título
descripcion = document.createElement('p');
descripcion.textContent = noticia.descripcion;
descripcion.classList.add('card-text'); // Añadir clase 'collapse' para la descripción inicialmente oculta
btnNoticia = document.createElement('div');
btnNoticia.classList.add('btn-noticia');
enlace = document.createElement('a');
enlace.classList.add('btn', 'btn-success');
enlace.href = '#'; // Ajusta el atributo href según sea necesario
enlace.onclick = () => cargarNoticia(noticia.id);
enlace.dataset.bsToggle = 'modal';
enlace.dataset.bsTarget = 'Noticia-ampliada';
enlace.textContent = 'Ver noticia';
// Construir la estructura de la tarjeta
btnNoticia.appendChild(enlace);
cardBody.appendChild(titulo);
cardBody.appendChild(descripcion);
cardBody.appendChild(btnNoticia);
card.appendChild(img);
card.appendChild(cardBody);
article.appendChild(card);
col.appendChild(article);
// Agregar la tarjeta al último div row creado
row.appendChild(col);
Contador++; // Incrementar el Contador de Tarjetas
});
} catch (error) {
console.error('Error al cargar las noticias:', error);
}
}
// Verificar si estamos en la página 'news.html' antes de cargar las noticias
if (window.location.pathname.includes('news.html')) {
mostrarNoticias().catch(error => console.error('Error al mostrar las noticias:', error)); // Llamar a la función para cargar las noticias
}
// Cargar Noticia en específico
async function cargarNoticia(id) {
try {
const header = document.getElementById('modal-title'); // Contenedor donde se agregará el título del modal
const body = document.getElementById('modal-body'); // Contenedor donde se agregará el cuerpo del modal
// Limpiar contenido anterior
if (header) header.textContent = '';
if (body) body.textContent = '';
const noticia = noticias.find(noticia => noticia.id === id); // Buscar noticia por ID
if (noticia) {
// Mostrar título y cuerpo de la noticia en el modal
const titulo = document.createElement('h3');
titulo.id = 'modal-titulo';
titulo.textContent = noticia.titulo;
const subtitulo = document.createElement('h4');
subtitulo.textContent = noticia.descripcion;
subtitulo.id = 'modal-subtitulo';
const imagen = document.createElement('img');
imagen.src = noticia.imagen;
imagen.alt = noticia.titulo;
imagen.title = noticia.titulo;
imagen.classList.add('img-fluid');
const parrafo = document.createElement('p');
parrafo.textContent = noticia.cuerpo;
parrafo.id = 'modal-parrafo';
const fecha = document.createElement('p');
fecha.textContent = noticia.fecha;
fecha.id = 'modal-fecha';
header.appendChild(imagen);
header.appendChild(titulo);
header.appendChild(subtitulo);
body.appendChild(parrafo);
body.appendChild(fecha);
// Mostrar el modal usando Bootstrap
var myModal = new bootstrap.Modal(document.getElementById('Noticia-ampliada'));
myModal.show();
}
} catch (error) {
console.error('Error al cargar la noticia:', error);
}
}