|
1 | | -/* |
2 | | - Render laureate details and add CSS styling. |
3 | | -*/ |
4 | | - |
5 | 1 | 'use strict'; |
6 | 2 |
|
7 | 3 | { |
8 | 4 | const API_BASE_URL = 'http://api.nobelprize.org/v1'; |
9 | 5 |
|
| 6 | + /** |
| 7 | + * |
| 8 | + * @param {string} url |
| 9 | + * @param {(err: Error, data: any) => void} cb |
| 10 | + */ |
10 | 11 | function fetchJSON(url, cb) { |
11 | 12 | const xhr = new XMLHttpRequest(); |
12 | 13 | xhr.open('GET', url); |
|
22 | 23 | xhr.send(); |
23 | 24 | } |
24 | 25 |
|
| 26 | + /** |
| 27 | + * |
| 28 | + * @param {string} name |
| 29 | + * @param {HTMLElement} parent |
| 30 | + * @param {{[key: string]: string}} options |
| 31 | + */ |
25 | 32 | function createAndAppend(name, parent, options = {}) { |
26 | 33 | const elem = document.createElement(name); |
27 | 34 | parent.appendChild(elem); |
|
35 | 42 | return elem; |
36 | 43 | } |
37 | 44 |
|
| 45 | + /** |
| 46 | + * |
| 47 | + * @param {Error} err |
| 48 | + */ |
38 | 49 | function renderError(err) { |
39 | 50 | const listContainer = document.getElementById('list-container'); |
40 | 51 | listContainer.innerHTML = ''; |
|
45 | 56 | }); |
46 | 57 | } |
47 | 58 |
|
| 59 | + /** |
| 60 | + * |
| 61 | + * @param {HTMLElement} tbody |
| 62 | + * @param {string} label |
| 63 | + * @param {string} value |
| 64 | + */ |
48 | 65 | function addRow(tbody, label, value) { |
49 | 66 | const tr = createAndAppend('tr', tbody); |
50 | 67 | createAndAppend('td', tr, { text: `${label}:`, class: 'label' }); |
51 | 68 | createAndAppend('td', tr, { text: value }); |
52 | 69 | } |
53 | 70 |
|
| 71 | + /** |
| 72 | + * |
| 73 | + * @param {HTMLElement} tbody |
| 74 | + * @param {any[]} prizes |
| 75 | + */ |
54 | 76 | function renderLaureatePrizes(tbody, prizes) { |
55 | 77 | const tr = createAndAppend('tr', tbody); |
56 | 78 | createAndAppend('td', tr, { text: 'Prizes:', class: 'label' }); |
|
68 | 90 | }); |
69 | 91 | } |
70 | 92 |
|
| 93 | + /** |
| 94 | + * |
| 95 | + * @param {any[]} laureates |
| 96 | + * @param {HTMLElement} listContainer |
| 97 | + */ |
71 | 98 | function renderLaureates(laureates, listContainer) { |
72 | 99 | laureates.forEach(laureate => { |
73 | 100 | const { surname, firstname } = laureate; |
|
80 | 107 | addRow( |
81 | 108 | tbody, |
82 | 109 | 'Born', |
83 | | - `${laureate.born}, ${laureate.bornCity}, ${laureate.bornCountry}`, |
| 110 | + `${laureate.born}, ${laureate.bornCity}, ${laureate.bornCountry}` |
84 | 111 | ); |
85 | 112 | if (laureate.died !== '0000-00-00') { |
86 | 113 | addRow( |
87 | 114 | tbody, |
88 | 115 | 'Died', |
89 | | - `${laureate.died}, ${laureate.diedCity}, ${laureate.diedCountry}`, |
| 116 | + `${laureate.died}, ${laureate.diedCity}, ${laureate.diedCountry}` |
90 | 117 | ); |
91 | 118 | } |
92 | 119 | renderLaureatePrizes(tbody, laureate.prizes); |
93 | 120 | }); |
94 | 121 | } |
95 | 122 |
|
| 123 | + /** |
| 124 | + * |
| 125 | + * @param {string} countryCode |
| 126 | + * @param {HTMLElement} ul |
| 127 | + */ |
96 | 128 | function onChangeSelect(countryCode, ul) { |
97 | 129 | ul.innerHTML = ''; |
98 | 130 |
|
|
104 | 136 | return; |
105 | 137 | } |
106 | 138 | renderLaureates(data.laureates, ul); |
107 | | - }, |
| 139 | + } |
108 | 140 | ); |
109 | 141 | } |
110 | 142 |
|
|
114 | 146 | const header = createAndAppend('header', root); |
115 | 147 | const ul = createAndAppend('ul', root, { id: 'list-container' }); |
116 | 148 |
|
117 | | - const select = createAndAppend('select', header); |
| 149 | + /* prettier-ignore */ |
| 150 | + const select = /**@type HTMLSelectElement*/ (createAndAppend( |
| 151 | + 'select', |
| 152 | + header |
| 153 | + )); |
118 | 154 | createAndAppend('option', select, { |
119 | 155 | text: 'Select a country', |
120 | 156 | disabled: 'disabled', |
|
127 | 163 | return; |
128 | 164 | } |
129 | 165 |
|
130 | | - data.countries |
| 166 | + /**@type {{code: string, name: string}[]} */ |
| 167 | + const countries = data.countries; |
| 168 | + |
| 169 | + countries |
131 | 170 | .filter(country => country.code !== undefined) |
132 | 171 | .sort((a, b) => a.name.localeCompare(b.name)) |
133 | 172 | .forEach(country => { |
|
0 commit comments