|
| 1 | +const poke_container = document.getElementById('poke_container'); |
| 2 | +const pokemons_number = 1000; |
| 3 | +const colors = { |
| 4 | + fire: '#FDDFDF', |
| 5 | + grass: '#DEFDE0', |
| 6 | + electric: '#FCF7DE', |
| 7 | + water: '#DEF3FD', |
| 8 | + ground: '#f4e7da', |
| 9 | + rock: '#d5d5d4', |
| 10 | + fairy: '#fceaff', |
| 11 | + poison: '#98d7a5', |
| 12 | + bug: '#f8d5a3', |
| 13 | + dragon: '#97b3e6', |
| 14 | + psychic: '#eaeda1', |
| 15 | + flying: '#F5F5F5', |
| 16 | + fighting: '#E6E0D4', |
| 17 | + normal: '#F5F5F5' |
| 18 | +}; |
| 19 | + |
| 20 | +const main_type = Object.keys(colors); |
| 21 | + |
| 22 | +console.log(main_type); |
| 23 | + |
| 24 | +const fetchPokemons = async () => { |
| 25 | + for(let i=1;i<=pokemons_number;i++){ |
| 26 | + await getPokemon(i); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +const getPokemon = async id => { |
| 31 | + const url = `https://pokeapi.co/api/v2/pokemon/${id}`; |
| 32 | + const res = await fetch(url); |
| 33 | + const pokemon = await res.json(); |
| 34 | + createPokemonCard(pokemon); |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +function createPokemonCard(pokemon){ |
| 40 | + const pokemonEl = document.createElement('div'); |
| 41 | + pokemonEl.classList.add('pokemon'); |
| 42 | + const poke_types = pokemon.types.map(el => el.type.name); |
| 43 | + const type = main_type.find( |
| 44 | + type => poke_types.indexOf(type)> -1 |
| 45 | + ); |
| 46 | + |
| 47 | + const name = pokemon.name[0].toUpperCase() + pokemon.name.slice(1); |
| 48 | + // name = name.substring(0,name.indexOf('-')); |
| 49 | + |
| 50 | + //const sm_name = name.slice(0,'name.lastIndexOf("-")'); |
| 51 | + const color = colors[type]; |
| 52 | + |
| 53 | + pokemonEl.style.backgroundColor = color; |
| 54 | + const pokeInnerHtml = ` |
| 55 | + <div class="img-container"> |
| 56 | + <img src="https://pokeres.bastionbot.org/images/pokemon/${pokemon.id}.png" alt="Sorry!"/> |
| 57 | + </div>` |
| 58 | + const info = `<div class = "info"> |
| 59 | + <span class="number">#${pokemon.id.toString().padStart(3,'0')} |
| 60 | + </span> |
| 61 | + <h3 class="name">${name}</h3> |
| 62 | + <small class="type">Type: <span>${type}</span></small> |
| 63 | + </div> |
| 64 | + `; |
| 65 | + let abilities = pokemon.abilities.length == 0 ? |
| 66 | + ` |
| 67 | + <h3 class="name">No Abilities</h3> |
| 68 | + `: |
| 69 | + ` |
| 70 | + <h3 class ="name">Abilities</h3> |
| 71 | + `; |
| 72 | + pokemon.abilities.forEach((abi, ind)=>{ |
| 73 | +// console.log(abi.ability.name.split() |
| 74 | + abilities += `<br/><small class="type">${ind + 1}${". "} |
| 75 | + <span>${abi.ability.name.split('-').map(val => val.charAt(0).toUpperCase() + val.slice(1)).join(' ')} |
| 76 | + </span></small>` |
| 77 | + }) |
| 78 | + |
| 79 | + pokemonEl.innerHTML = pokeInnerHtml + info; |
| 80 | + |
| 81 | + poke_container.appendChild(pokemonEl); |
| 82 | + |
| 83 | + pokemonEl.addEventListener('mouseenter', ()=>{ |
| 84 | + pokemonEl.style.filter = 'brightness(110%)'; |
| 85 | + pokemonEl.innerHTML = abilities; |
| 86 | + |
| 87 | + }) |
| 88 | + pokemonEl.addEventListener('mouseleave', ()=>{ |
| 89 | + pokemonEl.style.filter = 'none'; |
| 90 | + pokemonEl.innerHTML = pokeInnerHtml + info; |
| 91 | + }) |
| 92 | + |
| 93 | +} |
| 94 | +fetchPokemons(); |
0 commit comments