-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmarvel.js
More file actions
100 lines (79 loc) · 3.11 KB
/
marvel.js
File metadata and controls
100 lines (79 loc) · 3.11 KB
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
const timeStamp = '1737300360';
const apiKey = 'dd361ff2995c08961d504690af3e99ac';
const md5 = 'b9167f19018d6d34a7fc5e130087008a';
const prevButton = document.createElement("button");
prevButton.innerText = "Anterior";
prevButton.disabled = true;
prevButton.classList.add("prev");
const nextButton = document.createElement("button");
nextButton.innerText = "Próximo";
nextButton.classList.add("next");
let firstPag = 0;
const characterLimit = 15;
function nextPage() {
firstPag += characterLimit;
fetchCharacters();
}
function prevPage() {
if (firstPag >= characterLimit) {
firstPag -= characterLimit;
fetchCharacters();
}
}
prevButton.addEventListener("click", prevPage);
nextButton.addEventListener("click", nextPage);
fetchCharacters();
const searchButton = document.querySelector('#searchButton');
searchButton.addEventListener('click', () => {
const query = document.querySelector('#searchInput').value.trim();
fetchCharacters(query);
});
const searchInput = document.querySelector('#searchInput');
searchInput.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
const query = searchInput.value.trim();
fetchCharacters(query);
}
});
function fetchCharacters(query = '') {
const url = `https://gateway.marvel.com:443/v1/public/characters?ts=${timeStamp}&apikey=${apiKey}&hash=${md5}&limit=${characterLimit}&offset=${firstPag}${query ? '&nameStartsWith=' + query : ''}`;
fetch(url)
.then((response) => response.json())
.then((jsonParsed) => {
const divHero = document.querySelector('#herois');
divHero.innerHTML = '';
if (jsonParsed.data.results.length === 0) {
divHero.innerHTML = '<p>Nenhum personagem encontrado.</p>';
}
jsonParsed.data.results.forEach(element => {
prevButton.disabled = firstPag === 0;
nextButton.disabled = jsonParsed.data.results.length < characterLimit;
const srcImage = element.thumbnail.path + '.' + element.thumbnail.extension;
const nameHero = element.name;
if (!srcImage.includes('image_not_available')) {
createDivHero(srcImage, nameHero, divHero);
}
});
})
.catch((error) => {
console.error('Erro ao buscar dados:', error);
});
}
function createDivHero(srcImage, nameHero, divToAppend) {
const divPai = document.createElement('div');
const divFilho = document.createElement('div');
const textName = document.createElement('span');
const img = document.createElement('img');
textName.textContent = nameHero;
img.src = srcImage;
divFilho.appendChild(textName);
divPai.appendChild(img);
divPai.appendChild(divFilho);
divToAppend.appendChild(divPai);
divPai.classList.add('character-card');
}
const paginationContainer = document.createElement("div");
paginationContainer.appendChild(prevButton);
paginationContainer.appendChild(nextButton);
paginationContainer.classList.add("pagination");
document.body.appendChild(paginationContainer);