-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
222 lines (174 loc) · 6.8 KB
/
main.js
File metadata and controls
222 lines (174 loc) · 6.8 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
function getElement(element){
return document.querySelector(element);
}
const principalContainer = getElement("main .container");
const baseUrl = "https://pokeapi.co/api/v2/pokemon/";
var pokemonHolder;
var hasAErrorOnTheRequest = false;
async function requestPokemon(url, name){
await fetch(url + name)
.then(response => response.json())
.then(data => {pokemonHolder = data;})
.catch(err => {console.log(err); hasAErrorOnTheRequest = true;});
}
window.onload = ()=>{
let theSearchBoxAreOnFocus = false;
document.addEventListener("keypress", (event)=>{
if(event.key == "Enter" && theSearchBoxAreOnFocus){
searchPokemon();
}
});
document.querySelector("#search-box").addEventListener("focusin", (event)=>{
return theSearchBoxAreOnFocus = !theSearchBoxAreOnFocus;
});
document.querySelector("#search-box").addEventListener("focusout", (event)=>{
return theSearchBoxAreOnFocus = !theSearchBoxAreOnFocus;
});
}
function pokemonViewBuilder(pokemon){
let pokemonContainer = document.createElement("div");
pokemonContainer.className = `pokemon ${pokemon.types[0].type.name}-bg`;
//ID Component Builder
let idPokemon = document.createElement("span");
idPokemon.className = "id";
idPokemon.textContent = pokemon.id;
pokemonContainer.appendChild(idPokemon);
//IMG Component Builder
let imagePokemon = document.createElement("img");
imagePokemon.src = pokemon.sprites.front_default;
pokemonContainer.appendChild(imagePokemon);
//Name Component Builder
let namePokemon = document.createElement("h3");
namePokemon.textContent = (pokemon.name).toUpperCase();
pokemonContainer.appendChild(namePokemon);
//Types Component Builder
let typesContainer = document.createElement("div");
typesContainer.className = "types";
for(index = 0; index < pokemon.types.length; index++){
let type = document.createElement("i");
type.className = `fas fa-circle ${pokemon.types[index].type.name}`;
type.title = `${pokemon.types[index].type.name}`
typesContainer.appendChild(type);
}
pokemonContainer.appendChild(typesContainer);
//Stats Component Builder
let statsContainer = document.createElement("div");
statsContainer.className = "stats";
for(index = 0; index < pokemon.stats.length; index++){
let div = document.createElement("div");
let statName = pokemon.stats[index].stat.name;
div.className = statName;
let styles = getComputedStyle(document.body);
let colorBackground = styles.getPropertyValue(`--${statName}`);
let statValue = pokemon.stats[index].base_stat;
div.title = statValue;
let background = `linear-gradient(to right, ${colorBackground} 0% ${statValue}%, white ${statValue}% 100%)`;
div.style.background = background;
div.textContent = statName.toUpperCase();
statsContainer.appendChild(div);
}
pokemonContainer.appendChild(statsContainer);
//Height and Weight Span Builder
let Wrapper = document.createElement("div");
let aboutContainer = document.createElement("div");
aboutContainer.className = "about"
let aboutContent = document.createElement("span");
aboutContent.innerHTML = `<i class="fas fa-arrows-alt-v"></i> ${pokemon.height/10}M <i class="fas fa-weight"></i> ${pokemon.weight/10}KG`;
aboutContainer.appendChild(aboutContent);
Wrapper.appendChild(aboutContainer);
pokemonContainer.appendChild(Wrapper);
//Add to HTML
principalContainer.appendChild(pokemonContainer);
}
function searchPokemon(pokemonIdentifier = (getElement("#search-box").value).toLowerCase(), myTeam = false){
if(document.getElementById("welcome") != null){
principalContainer.removeChild(document.getElementById("welcome"));
}
let outFirstIntervalSearchAreaInAPI = (parseInt(pokemonIdentifier) <= 0 || parseInt(pokemonIdentifier) >= 899);
let outSecondIntervalSearchAreaAPI = (parseInt(pokemonIdentifier) <= 10000 || parseInt(pokemonIdentifier) >= 10221);
if(pokemonIdentifier === "" || (outFirstIntervalSearchAreaInAPI && outSecondIntervalSearchAreaAPI)){
displayErrorOnRequestCard("show");
return;
}
if(!myTeam){
disableButtons();
}
pokemonHolder = null;
requestPokemon(baseUrl, pokemonIdentifier);
setTimeout(() => {
if(hasAErrorOnTheRequest || pokemonHolder == null){
displayErrorOnRequestCard("show");
hasAErrorOnTheRequest = !hasAErrorOnTheRequest;
if(!myTeam){
disableButtons();
}
}else{
displayErrorOnRequestCard("hide");
pokemonViewBuilder(pokemonHolder);
if(!myTeam){
disableButtons();
}
}
}, 1000);
}
function displayErrorOnRequestCard(opt){
let card = getElement(".placeholder-container");
if(opt == "show"){
principalContainer.innerHTML = "";
principalContainer.appendChild(card);
card.style.display = "flex";
}else if(opt = "hide"){
card.style.display = "none";
}
}
function clearView(){
let pokemons = document.querySelectorAll(".pokemon");
if(pokemons.length == 0){
return console.log("Already clear");
}else{
for(i = 0; i < pokemons.length; i++){
principalContainer.removeChild(document.querySelector(".pokemon"));
}
}
}
function myTeam(){
disableButtons();
clearView();
var counter = 0;
let interval = setInterval(()=>{
if(counter == 3){
clearInterval(interval);
disableButtons();
}
counter++;
delayForRequest();
}, 1100);
}
function delayForRequest(){
var id = Math.floor(Math.random() * 898 - 0);
setTimeout(
()=>{
searchPokemon(id, true);
}, 1000
)
}
function disableButtons(){
let resetButton = document.querySelector(".reset");
let searchButton = document.querySelector("#search-box");
let myTeamButton = document.querySelector(".myTeam");
if(resetButton.disabled){
resetButton.disabled = !resetButton.disabled;
resetButton.style.pointerEvents = "all";
searchButton.disabled = !searchButton.disabled;
searchButton.style.pointerEvents = "all";
myTeamButton.disabled = !myTeamButton.disabled;
myTeamButton.style.pointerEvents = "all";
}else{
resetButton.disabled = !resetButton.disabled;
resetButton.style.pointerEvents = "none";
searchButton.disabled = !searchButton.disabled;
searchButton.style.pointerEvents = "none";
myTeamButton.disabled = !myTeamButton.disabled;
myTeamButton.style.pointerEvents = "none";
}
}