-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_pokedex.js
111 lines (102 loc) · 3.73 KB
/
api_pokedex.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
/*
Nombre
Imagen
Tipo de pokemón
Estadísticas
Habilidades
*/
const pokeCard = document.querySelector('[data-poke-card]');
const pokeName = document.querySelector('[data-poke-name]');
const pokeNameError = document.querySelector('[data-poke-name-error]')
const pokeImg = document.querySelector('[data-poke-img]');
const pokeImgContainer = document.querySelector('[data-poke-img-container]');
const pokeId = document.querySelector('[data-poke-id]');
const pokeTypes = document.querySelector('[data-poke-types]');
const pokeStats = document.querySelector('[data-poke-stats]');
const pokeAbilities = document.querySelector('[data-poke-abilities]');
const typeColors = {
electric: '#FFEC5C',
normal: '#B09398',
fire: '#FF675C',
water: '#0596C7',
ice: '#AFEAFD',
rock: '#999799',
flying: '#7AE7C7',
grass: '#4A9681',
psychic: '#FFC6D9',
ghost: '#561D25',
bug: '#84FA86',
poison: '#795663',
ground: '#D2B074',
dragon: '#DA627D',
steel: '#1D8A99',
fighting: '#2F2F2F',
default: '#2A1A1F',
};
const searchPokemon = event => {
event.preventDefault();
const { value } = event.target.pokemon;
fetch(`https://pokeapi.co/api/v2/pokemon/${value.toLowerCase()}`)
.then(data => data.json())
.then(response => renderPokemonData(response))
.catch(err => renderNotFound())
}
const renderPokemonData = data => {
const sprite = data.sprites.front_default;
const { stats, types, abilities } = data;
pokeName.textContent = data.name;
pokeImg.setAttribute('src', sprite);
pokeId.textContent = `Nº ${data.id}`;
setCardColor(types);
renderPokemonTypes(types);
renderPokemonStats(stats);
renderPokemonAbilities(abilities);
}
const setCardColor = types => {
const colorOne = typeColors[types[0].type.name];
const colorTwo = types[1] ? typeColors[types[1].type.name] : typeColors.default;
pokeImg.style.background = `radial-gradient(${colorTwo} 33%, ${colorOne} 33%)`;
pokeImg.style.backgroundSize = ' 5px 5px';
}
const renderPokemonTypes = types => {
pokeTypes.innerHTML = '';
types.forEach(type => {
const typeTextElement = document.createElement("div");
typeTextElement.style.color = typeColors[type.type.name];
typeTextElement.textContent = type.type.name;
pokeTypes.appendChild(typeTextElement);
});
}
const renderPokemonStats = stats => {
pokeStats.innerHTML = '';
stats.forEach(stat => {
const statElement = document.createElement("div");
const statElementName = document.createElement("div");
const statElementAmount = document.createElement("div");
statElementName.textContent = stat.stat.name;
statElementAmount.textContent = stat.base_stat;
statElement.appendChild(statElementName);
statElement.appendChild(statElementAmount);
pokeStats.appendChild(statElement);
});
}
const renderPokemonAbilities = abilities => {
pokeAbilities.innerHTML = '';
abilities.forEach(ability => {
const abilityElement = document.createElement("div");
const abilityElementName = document.createElement("div");
abilityElementName.textContent = ability.ability.name;
abilityElement.appendChild(abilityElementName);
pokeAbilities.appendChild(abilityElement);
});
}
const renderNotFound = () => {
pokeNameError.textContent = 'No encontrado';
pokeImg.setAttribute('src', 'poke-shadow.png');
pokeImg.style.background = '#FFF';
pokeName.textContent = '';
pokeId.textContent = '';
pokeTypes.innerHTML = '';
pokeStats.innerHTML = '';
pokeAbilities.innerHTML = '';
}