|
1 | 1 | 'use strict'; |
2 | 2 | { |
3 | | - // cb(err, data) |
| 3 | + function fetchJSON(url) { |
| 4 | + return new Promise((resolve, reject) => { |
| 5 | + const xhr = new XMLHttpRequest(); |
| 6 | + xhr.open('GET', url); |
| 7 | + xhr.responseType = 'json'; |
| 8 | + xhr.onload = () => { |
| 9 | + if (xhr.status < 400) { |
| 10 | + resolve(xhr.response); |
| 11 | + } else { |
| 12 | + reject(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`)); |
| 13 | + } |
| 14 | + }; |
| 15 | + xhr.onerror = () => reject(new Error('Network request failed')); |
| 16 | + xhr.send(); |
| 17 | + }); |
| 18 | + } |
4 | 19 |
|
5 | 20 | function createAndAppend(name, parent, options = {}) { |
6 | 21 | const elem = document.createElement(name); |
|
21 | 36 | createAndAppend('td', tr, { text: value }); |
22 | 37 | } |
23 | 38 |
|
24 | | - function renderLaureates(ul, data) { |
25 | | - ul.innerHTML = ''; |
26 | | - |
27 | | - data.laureates.forEach(laureate => { |
28 | | - const li = createAndAppend('li', ul, { class: 'list-item' }); |
29 | | - const table = createAndAppend('table', li); |
30 | | - const tbody = createAndAppend('tbody', table); |
31 | | - const fullName = `${laureate.firstname} ${laureate.surname}`; |
32 | | - addRow('Name:', fullName, tbody); |
33 | | - const bornDetails = `${laureate.born}, ${laureate.bornCity}`; |
34 | | - addRow('Born:', bornDetails, tbody); |
35 | | - }); |
| 39 | + async function fetchAndRender(url, container) { |
| 40 | + container.innerHTML = ''; |
| 41 | + const ul = createAndAppend('ul', container, { id: 'list-container' }); |
| 42 | + try { |
| 43 | + const resp = await fetchJSON(url); |
| 44 | + resp.laureates.forEach(laureate => { |
| 45 | + const li = createAndAppend('li', ul, { class: 'list-item' }); |
| 46 | + const table = createAndAppend('table', li); |
| 47 | + const tbody = createAndAppend('tbody', table); |
| 48 | + const fullName = `${laureate.firstname} ${laureate.surname}`; |
| 49 | + addRow('Name:', fullName, tbody); |
| 50 | + const bornDetails = `${laureate.born}, ${laureate.bornCity}`; |
| 51 | + addRow('Born:', bornDetails, tbody); |
| 52 | + }); |
| 53 | + } catch (err) { |
| 54 | + createAndAppend('p', container, { text: err.message }); |
| 55 | + } |
36 | 56 | } |
37 | 57 |
|
38 | | - function main() { |
39 | | - const queries = [ |
40 | | - { |
41 | | - description: 'All Dutch lauerates', |
42 | | - url: 'http://api.nobelprize.org/v1/laureate.json?bornCountryCode=NL' |
43 | | - }, |
44 | | - { |
45 | | - description: 'All female laureates', |
46 | | - url: 'http://api.nobelprize.org/v1/laureate.json?gender=female' |
47 | | - } |
48 | | - ]; |
49 | | - |
| 58 | + function main(queries) { |
50 | 59 | const root = document.getElementById('root'); |
51 | 60 | const select = createAndAppend('select', root); |
| 61 | + |
52 | 62 | queries.forEach((query, index) => { |
53 | 63 | createAndAppend('option', select, { value: index, text: query.description }); |
54 | 64 | }); |
55 | 65 |
|
56 | | - const ul = createAndAppend('ul', root, { id: 'list-container' }); |
57 | | - |
58 | 66 | select.addEventListener('change', () => { |
59 | | - console.log(select.value); |
60 | 67 | const query = queries[select.value]; |
61 | | - fetchJSON(query.url, (err, data) => { |
62 | | - renderLaureates(ul, data); |
63 | | - }); |
64 | | - }); |
65 | | - |
66 | | - |
67 | | - fetchJSON(queries[0].url, (err, data) => { |
68 | | - renderLaureates(ul, data); |
| 68 | + fetchAndRender(query.url, container); |
69 | 69 | }); |
70 | 70 |
|
| 71 | + const container = createAndAppend('div', root, { id: 'list-container' }); |
| 72 | + fetchAndRender(queries[0].url, container); |
71 | 73 | } |
72 | 74 |
|
73 | | - window.onload = main; |
| 75 | + const QUERIES = [ |
| 76 | + { |
| 77 | + description: 'All Dutch lauerates', |
| 78 | + url: 'http://api.nobelprize.org/v1/laureate.json?bornCountryCode=NL' |
| 79 | + }, |
| 80 | + { |
| 81 | + description: 'All female laureates', |
| 82 | + url: 'http://api.nobelprize.org/v1/laureate.json?gender=female' |
| 83 | + } |
| 84 | + ]; |
| 85 | + |
| 86 | + window.onload = () => main(QUERIES); |
74 | 87 | } |
0 commit comments