-
Notifications
You must be signed in to change notification settings - Fork 1
/
pokemons.js
65 lines (56 loc) · 1.93 KB
/
pokemons.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
const baseUrl = 'https://pokeapi.co/api/v2/pokemon/';
const searchInput = getElement('.search-input'),
searchButton = getElement('.search-button'),
container = getElement('.pokemon-container'),
erroMessage = getElement('.error');
var pokeName,
pokemon,
card;
function getElement(element) {
return document.querySelector(element);
}
function requestPokeInfo(url, name) {
fetch(url + name)
.then(response => response.json())
.then(data => {
pokemon = data;
})
.catch(err => console.log(err));
}
function createCard () {
card = `
<div class="pokemon-picture">
<img src="${pokemon.sprites.front_default}" alt="Sprite of ${pokemon.name}">
</div>
<div class="pokemon-info">
<h1 class="name">Name: ${pokemon.name}</h1>
<h2 class="number">Nº ${pokemon.id}</h2>
<h3 class="type">Type: ${pokemon.types.map(item => item.type.name).toString()}</h3>
<h3 class="skill">Skills: ${pokemon.moves.map(item => ' ' + item.move.name).toString()}</h3>
<h3 class="weight">Weight: ${pokemon.weight / 10}kg</h3>
<h3 class="height">Height: ${pokemon.height / 10}m</h3>
</div>`;
return card;
}
function startApp(pokeName) {
requestPokeInfo(baseUrl, pokeName);
setTimeout(function () {
if(pokemon.detail) {
erroMessage.style.display = 'block';
container.style.display = 'none';
}else{
erroMessage.style.display = 'none';
container.style.display = 'flex';
container.innerHTML = createCard();
}
}, 2000);
}
searchButton.addEventListener('click', event => {
event.preventDefault();
pokeName = searchInput.value.toLowerCase();
startApp(pokeName);
container.classList.add('fade');
setTimeout(() => {
container.classList.remove('fade');
}, 3000);
});