-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
43 lines (38 loc) · 1.29 KB
/
script.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
// script.js
const TU_API_KEY = "_Lo9ruec1rqQ_-ERrp_NHgqbfBdANyAzPYaSLwGdXh0";
const btn = document.getElementById("btn");
async function fetchPhotos(endpoint, callback) {
const client_id = "client_id=";
const unsplash = "https://api.unsplash.com";
const url = `${unsplash}${endpoint}&${client_id}${TU_API_KEY}`;
try {
const response = await fetch(url);
const data = await response.json();
callback(data);
} catch (error) {
console.error('Error:', error);
}
}
function searchPhotos(data) {
const imgContainer = document.getElementById("imgContainer");
const totalResult = document.getElementById("total");
totalResult.innerHTML = "Resultados: " + data.total;
let box = "";
data.results.forEach(element => {
box += `
<div class="pure-u-1-3 divider">
<a href="${element.links.html}"><img class="pure-img" src="${element.urls.small}" alt=""></a>
</div>
`;
});
imgContainer.innerHTML = box;
}
btn.addEventListener("click", (e) => {
e.preventDefault();
const method = "/search/photos?per_page=30&query=";
const input = document.getElementById("input");
const value = document.getElementById("value");
const inputValue = input.value;
value.innerHTML = "Termino de busqueda: " + inputValue;
fetchPhotos(method + inputValue, searchPhotos);
});