Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b08b62b

Browse files
committed
JS3 - Week3 - Part 1
1 parent 591f142 commit b08b62b

File tree

2 files changed

+54
-74
lines changed

2 files changed

+54
-74
lines changed

homework/index.js

Lines changed: 50 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
'use strict';
22

33
{
4-
function fetchJSON(url) {
5-
return new Promise((resolve, reject) => {
6-
const xhr = new XMLHttpRequest();
7-
xhr.open('GET', url);
8-
xhr.responseType = 'json';
9-
xhr.onload = () => {
10-
if (xhr.status >= 200 && xhr.status <= 299) {
11-
resolve(xhr.response);
12-
} else {
13-
reject(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`));
14-
}
15-
};
16-
xhr.onerror = () => reject(new Error('Network request failed'));
17-
xhr.send();
18-
});
4+
async function fetchJSON(url) {
5+
const response = await fetch(url);
6+
if (!response.ok) {
7+
throw new Error(
8+
`Network error: ${response.status} - ${response.statusText}`,
9+
);
10+
}
11+
return response.json();
1912
}
2013

2114
function createAndAppend(name, parent, options = {}) {
@@ -58,36 +51,29 @@
5851

5952
const root = document.getElementById('root');
6053

61-
function renderContributors(repo, div) {
62-
fetchJSON(repo.contributors_url)
63-
.then(contributors => {
64-
div.innerHTML = '';
65-
66-
contributors.forEach(contributor => {
67-
const contributorDiv = createAndAppend('div', div, {
68-
class: 'contributor-details',
69-
});
70-
createAndAppend('img', contributorDiv, {
71-
src: contributor.avatar_url,
72-
});
73-
createAndAppend('a', contributorDiv, {
74-
text: contributor.login,
75-
href: contributor.html_url,
76-
target: '_blank',
77-
class: 'user-name',
78-
});
79-
createAndAppend('div', contributorDiv, {
80-
text: contributor.contributions,
81-
class: 'contributions-count',
82-
});
54+
async function renderContributors(repo, div) {
55+
try {
56+
const contributors = await fetchJSON(repo.contributors_url);
57+
div.innerHTML = '';
58+
contributors.forEach(contributor => {
59+
const contributorDiv = createAndAppend('div', div, {
60+
class: 'contributor-details',
61+
});
62+
createAndAppend('img', contributorDiv, { src: contributor.avatar_url });
63+
createAndAppend('a', contributorDiv, {
64+
text: contributor.login,
65+
href: contributor.html_url,
66+
target: '_blank',
67+
class: 'user-name',
8368
});
84-
})
85-
.catch(err => {
86-
createAndAppend('div', root, {
87-
text: err.message,
88-
class: 'alert-error',
69+
createAndAppend('div', contributorDiv, {
70+
text: contributor.contributions,
71+
class: 'contributions-count',
8972
});
9073
});
74+
} catch (err) {
75+
createAndAppend('div', root, { text: err.message, class: 'alert-error' });
76+
}
9177
}
9278

9379
function renderOptionElements(repos, select) {
@@ -96,49 +82,43 @@
9682
});
9783
}
9884

99-
function main(url) {
85+
async function main(url) {
10086
const header = createAndAppend('header', root);
10187
createAndAppend('h2', header, { text: 'HYF Repositories' });
10288
const select = createAndAppend('select', header, {
103-
id: 'select-btn',
89+
id: 'btn',
10490
title: 'Select Repository',
10591
});
106-
const mainContainer = createAndAppend('div', root, {
107-
id: `main-container`,
92+
const mainCont = createAndAppend('div', root, { id: `main-cont` });
93+
const reposCont = createAndAppend('section', mainCont, {
94+
id: 'repos-cont',
10895
});
109-
const reposContainer = createAndAppend('section', mainContainer, {
110-
id: 'repos-container',
96+
const contributorsCont = createAndAppend('section', mainCont, {
97+
id: 'contributors-cont',
11198
});
112-
const contributorsContainer = createAndAppend('section', mainContainer, {
113-
id: 'contributors-container',
114-
});
115-
createAndAppend('p', contributorsContainer, {
99+
createAndAppend('p', contributorsCont, {
116100
text: `Contributors:`,
117101
id: 'contributors-title',
118102
});
119-
const div = createAndAppend('div', contributorsContainer, {
103+
const div = createAndAppend('div', contributorsCont, {
120104
id: 'list-contributions',
121105
});
122106

123-
fetchJSON(url)
124-
.then(repos => repos.sort((a, b) => a.name.localeCompare(b.name)))
125-
.then(repos => {
126-
renderOptionElements(repos, select);
127-
renderRepoDetails(repos[0], reposContainer);
128-
renderContributors(repos[0], div);
107+
try {
108+
const repos = await fetchJSON(url);
109+
repos.sort((a, b) => a.name.localeCompare(b.name));
110+
renderOptionElements(repos, select);
111+
renderRepoDetails(repos[0], reposCont);
112+
renderContributors(repos[0], div);
129113

130-
select.addEventListener('change', () => {
131-
const repo = repos[select.value];
132-
renderRepoDetails(repo, reposContainer);
133-
renderContributors(repo, div);
134-
});
135-
})
136-
.catch(err => {
137-
createAndAppend('div', root, {
138-
text: err.message,
139-
class: 'alert-error',
140-
});
114+
select.addEventListener('change', () => {
115+
const repo = repos[select.value];
116+
renderRepoDetails(repo, reposCont);
117+
renderContributors(repo, div);
141118
});
119+
} catch (err) {
120+
createAndAppend('div', root, { text: err.message, class: 'alert-error' });
121+
}
142122
}
143123

144124
const HYF_REPOS_URL =

homework/style.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ h2 {
3737
padding: 45px 0 0 25px;
3838
}
3939

40-
#select-btn {
40+
#btn {
4141
width: 240px;
4242
height: 30px;
4343
margin: 10px 0 0 25px;
@@ -52,13 +52,13 @@ h2 {
5252
background-position-x: 216px;
5353
}
5454

55-
#main-container {
55+
#main-cont {
5656
display: grid;
5757
grid-template-columns: 1fr 1fr;
5858
grid-gap: 10px;
5959
}
6060

61-
#repos-container {
61+
#repos-cont {
6262
text-align: left;
6363
border-bottom: 1px solid #adadad;
6464
background-color: #ffffff;
@@ -89,7 +89,7 @@ img {
8989
border-radius: 5px;
9090
}
9191

92-
#contributors-container {
92+
#contributors-cont {
9393
border-bottom: 1px solid #adadad;
9494
background-color: #ffffff;
9595
padding: 20px 20px 0 20px;

0 commit comments

Comments
 (0)