-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
54 lines (48 loc) · 2.24 KB
/
app.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
document.getElementById('searchButton').addEventListener('click', function() {
const interest = document.getElementById('interestInput').value;
const token = document.getElementById('apiTokenInput').value;
if (!interest || !token) {
alert('Por favor, completa ambos campos.');
return;
}
// Mostrar el spinner y limpiar la tabla de resultados mientras se carga la búsqueda
document.getElementById('loadingSpinner').style.display = 'block';
document.getElementById('resultsTableBody').innerHTML = ''; // Limpiar la tabla de resultados
// URL de búsqueda de intereses
const apiUrl = `https://graph.facebook.com/search?type=adinterest&q=["${interest}"]&limit=200&locale=es_ES&access_token=${token}`;
// Hacer la solicitud de búsqueda de intereses
fetch(apiUrl)
.then(response => response.json())
.then(data => {
mostrarData(data);
// Ocultar el spinner cuando se complete la carga
document.getElementById('loadingSpinner').style.display = 'none';
})
.catch(error => {
console.error('Error al hacer la solicitud:', error);
alert('Hubo un problema al hacer la solicitud. Revisa el token de API e inténtalo de nuevo.');
document.getElementById('loadingSpinner').style.display = 'none';
});
});
// Función para mostrar los datos en la tabla
const mostrarData = (data) => {
const tableBody = document.getElementById('resultsTableBody');
tableBody.innerHTML = '';
if (data.data && data.data.length > 0) {
data.data.forEach((item, index) => {
const row = `<tr>
<td>${index + 1}</td>
<td>${item.name || 'N/A'}</td>
<td>${item.topic || 'N/A'}</td>
<td>${item.audience_size_lower_bound || 'N/A'}</td>
<td>${item.audience_size_upper_bound || 'N/A'}</td>
<td>${item.path ? item.path.join(' > ') : 'N/A'}</td>
<td>${item.description || 'N/A'}</td>
</tr>`;
tableBody.innerHTML += row;
});
} else {
const row = `<tr><td colspan="7" class="text-center">No se encontraron resultados</td></tr>`;
tableBody.innerHTML += row;
}
};