-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
55 lines (50 loc) · 1.68 KB
/
script.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
/*
function disableButton(){
console.log("+1");
if(/^[A-Za-z]{3,}$/.test(document.getElementById("requestPokemon")) !== false){
document.getElementById("searchPokemon").disableButton = true;
}
}
*/
async function fetchPokemon(pokemonNameOrId){
const response = await fetch("https://pokeapi.co/api/v2/pokemon/" + pokemonNameOrId);
if (response.ok === false) {
throw new Error("Ce pokémon n'existe pas");
}
var pokemon = await response.json();
return pokemon;
}
function updatePokedex(pokemon){
document
.getElementById("name")
.innerText = "Nom : " + pokemon.name;
document
.getElementById("weight")
.innerText = "Il fait " + pokemon.weight + "g";
document
.getElementById("type")
.innerText = "Il est de type(s) " + pokemon.types.map((singleType) => singleType.type.name + " ")
document
.getElementById("pokeId")
.innerText = "ID du pokémon : " + pokemon.id;
document
.getElementById("image")
.setAttribute("src", pokemon.sprites.front_default);
}
async function stringQuery(){
const url = (new URL(document.location)).searchParams;
const pokemon = await fetchPokemon(url.get('id'));
updatePokedex(pokemon);
}
function main() {
const pokemonNameElement = document.getElementById("requestPokemon");
document.getElementById("requestPokemon").addEventListener('keypress', (event) => disableButton());
document.getElementById("searchPokemon").addEventListener('click', async (event) => {
const pokemon = await fetchPokemon(pokemonNameElement.value);
updatePokedex(pokemon);
});
}
document.addEventListener("DOMContentLoaded", function(event) {
stringQuery();
main();
});