-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (51 loc) · 2.29 KB
/
app.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
const getAllPokemon = async () => {
try {
console.log("Trying to get all pokemon....")
const response = await fetch(`https://powerful-pear-fedora.cyclic.app/api/v1/pokemon/`)
const pokemonList = await response.json()
console.log(pokemonList)
// dipslay all the pokemon
let outputHtml = ""
// loop through all the pokemon from the API
// for each pokemon, display their name and description and photo
for (let i = 0; i < pokemonList.length; i++) {
outputHtml += `
<p>Name: ${pokemonList[i].name}</p>
<p>Description: ${pokemonList[i].desc}</p>
<img class="img-thumbnail" src="https://powerful-pear-fedora.cyclic.app/images/${pokemonList[i].image}"/>
`
// build the html for a single pokemon
// append the html to the outputHTML variable
}
// insert all the html in the #results-container div on the front -end
document.querySelector("#results-container").innerHTML = outputHtml
// TODO: Output data to the screen
// document.querySelector("#results-container").innerHTML = "Something goes here"
} catch (err) {
console.log(err)
console.log("ERROR")
}
}
const getOnePokemon = async () => {
// get name from UI
const searchTerm = document.querySelector("input").value
console.log(`Searching for: ${searchTerm}`)
// get from server
try {
const response = await fetch(`https://powerful-pear-fedora.cyclic.app/api/v1/pokemon/${searchTerm}`)
const pokemon = await response.json()
console.log(pokemon)
// output it to the screen
let outputHTML = `
<img src="https://powerful-pear-fedora.cyclic.app/images/${pokemon.image}">
<h2 class="fs-4">${pokemon.name}</h2>
<p class="text-secondary">${pokemon.desc}</h2>
`
document.querySelector("#results-container").innerHTML = outputHTML
} catch (err) {
console.log("ERROR")
}
}
document.querySelector("#btn-get-all").addEventListener("click", getAllPokemon)
document.querySelector("#btn-get-one").addEventListener("click", getOnePokemon)
document.querySelector("#btn-contribute").addEventListener("click", addPokemon)