Skip to content

Commit

Permalink
Fetch pokemon at the same time, add alpine.js for interactivity and p…
Browse files Browse the repository at this point in the history
…ersonalize the style.
  • Loading branch information
Pizt0lmnk committed Feb 2, 2022
1 parent a731366 commit a4a3d74
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 302 deletions.
65 changes: 16 additions & 49 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,23 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta content="IE=edge" http-equiv="X-UA-Compatible">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>Pokédex</title>

<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap" rel="stylesheet">
<link rel="stylesheet" href="styles.css">
<script src="scripts.js"></script>


<script defer src="//unpkg.com/alpinejs"></script>
<link href="https://fonts.googleapis.com" rel="preconnect">
<link crossorigin href="https://fonts.gstatic.com" rel="preconnect">
<link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap"
rel="stylesheet">
<link href="styles.css" rel="stylesheet">
<script src="scripts.js"></script>
</head>
<body onload="displayOverview()">
<nav class="">
<div class="displayOverview">
<a id="nav-container" class="navbar-brand" href="#">
<div class="div-left-nav">
<img id="nav-img" src="/img/pokeball.png" alt="" class="">
<h1 id="nav-text">Pokédex</h1>
</div>
<img id="nav-img-header" src="/img/Pokémon-header.jpg" class="">
</a>
</div>
</nav>

<div id="overlay-pokedex" class="main-overlay"></div>

<div id="overview" class="container-overview"></div>


<div onclick="hideCard()" id="pokedex" class="d-none m-3 card bg-nature" style="width: 18rem;">
<img id="pokemonImage" style="height:180px; width:180px;" src='' class=" ms-4 card-img-top" alt="...">
<div class="card-body">
<h5 id="pokemonName" class="card-title">Pokémon Name</h5>
<p class="card-text">Base-Stats</p>
</div>
<ul class="list-group list-group-flush">
<li id="skill-hp" class="list-group-item">hp</li>
<li id="skill-attack" class="list-group-item">attack</li>
<li id="skill-defence" class="list-group-item">defence</li>
<li id="skill-speed" class="list-group-item">speed</li>

</ul>
<div class="card-body">
<p id="type-1" >Type 1</p>
<p id="type-2" >Type 2</p>
</div>
</div>



<body>
<nav class="navbar">
<h1 class="navbar-headline">Pokédex</h1>
</nav>
<div class="overlay" x-init="" x-on:click="$store.cards.centered = !$store.cards.centered"
x-show="$store.cards.centered"></div>
<div class="container-overview" id="pokemon-list"></div>
</body>
</html>
62 changes: 0 additions & 62 deletions pokedex.html

This file was deleted.

