|
2 | 2 |
|
3 | 3 | { |
4 | 4 | function fetchJSON(url, cb) { |
5 | | - const xhr = new XMLHttpRequest(); |
6 | | - xhr.open('GET', url); |
7 | | - xhr.responseType = 'json'; |
8 | | - xhr.onload = () => { |
9 | | - if (xhr.status >= 200 && xhr.status <= 299) { |
10 | | - cb(null, xhr.response); |
11 | | - } else { |
12 | | - cb(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`)); |
13 | | - } |
14 | | - }; |
15 | | - xhr.onerror = () => cb(new Error('Network request failed')); |
16 | | - xhr.send(); |
| 5 | + fetch(url) |
| 6 | + .then((response) => { |
| 7 | + if (!response.ok) { |
| 8 | + throw new Error( |
| 9 | + `Network error: ${response.status} - ${response.statusText}`, |
| 10 | + ); |
| 11 | + } |
| 12 | + return response.json(); |
| 13 | + }) |
| 14 | + .then((data) => cb(null, data)) |
| 15 | + .catch((error) => cb(error, null)); |
17 | 16 | } |
18 | 17 |
|
19 | 18 | function createAndAppend(name, parent, options = {}) { |
|
26 | 25 | else if (key === 'avatar') { |
27 | 26 | elem.innerHTML += `<img src="${value}"></img>` |
28 | 27 | } |
29 | | - else if (key === 'username') { |
30 | | - elem.innerHTML += `<span>${value}</span>` |
31 | | - } |
32 | | - else if (key === 'count') { |
| 28 | + else if (key === 'username' || key === 'count') { |
33 | 29 | elem.innerHTML += `<span>${value}</span>` |
34 | 30 | } |
35 | 31 | else { |
|
40 | 36 | } |
41 | 37 |
|
42 | 38 | function main(url) { |
43 | | - const header = document.createElement('header'); |
| 39 | + const header = document.querySelector('header'); |
44 | 40 | const heading = document.createElement('h1'); |
45 | 41 | heading.innerText = 'HYF Repositories'; |
46 | 42 | header.prepend(heading); |
|
51 | 47 | firstOption.appendChild(firstOptionContent); |
52 | 48 | selectBox.appendChild(firstOption); |
53 | 49 | header.appendChild(selectBox); |
54 | | - document.body.prepend(header); |
| 50 | + const repoContainer = document.querySelector('.repo-container'); |
| 51 | + const contributorsContainer = document.querySelector('.contributors-container'); |
| 52 | + const repoDetails = createAndAppend('ul', repoContainer); |
| 53 | + const repoContributions = createAndAppend('ul', contributorsContainer); |
55 | 54 | fetchJSON(url, (err, repos) => { |
56 | | - const root = document.getElementById('root'); |
| 55 | + const main = document.querySelector('.main-container'); |
57 | 56 | if (err) { |
58 | | - createAndAppend('div', root, { |
| 57 | + createAndAppend('div', main, { |
59 | 58 | text: err.message, |
60 | 59 | class: 'alert-error', |
61 | 60 | }); |
62 | 61 | return; |
63 | 62 | } |
64 | | - const repoDetails = createAndAppend('ul', root); |
65 | | - const repoContributions = createAndAppend('ul', root); |
66 | 63 | repos.sort((a, b) => a.name.localeCompare(b.name, 'en', { sensitivity: 'base' })); |
67 | 64 | repos.forEach(repo => { |
68 | 65 | const repoOptions = document.createElement("option"); |
|
78 | 75 | repoDetails.textContent = ''; |
79 | 76 | repoContributions.textContent = ''; |
80 | 77 | const selectedRepo = selectBox.options[selectBox.selectedIndex].value; |
81 | | - const filteredRepo = repos.filter(a => a.id == selectedRepo); |
| 78 | + const filteredRepo = repos.filter(repo => repo.id == selectedRepo); |
82 | 79 | createAndAppend('li', repoDetails, { Repository: filteredRepo[0].name, Description: filteredRepo[0].description, Forks: filteredRepo[0].forks, Updated: filteredRepo[0].updated_at }) |
83 | 80 | fetchJSON(filteredRepo[0].contributors_url, (err, reposContributors) => { |
84 | 81 | if (err) { |
85 | | - createAndAppend('div', root, { |
| 82 | + createAndAppend('div', main, { |
86 | 83 | text: err.message, |
87 | 84 | class: 'alert-error', |
88 | 85 | }); |
|
0 commit comments