-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
14 changed files
with
123 additions
and
105 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,65 @@ | ||
/* | ||
Enhance createAndAppend() to take optional text and attributes parameters | ||
*/ | ||
|
||
'use strict'; | ||
|
||
{ | ||
const BASE_URL = 'http://api.nobelprize.org/v1'; | ||
|
||
function fetchJSON(url, cb) { | ||
const xhr = new XMLHttpRequest(); | ||
xhr.responseType = 'json'; | ||
xhr.open('GET', url); | ||
xhr.onload = () => { | ||
if (xhr.status >= 200 && xhr.status <= 299) { | ||
cb(null, xhr.response); | ||
} else { | ||
cb(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`)); | ||
} | ||
}; | ||
xhr.onerror = () => cb(new Error('Network request failed')); | ||
xhr.send(); | ||
} | ||
|
||
function createAndAppend(name, parent, options = {}) { | ||
const elem = document.createElement(name); | ||
parent.appendChild(elem); | ||
Object.entries(options).forEach(([key, value]) => { | ||
if (key === 'text') { | ||
elem.textContent = value; | ||
} else { | ||
elem.setAttribute(key, value); | ||
} | ||
}); | ||
return elem; | ||
} | ||
|
||
function onClick(countryCode, ul) { | ||
ul.innerHTML = ''; | ||
|
||
const url = `${BASE_URL}/laureate.json?bornCountryCode=${countryCode}`; | ||
|
||
fetchJSON(url, (err, data) => { | ||
if (err) { | ||
console.error(err.message); // TODO: render errors to the page | ||
return; // exit early in case of errors | ||
} | ||
data.laureates.forEach(laureate => { | ||
createAndAppend('li', ul, { | ||
text: `${laureate.firstname} ${laureate.surname}`, | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
function main() { | ||
const root = document.getElementById('root'); | ||
const input = createAndAppend('input', root, { type: 'text' }); | ||
const button = createAndAppend('button', root, { text: 'GO' }); | ||
const ul = createAndAppend('ul', root); | ||
button.addEventListener('click', () => onClick(input.value, ul)); | ||
} | ||
|
||
window.onload = main; | ||
} |
File renamed without changes.
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
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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
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
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,16 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>Nobel Prizes</title> | ||
</head> | ||
|
||
<body> | ||
<div id="root"></div> | ||
<script src="app.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