105 changes: 37 additions & 68 deletions scripts.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,49 @@
let allPokemon = ["Bulbasaur", "Ivysaur", "Venusaur", "Charmander", "Charmeleon", "Charizard", "Squirtle", "Wartortle", "Blastoise", "Caterpie", "Metapod", "Butterfree", "Weedle", "Kakuna", "Beedrill", "Pidgey", "Pidgeotto", "Pidgeot", "Rattata", "Raticate", "Spearow", "Fearow", "Ekans", "Arbok", "Pikachu", "Raichu", "Sandshrew", "Sandslash", "nidoran-f", "Nidorina", "Nidoqueen", "nidoran-m", "Nidorino", "Nidoking", "Clefairy", "Clefable", "Vulpix", "Ninetales", "Jigglypuff", "Wigglytuff", "Zubat", "Golbat", "Oddish", "Gloom", "Vileplume", "Paras", "Parasect", "Venonat", "Venomoth", "Diglett", "Dugtrio", "Meowth", "Persian", "Psyduck", "Golduck", "Mankey", "Primeape", "Growlithe", "Arcanine", "Poliwag", "Poliwhirl", "Poliwrath", "Abra", "Kadabra", "Alakazam", "Machop", "Machoke", "Machamp", "Bellsprout", "Weepinbell", "Victreebel", "Tentacool", "Tentacruel", "Geodude", "Graveler", "Golem", "Ponyta", "Rapidash", "Slowpoke", "Slowbro", "Magnemite", "Magneton", "Farfetchd", "Doduo", "Dodrio", "Seel", "Dewgong", "Grimer", "Muk", "Shellder", "Cloyster", "Gastly", "Haunter", "Gengar", "Onix", "Drowzee", "Hypno", "Krabby", "Kingler", "Voltorb", "Electrode", "Exeggcute", "Exeggutor", "Cubone", "Marowak", "Hitmonlee", "Hitmonchan", "Lickitung", "Koffing", "Weezing", "Rhyhorn", "Rhydon", "Chansey", "Tangela", "Kangaskhan", "Horsea", "Seadra", "Goldeen", "Seaking", "Staryu", "Starmie", "mr-mime", "Scyther", "Jynx", "Electabuzz", "Magmar", "Pinsir", "Tauros", "Magikarp", "Gyarados", "Lapras", "Ditto", "Eevee", "Vaporeon", "Jolteon", "Flareon", "Porygon", "Omanyte", "Omastar", "Kabuto", "Kabutops", "Aerodactyl", "Snorlax", "Articuno", "Zapdos", "Moltres", "Dratini", "Dragonair", "Dragonite", "Mewtwo"];
let allPokemon = ["Bulbasaur", "Ivysaur", "Venusaur", "Charmander", "Charmeleon", "Charizard", "Squirtle", "Wartortle", "Blastoise", "Caterpie", "Metapod", "Butterfree", "Weedle", "Kakuna", "Beedrill", "Pidgey", "Pidgeotto", "Pidgeot", "Rattata", "Raticate", "Spearow", "Fearow", "Ekans", "Arbok", "Pikachu", "Raichu", "Sandshrew", "Sandslash", "nidoran-f", "Nidorina", "Nidoqueen", "nidoran-m", "Nidorino", "Nidoking", "Clefairy", "Clefable", "Vulpix", "Ninetales", "Jigglypuff", "Wigglytuff", "Zubat", "Golbat", "Oddish", "Gloom", "Vileplume", "Paras", "Parasect", "Venonat", "Venomoth", "Diglett", "Dugtrio", "Meowth", "Persian", "Psyduck", "Golduck", "Mankey", "Primeape", "Growlithe", "Arcanine", "Poliwag", "Poliwhirl", "Poliwrath", "Abra", "Kadabra", "Alakazam", "Machop", "Machoke", "Machamp", "Bellsprout", "Weepinbell", "Victreebel", "Tentacool", "Tentacruel", "Geodude", "Graveler", "Golem", "Ponyta", "Rapidash", "Slowpoke", "Slowbro", "Magnemite", "Magneton", "Farfetchd", "Doduo", "Dodrio", "Seel", "Dewgong", "Grimer", "Muk", "Shellder", "Cloyster", "Gastly", "Haunter", "Gengar", "Onix", "Drowzee", "Hypno", "Krabby", "Kingler", "Voltorb", "Electrode", "Exeggcute", "Exeggutor", "Cubone", "Marowak", "Hitmonlee", "Hitmonchan", "Lickitung", "Koffing", "Weezing", "Rhyhorn", "Rhydon", "Chansey", "Tangela", "Kangaskhan", "Horsea", "Seadra", "Goldeen", "Seaking", "Staryu", "Starmie", "mr-mime", "Scyther", "Jynx", "Electabuzz", "Magmar", "Pinsir", "Tauros", "Magikarp", "Gyarados", "Lapras", "Ditto", "Eevee", "Vaporeon", "Jolteon", "Flareon", "Porygon", "Omanyte", "Omastar", "Kabuto", "Kabutops", "Aerodactyl", "Snorlax", "Articuno", "Zapdos", "Moltres", "Dratini", "Dragonair", "Dragonite", "Mewtwo", "Mew"];

document.addEventListener('alpine:init', () => {
Alpine.store('cards', {
centered: false, centeredPokemon: '',
})

let currentPokemon; // globaly declader to be used later in all functions when filled
fetchPokemon().then(() => console.log('Pokemon loaded.'));
})


async function displayOverview() {
async function fetchPokemon() {
const urls = [];
for (let i = 0; i < allPokemon.length; i++) { // load all pokemon from array
const thisPokemon = allPokemon[i];
let url = 'https://pokeapi.co/api/v2/pokemon/' + thisPokemon.toLowerCase(); //lowercase so it fits the URL

let response = await fetch(url); //waiting so the function doesnt continue without this input
currentPokemon = await response.json(); // to json so we have a file type we can work with

urls.push('https://pokeapi.co/api/v2/pokemon/' + allPokemon[i].toLowerCase()); //lowercase so it fits the URL
}
const requests = urls.map((url) => fetch(url));
const responses = await Promise.all(requests);
const json = responses.map((response) => response.json());
const data = await Promise.all(json);
data.forEach((pokemon, index) => {
let pokemonEntry = ''; // predefined and empty so it can be filled later (3 parts)

let Types = currentPokemon["types"].length; // checking if one or two types are defined
let Types = pokemon["types"].length; // checking if one or two types are defined
pokemonEntry += `
<div onclick="loadPokemon('${currentPokemon['name']}')" class="card-wrapper bg-${currentPokemon["types"]["0"]["type"]["name"]}">
<img src="${currentPokemon["sprites"]["other"]["dream_world"]["front_default"]}" class="card-image " alt="${currentPokemon['name']}">
<div :id="$id('pokemon')" x-bind:class="$store.cards.centered && $store.cards.centeredPokemon === $el.id ? 'card-centered' : ''" x-on:click="setPokemon($el)" class="card-wrapper bg-${pokemon["types"]["0"]["type"]["name"]}">
<img src="${pokemon["sprites"]["other"]["dream_world"]["front_default"]}" class="card-image " alt="${pokemon['name']}">
<div class="card-description">
<h2 class="card-title">${currentPokemon['name'].substring(0,1).toUpperCase() + currentPokemon['name'].substring(1)}</h2>
<p class="card-text"> ${currentPokemon["types"]["0"]["type"]["name"]}</p>`
if (Types > 1) { // to display only if both types are set
pokemonEntry += `<p class="card-text">${currentPokemon["types"]["1"]["type"]["name"]}</p>`
}
pokemonEntry += `
<h2 class="card-title">${pokemon['name'].substring(0, 1).toUpperCase() + pokemon['name'].substring(1)}<small class="card-number">#${index + 1}</small></h2>
<p class="card-text"> ${pokemon["types"]["0"]["type"]["name"]}</p>`
if (Types > 1) { // to display only if both types are set
pokemonEntry += `<p class="card-text">${pokemon["types"]["1"]["type"]["name"]}</p>`
}
pokemonEntry += `
</div>
</div>`
document.getElementById('overview').innerHTML += pokemonEntry; // let the different elements be in place befor you display it
}
}

/*
function SeconType() {
let Types = currentPokemon["types"].length;
if (Types > 1) {
document.getElementById('quick-fix').innerHTML += ` <p class="card-text">${currentPokemon["types"]["1"]["type"]["name"]}</p>`;
}
}
*/


function renderPokemonInfo() { // shows a variaty of info on selected pokemnon

document.getElementById('pokemonName').innerHTML = currentPokemon['name'].toUpperCase();
document.getElementById('pokemonImage').src = currentPokemon["sprites"]["other"]["dream_world"]["front_default"]; //its the .img so src =
document.getElementById('skill-hp').innerHTML = currentPokemon["stats"][0]["base_stat"] + ` hp`;
document.getElementById('skill-attack').innerHTML = currentPokemon["stats"][1]["base_stat"] + ` attack`;
document.getElementById('skill-defence').innerHTML = currentPokemon["stats"][2]["base_stat"] + ` defence`;
document.getElementById('skill-speed').innerHTML = currentPokemon["stats"][5]["base_stat"] + ` speed`;
document.getElementById('type-1').innerHTML = currentPokemon["types"]["0"]["type"]["name"];
document.getElementById('type-2').innerHTML = currentPokemon["types"]["1"]["type"]["name"];
}



async function loadPokemon(name) {
showCard();
console.log('The picked Pokemon is', name);
let url = 'https://pokeapi.co/api/v2/pokemon/' + name; // to lowercase to make all characters small
let response = await fetch(url); // wait for the fetch
currentPokemon = await response.json(); //declare api/json to be used later

renderPokemonInfo(); // display single pokemoncard
}


function showCard() {
document.getElementById('pokedex').classList.remove('d-none');
document.getElementById('overview').classList.add('d-none');
}


function hideCard() {
document.getElementById('pokedex').classList.add('d-none');
document.getElementById('overview').classList.remove('d-none');
document.getElementById('pokemon-list').innerHTML += pokemonEntry; // let the different elements be in place befor you display it
});
}

async function setPokemon(element) {
if (Alpine.store('cards').centered === false) {
Alpine.store('cards').centered = true;
Alpine.store('cards').centeredPokemon = element.id;
} else {
Alpine.store('cards').centered = false;
Alpine.store('cards').centeredPokemon = '';
}
}
Loading

0 comments on commit a4a3d74

Please sign in to comment.