Skip to content

Commit 11b904a

Browse files
Implemented API Call Challenge
1 parent 5a58950 commit 11b904a

File tree

7 files changed

+192
-1
lines changed

7 files changed

+192
-1
lines changed

package-lock.json

Lines changed: 85 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"body-parser": "^1.19.0",
1717
"cors": "^2.8.5",
1818
"express": "^4.17.1",
19+
"node-fetch": "^3.3.2",
1920
"webpack": "^5.90.0",
2021
"webpack-cli": "^5.1.4"
2122
},

src/client/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { checkForName } from './js/nameChecker';
22
import { handleSubmit } from './js/formHandler';
3-
43
import './styles/resets.scss';
54
import './styles/base.scss';
65
import './styles/form.scss';
6+
import './styles/table.scss';
77
import './styles/footer.scss';
88
import './styles/header.scss';
99

10+
11+
1012
console.log(checkForName);
1113

1214
export {

src/client/js/formHandler.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,59 @@ function handleSubmit(event) {
1616
})
1717
}
1818

19+
20+
21+
const btn = document.getElementById('btn');
22+
const result = document.getElementById('result');
23+
24+
btn.addEventListener('click', async function (event) {
25+
event.preventDefault();
26+
const pokemonName = document.getElementById('pokemonName').value.toLowerCase();
27+
28+
try {
29+
const res = await fetch(`https://pokeapi.co/api/v2/pokemon/${pokemonName}`);
30+
31+
if (!res.ok) {
32+
throw new Error("Could not fetch resource");
33+
}
34+
35+
const data = await res.json();
36+
const pokemonSprite = data.sprites.front_default;
37+
const pokemon = document.getElementById('pokemon');
38+
const type = document.getElementById('type');
39+
const type_0 = data.types[0].type.name;
40+
const type_1 = data.types[1] ? data.types[1].type.name : null;
41+
42+
result.innerHTML = `<img src="${pokemonSprite}" alt="Pokemon"/>`;
43+
pokemon.innerHTML = pokemonName;
44+
type.innerHTML = type_1 ? `${type_0} , ${type_1}` : type_0;
45+
46+
} catch (error) {
47+
console.error(error);
48+
}
49+
});
50+
51+
52+
1953
export { handleSubmit }
54+
55+
56+
57+
58+
59+
/* const result = document.getElementById('result');
60+
const btn = document.getElementById('btn');
61+
btn.addEventListener('click', getRandomCat);
62+
63+
function getRandomCat() {
64+
fetch('http://placekitten.com/200/300')
65+
.then(res => res.blob()) // Use .blob() for image data
66+
.then(data => {
67+
// Create a URL for the blob data and set it as the image source
68+
const imageUrl = URL.createObjectURL(data);
69+
result.innerHTML = `<img src="${imageUrl}" alt="Random Cat"/>`;
70+
})
71+
.catch(error => {
72+
console.error('Error fetching cat:', error);
73+
});
74+
} */

src/client/styles/table.scss

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
$table-width: 50%;
2+
$table-margin: 20px auto;
3+
4+
table {
5+
width: $table-width;
6+
margin: $table-margin;
7+
border-collapse: collapse;
8+
9+
th, td {
10+
padding: 10px;
11+
text-align: left;
12+
border: 1px solid #ddd;
13+
}
14+
15+
th {
16+
background-color: #f2f2f2;
17+
}
18+
19+
#result img {
20+
max-width: 100px;
21+
height: auto;
22+
}
23+
}

src/client/views/index.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@
3333
<strong>Form Results:</strong>
3434
<div id="results"></div>
3535
</section>
36+
37+
<form id="yourFormId">
38+
<input type="text" placeholder="Enter Pokemon Name" id="pokemonName">
39+
<button id="btn" type="submit">Fetch Pokemon</button>
40+
</form>
41+
42+
<section>
43+
<strong>Form Results:</strong>
44+
<table >
45+
<tr>
46+
<th>Pokemon</th>
47+
<td><div id="result"></div></td>
48+
</tr>
49+
<tr>
50+
<th>Name</th>
51+
<td><div id="pokemon"></div></td>
52+
</tr>
53+
<tr>
54+
<th>Type</th>
55+
<td><div id="type"></div></td>
56+
</tr>
57+
</table>
58+
59+
</section>
3660
</main>
3761

3862
<footer>

src/server/mockAPI.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ let json = {
55
}
66

77
module.exports = json
8+

0 commit comments

Comments
 (0)