|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +{ |
| 4 | + const { createAndAppend } = window.Util; |
| 5 | + |
| 6 | + function LaureatesView(container) { |
| 7 | + this.container = container; |
| 8 | + } |
| 9 | + |
| 10 | + LaureatesView.prototype.update = function(state) { |
| 11 | + if (!state.error && state.laureates != null) { |
| 12 | + this.render(state.laureates); |
| 13 | + } |
| 14 | + }; |
| 15 | + |
| 16 | + LaureatesView.prototype.render = function(laureates) { |
| 17 | + this.container.innerHTML = ''; |
| 18 | + if (laureates.length === 0) { |
| 19 | + createAndAppend('h4', this.container, { |
| 20 | + text: 'No laureates found', |
| 21 | + }); |
| 22 | + return; |
| 23 | + } |
| 24 | + const ul = createAndAppend('ul', this.container, { |
| 25 | + id: 'list-container', |
| 26 | + }); |
| 27 | + laureates.forEach(laureate => { |
| 28 | + const { surname, firstname } = laureate; |
| 29 | + const li = createAndAppend('li', ul, { |
| 30 | + class: 'list-item', |
| 31 | + }); |
| 32 | + const table = createAndAppend('table', li); |
| 33 | + const tbody = createAndAppend('tbody', table); |
| 34 | + this.addRow(tbody, 'Name', `${firstname} ${surname || ''} `); |
| 35 | + this.addRow( |
| 36 | + tbody, |
| 37 | + 'Born', |
| 38 | + `${laureate.born}, ${laureate.bornCity}, ${laureate.bornCountry}`, |
| 39 | + ); |
| 40 | + if (laureate.died !== '0000-00-00') { |
| 41 | + this.addRow( |
| 42 | + tbody, |
| 43 | + 'Died', |
| 44 | + `${laureate.died}, ${laureate.diedCity}, ${laureate.diedCountry}`, |
| 45 | + ); |
| 46 | + } |
| 47 | + this.renderLaureatePrizes(tbody, laureate.prizes); |
| 48 | + }); |
| 49 | + }; |
| 50 | + |
| 51 | + LaureatesView.prototype.renderLaureatePrizes = function(tbody, prizes) { |
| 52 | + const tr = createAndAppend('tr', tbody); |
| 53 | + createAndAppend('th', tr, { text: 'Prizes:', class: 'label' }); |
| 54 | + const td = createAndAppend('td', tr); |
| 55 | + const ul = createAndAppend('ul', td); |
| 56 | + prizes.forEach(prize => { |
| 57 | + const li = createAndAppend('li', ul); |
| 58 | + createAndAppend('span', li, { |
| 59 | + text: `${prize.year}, ${prize.category}`, |
| 60 | + }); |
| 61 | + if (prize.motivation) { |
| 62 | + createAndAppend('span', li, { |
| 63 | + text: `: ${prize.motivation}`, |
| 64 | + class: 'motivation', |
| 65 | + }); |
| 66 | + } |
| 67 | + }); |
| 68 | + }; |
| 69 | + |
| 70 | + LaureatesView.prototype.addRow = function(tbody, label, value) { |
| 71 | + const row = createAndAppend('tr', tbody); |
| 72 | + createAndAppend('th', row, { |
| 73 | + text: `${label}:`, |
| 74 | + class: 'label', |
| 75 | + }); |
| 76 | + createAndAppend('td', row, { text: value }); |
| 77 | + return row; |
| 78 | + }; |
| 79 | + |
| 80 | + window.LaureatesView = LaureatesView; |
| 81 | +} |
0 commit comments