-
Notifications
You must be signed in to change notification settings - Fork 0
/
backupmain.js
150 lines (139 loc) · 4.79 KB
/
backupmain.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
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
let pokemons = [];
const POKE_CONT = document.getElementById('poke_cont')
const URL = 'https://pokeapi.co/api/v2/pokemon'
const POKEMONS_NUMBER = 151
const search = document.getElementById("search")
const form = document.getElementById("form")
function clickEvent(type){
type = type.toString().toLowerCase();
getPokemonByType(type);
console.log(type);
}
const fetchPokemons = async() => {
for (let count = 1; count <= POKEMONS_NUMBER; count++){
await getAllPokemons(count);
}
pokemons.forEach(pokemon => createPokemonCart(pokemon))
};
const removePokemon = () => {
const pokemonEls = document.getElementsByClassName('pokemon');
let removablePokemon = [];
for (let count = 0; count < pokemonEls.length; count++){
const pokemonEl = pokemonEls[count]
removablePokemon = [...removablePokemon, pokemonEl]
}
removablePokemon.forEach(remPoke => remPoke.remove())
}
const getPokemonByType = async(type) => {
type = type.toLowerCase()
//const pokeTypes = pokemon.types.map((el) => el.type.name).slice(0, 1);
const searchPokemons = pokemons.filter((poke) => poke.types[0].type.name=== type);
console.log(searchPokemons)
removePokemon();
searchPokemons.forEach((pokemon) => createPokemonCart(pokemon))
}
const getPokemon = async(id) => {
const searchPokemons = pokemons.filter((poke) => poke.name.startsWith(id))
removePokemon();
searchPokemons.forEach((pokemon) => createPokemonCart(pokemon));
}
const getAllPokemons = async(id) => {
const res = await fetch(`${URL}/${id}`);
const pokemon = await res.json();
pokemons = [...pokemons, pokemon]
};
fetchPokemons();
function emoji_types(type){
type = type.toString().toLowerCase();
switch (type) {
case "normal":
return "⚫︎";
case "fire":
return "🔥";
case "water":
return "💧";
case "grass":
return "🌱";
case "flying":
return "🕊️";
case "fighting":
return "🥊";
case "poison":
return "☢";
case "electric":
return "⚡";
case "ground":
return "🗻";
case "rock":
return "🗿";
case "psychic":
return "🔮";
case "ice":
return "❄";
case "bug":
return "🐞";
case "ghost":
return "👻";
case "dragon":
return "🐉";
case "dark":
return "🌙";
case "fairy":
return "✨";
}
};
function ftReset(event){
pokemons = [];
removePokemon();
fetchPokemons();
}
function createPokemonCart(pokemon){
const pokemonEl = document.createElement("div");
pokemonEl.classList.add("pokemon");
let poke_types = pokemon.types.map((el) => el.type.name).slice(0, 1);
poke_types = poke_types.map(el => emoji_types(el));
const name = pokemon.name[0].toUpperCase() + pokemon.name.slice(1);
const poke_stat = pokemon.stats.map((el) => el.stat.name)
const stats = pokemon.stats.slice(0,3);
const base_value = pokemon.stats.map((el) => el.base_stat);
const base_stat = base_value.slice(0, 3);
const stat = stats.map((s) => {
// {"base_stat":45,"effort":0,"stat":{"name":"hp","url":"https://pokeapi.co/api/v2/stat/1/"}}
const { base_stat , stat } = s
const { name } = stat // {"name":"hp","url":"https://pokeapi.co/api/v2/stat/1/"}
return `<li class="names">${name}: ${base_stat}</li>`;
}).join("");
const base = base_stat.map((base) => {
return `<li class="base>${base}</li>`
}).join("");
const pokeInnerHTML = `<div class="img-container">
<img src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/home/${pokemon.id}.png"
alt="${name}"/>
</div>
<div class="info">
<span class="number">#${pokemon.id.toString().padStart(3, "0")}</span>
<h3 class="name">${name}</h3>
<small class="type"><span>${poke_types}</span></small>
</div>
<div class="stats">
<h2>Stats</h2>
<div class="flex">
<ul>${stat}</ul>
<ul>${base}</ul>
</div>
</div>`;
pokemonEl.innerHTML = pokeInnerHTML;
POKE_CONT.appendChild(pokemonEl);
}
form.addEventListener("submit", e =>{
e.preventDefault();
const searchTerm = search.value.toLowerCase();
if (searchTerm) {
getPokemon(searchTerm);
search.value = "";
}else if (searchTerm === ""){
pokemons = [];
removePokemon();
fetchPokemons();
}
});