Skip to content

Commit 3b41b4f

Browse files
week2 works added to homework
1 parent d3a679a commit 3b41b4f

2 files changed

Lines changed: 116 additions & 18 deletions

File tree

homework/index.js

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,37 @@
2121
parent.appendChild(elem);
2222
Object.entries(options).forEach(([key, value]) => {
2323
if (key === 'Repository' || key === 'Description' || key === 'Forks' || key === 'Updated') {
24-
elem.innerHTML += `<strong>${key}:</strong> ${value} <br>`;
25-
} else {
24+
elem.innerHTML += `<strong>${key}:</strong> ${value} <br> `;
25+
}
26+
else if (key === 'foto') {
27+
elem.innerHTML += `<img src="${value}"></img>`
28+
}
29+
else if (key === 'name') {
30+
elem.innerHTML += `<span>${value}</span>`
31+
}
32+
else if (key === 'count') {
33+
elem.innerHTML += `<span>${value}</span>`
34+
}
35+
else {
2636
elem.setAttribute(key, value);
2737
}
2838
});
2939
return elem;
3040
}
31-
function renderRepoDetails(repo, ul) {
32-
createAndAppend('li', ul, { Repository: repo.name, Description: repo.description, Forks: repo.forks, Updated: repo.updated_at });
33-
}
3441

3542
function main(url) {
43+
const header = document.createElement('header');
3644
const heading = document.createElement('h1');
3745
heading.innerText = 'HYF Repositories';
38-
document.body.prepend(heading);
46+
header.prepend(heading);
47+
const selectBox = document.createElement('select');
48+
var firstOption = document.createElement("option");
49+
firstOption.setAttribute("value", 0);
50+
var firstOptionContent = document.createTextNode('Choose a Repository...');
51+
firstOption.appendChild(firstOptionContent);
52+
selectBox.appendChild(firstOption);
53+
header.appendChild(selectBox);
54+
document.body.prepend(header);
3955
fetchJSON(url, (err, repos) => {
4056
const root = document.getElementById('root');
4157
if (err) {
@@ -45,14 +61,43 @@
4561
});
4662
return;
4763
}
48-
const ul = createAndAppend('ul', root);
64+
const repoDetails = createAndAppend('ul', root);
65+
const repoContributions = createAndAppend('ul', root);
4966
repos.sort((a, b) => a.name.localeCompare(b.name, 'en', { sensitivity: 'base' }));
50-
// repos.slice(0, 10);
51-
repos.forEach(repo => renderRepoDetails(repo, ul));
67+
repos.forEach(repo => {
68+
const repoOptions = document.createElement("option");
69+
repoOptions.setAttribute("value", repo.id);
70+
const repoOptionsContent = document.createTextNode(repo.name);
71+
repoOptions.appendChild(repoOptionsContent);
72+
selectBox.appendChild(repoOptions);
73+
});
74+
if (selectBox.value === '0') {
75+
repoDetails.textContent = '';
76+
}
77+
selectBox.addEventListener('change', () => {
78+
repoDetails.textContent = '';
79+
repoContributions.textContent = '';
80+
const selectedRepo = selectBox.options[selectBox.selectedIndex].value;
81+
const filteredRepo = repos.filter(a => a.id == selectedRepo);
82+
createAndAppend('li', repoDetails, { Repository: filteredRepo[0].name, Description: filteredRepo[0].description, Forks: filteredRepo[0].forks, Updated: filteredRepo[0].updated_at })
83+
fetchJSON(filteredRepo[0].contributors_url, (err, reposContributors) => {
84+
if (err) {
85+
createAndAppend('div', root, {
86+
text: err.message,
87+
class: 'alert-error',
88+
});
89+
return;
90+
}
91+
reposContributors.forEach(contributors => {
92+
createAndAppend('li', repoContributions, { foto: contributors.avatar_url, name: contributors.login, count: contributors.contributions })
93+
});
94+
});
95+
});
5296
});
5397
}
5498

5599
const HYF_REPOS_URL =
56100
'https://api.github.com/orgs/HackYourFuture/repos?per_page=100';
101+
57102
window.onload = () => main(HYF_REPOS_URL);
58103
}

homework/style.css

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,77 @@
1-
body{
1+
body {
22
padding: 0;
33
margin: 0;
44
background-color: rgb(221, 221, 221);
55
}
6-
.alert-error {
7-
color: red;
8-
}
9-
h1{
10-
color:white;
6+
header {
7+
display: flex;
8+
flex-direction: row;
9+
align-items: center;
1110
background-color: blue;
11+
}
12+
h1 {
13+
color: white;
1214
margin: 0;
1315
padding: 1%;
1416
}
15-
ul{
17+
select {
18+
margin-left: 3%;
19+
height: 30px;
20+
}
21+
#root {
22+
display: flex;
23+
flex-direction: row;
24+
justify-content: space-between;
25+
}
26+
ul {
1627
list-style: none;
1728
padding: 0;
1829
margin: 1%;
30+
width: 100%;
1931
}
20-
li{
32+
li {
2133
padding: 1%;
2234
margin-bottom: 1%;
2335
background-color: white;
24-
}
36+
border: 1px solid rgb(196, 196, 196);
37+
border-radius: 5px;
38+
box-shadow: 3px 3px 3px grey;
39+
}
40+
ul:last-child > li {
41+
display: flex;
42+
flex-direction: row;
43+
justify-content: space-between;
44+
align-items: center;
45+
padding: 2% 10%;
46+
}
47+
ul:last-child > li img {
48+
width: 10%;
49+
border-radius: 10%;
50+
}
51+
ul:last-child > li span:last-child {
52+
padding: 1% 3%;
53+
background-color: grey;
54+
color: white;
55+
border-radius: 15%;
56+
box-shadow: 2px 2px 5px black;
57+
}
58+
.alert-error {
59+
color: red;
60+
}
61+
62+
@media all and (max-width: 600px) {
63+
header {
64+
flex-direction: column;
65+
}
66+
select {
67+
margin-bottom: 5%;
68+
}
69+
#root {
70+
flex-direction: column;
71+
padding: 2% 10%;
72+
}
73+
ul:last-child > li img {
74+
width: 25%;
75+
border-radius: 10%;
76+
}
77+
}

0 commit comments

Comments
 (0)