-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from fandok/feat/pokeapi
add pokeapi page
- Loading branch information
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
|
||
<title>PokeAPI</title> | ||
<meta name="viewport" content="width=device-width,initial-scale=1" /> | ||
<meta name="description" content="PokeAPI page example" /> | ||
|
||
<link rel="stylesheet" type="text/css" href="style.css" /> | ||
</head> | ||
<body> | ||
<h1>PokeAPI</h1> | ||
<table id="pokemon"> | ||
<tr> | ||
<th>Name</th> | ||
<th>URL</th> | ||
</tr> | ||
</table> | ||
<button id="loadmore" onclick="fetchData()">Load More</button> | ||
<script src="index.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const API_URL = "https://pokeapi.co/api/v2/pokemon"; | ||
|
||
const addDataToPage = (value = []) => { | ||
const pageData = value | ||
.map( | ||
(val) => | ||
`<tr><td>${val.name}</td><td><a href="${val.url}">${val.url}</a></td></tr>` | ||
) | ||
.join(""); | ||
|
||
const pokemonTag = document.getElementById("pokemon"); | ||
pokemonTag.innerHTML += pageData; | ||
}; | ||
|
||
const fetchData = async () => { | ||
let url = ""; | ||
if (document.getElementById("loadmore").value) { | ||
url = document.getElementById("loadmore").value; | ||
} else { | ||
url = API_URL; | ||
} | ||
|
||
const response = await fetch(url); | ||
const responseBody = await response.json(); | ||
addDataToPage(responseBody.results); | ||
|
||
document.getElementById("loadmore").value = responseBody.next; | ||
}; | ||
|
||
fetchData(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters