From 6544ddf96573a7323ea81aaeefa803759a1a12c4 Mon Sep 17 00:00:00 2001 From: Rafael Date: Wed, 21 Feb 2024 13:41:31 -0300 Subject: [PATCH] feat- add details page --- assets/css/pokedexDetails.css | 111 ++++++++++++++++++++++++++++++++++ assets/js/main.js | 90 +++++++++++++++++++-------- assets/js/poke-api.js | 9 +++ assets/js/pokemon-model.js | 4 ++ index.html | 1 + 5 files changed, 191 insertions(+), 24 deletions(-) create mode 100644 assets/css/pokedexDetails.css diff --git a/assets/css/pokedexDetails.css b/assets/css/pokedexDetails.css new file mode 100644 index 000000000..bc8521da4 --- /dev/null +++ b/assets/css/pokedexDetails.css @@ -0,0 +1,111 @@ +.pokemonDatails { + display: grid; + grid-template-columns: 1fr; + margin: 0; + padding: 0; + list-style: none; +} + +.pokemonDatails img { + width: 10rem; + height: 10rem; +} + +.normal { + background-color: #a6a877; +} + +.grass { + background-color: #77c850; +} + +.fire { + background-color: #ee7f30; +} + +.water { + background-color: #678fee; +} + +.electric { + background-color: #f7cf2e; +} + +.ice { + background-color: #98d5d7; +} + +.ground { + background-color: #dfbf69; +} + +.flying { + background-color: #a98ff0; +} + +.poison { + background-color: #a040a0; +} + +.fighting { + background-color: #bf3029; +} + +.psychic { + background-color: #f65687; +} + +.dark { + background-color: #725847; +} + +.rock { + background-color: #b8a137; +} + +.bug { + background-color: #a8b720; +} + +.ghost { + background-color: #6e5896; +} + +.steel { + background-color: #b9b7cf; +} + +.dragon { + background-color: #6f38f6; +} + +.fairy { + background-color: #f9aec7; +} + +.info { + display: flex; + flex-direction: column; + margin: .5rem; + padding: 1rem; + border-radius: 1rem; +} + +.info .number { + color: #000; + opacity: .3; + text-align: right; + font-size: .625rem; +} + +.info h2 { + text-transform: capitalize; + color: white; +} + +.stats { + background-color: white; + width: 100%; + border-top-right-radius: 1rem; + border-top-left-radius: 1rem; +} diff --git a/assets/js/main.js b/assets/js/main.js index bcaa24508..051d35e47 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -1,47 +1,89 @@ -const pokemonList = document.getElementById('pokemonList') -const loadMoreButton = document.getElementById('loadMoreButton') +const pokemonList = document.getElementById('pokemonList'); +const loadMoreButton = document.getElementById('loadMoreButton'); +const content = document.getElementsByClassName('content')[0]; -const maxRecords = 151 -const limit = 10 +const maxRecords = 151; +const limit = 10; let offset = 0; function convertPokemonToLi(pokemon) { - return ` + return `
  • #${pokemon.number} ${pokemon.name}
      - ${pokemon.types.map((type) => `
    1. ${type}
    2. `).join('')} + ${pokemon.types + .map((type) => `
    3. ${type}
    4. `) + .join('')}
    ${pokemon.name}
  • - ` + `; +} + +function convertPokemonDetailToHtml(pokemon) { + return ` +
    +
    +

    ${pokemon.name}

    + #${pokemon.number} +

    ${pokemon.type}

    +
    + ${pokemon.name} +
    +

    Height: ${pokemon.height}

    +

    Weight: ${pokemon.weight}

    +

    Abilities: ${pokemon.abilities.join(', ')}

    +
    +
    + ` } function loadPokemonItens(offset, limit) { - pokeApi.getPokemons(offset, limit).then((pokemons = []) => { - const newHtml = pokemons.map(convertPokemonToLi).join('') - pokemonList.innerHTML += newHtml - }) + pokeApi.getPokemons(offset, limit).then((pokemons = []) => { + const newHtml = pokemons.map(convertPokemonToLi).join(''); + pokemonList.innerHTML += newHtml; + }); } -loadPokemonItens(offset, limit) +loadPokemonItens(offset, limit); loadMoreButton.addEventListener('click', () => { - offset += limit - const qtdRecordsWithNexPage = offset + limit - - if (qtdRecordsWithNexPage >= maxRecords) { - const newLimit = maxRecords - offset - loadPokemonItens(offset, newLimit) - - loadMoreButton.parentElement.removeChild(loadMoreButton) - } else { - loadPokemonItens(offset, limit) - } -}) \ No newline at end of file + offset += limit; + const qtdRecordsWithNexPage = offset + limit; + + if (qtdRecordsWithNexPage >= maxRecords) { + const newLimit = maxRecords - offset; + loadPokemonItens(offset, newLimit); + + loadMoreButton.parentElement.removeChild(loadMoreButton); + } else { + loadPokemonItens(offset, limit); + } +}); + +async function getPokemonById(pokemonId) { + const url = `https://pokeapi.co/api/v2/pokemon/${pokemonId}`; + const response = await fetch(url); + const pokeDetail = await response.json(); + const pokemon = convertPokeApiDetailToPokemon(pokeDetail); + return convertPokemonDetailToHtml(pokemon); +} + +pokemonList.addEventListener('click', (event) => { + event.preventDefault(); + pokemonId = event.target + .closest('.pokemon') + .querySelector('.number') + .textContent.split('#')[1]; + + getPokemonById(pokemonId).then((html) => { + content.innerHTML = html; + }); + +}); diff --git a/assets/js/poke-api.js b/assets/js/poke-api.js index 38fbfd465..4f3620be2 100644 --- a/assets/js/poke-api.js +++ b/assets/js/poke-api.js @@ -14,6 +14,15 @@ function convertPokeApiDetailToPokemon(pokeDetail) { pokemon.photo = pokeDetail.sprites.other.dream_world.front_default + pokemon.abilities = pokeDetail.abilities.map((abilitySlot) => abilitySlot.ability.name) + + pokemon.weight = pokeDetail.weight + + pokemon.height = pokeDetail.height + + pokemon.frontDetailPhoto = pokeDetail.sprites.other['official-artwork'].front_default + + return pokemon } diff --git a/assets/js/pokemon-model.js b/assets/js/pokemon-model.js index b0d17bb90..8b827fb09 100644 --- a/assets/js/pokemon-model.js +++ b/assets/js/pokemon-model.js @@ -5,4 +5,8 @@ class Pokemon { type; types = []; photo; + abilities = []; + weight; + height; + frontDetailPhoto; } diff --git a/index.html b/index.html index 1a017821d..332a68016 100644 --- a/index.html +++ b/index.html @@ -20,6 +20,7 @@ +