forked from HackYourFuture/JavaScript3
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.js
More file actions
153 lines (145 loc) · 5.86 KB
/
index.js
File metadata and controls
153 lines (145 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const mainDiv = document.body;
function createElement(parentElement, element, nameId) {
const newElement = document.createElement(element);
newElement.setAttribute('id', nameId);
parentElement.appendChild(newElement);
return newElement;
}
const fetchRepositories = async url => {
const response = await fetch(url);
const dataList = await response.json();
return dataList;
};
const createOption = (item, index) => {
const newOption = document.createElement('option');
const repositoriesSelect = document.getElementById('repositoriesSelect');
newOption.className = 'repositoryOption';
newOption.text = item.name;
newOption.value = index;
repositoriesSelect.appendChild(newOption);
};
function appendToLi(indexLi, element, nameIdP1, textNodeP1, nameIdP2, textNodeP2) {
const p1 = createElement(indexLi, element, nameIdP1);
const contentP1 = document.createTextNode(textNodeP1);
p1.appendChild(contentP1);
const p2 = createElement(indexLi, element, nameIdP2);
const contentP2 = document.createTextNode(textNodeP2);
p2.appendChild(contentP2);
}
function createList(parentElement, element, lio, li1, li2, li3, aUrl) {
for (let y = 0; y < 4; y++) {
createElement(parentElement, element, `li${y}`);
}
const liTags = document.getElementsByTagName('li');
const p0 = createElement(liTags[0], 'p', 'repository');
const contentP0 = document.createTextNode('Repository:');
p0.appendChild(contentP0);
const aElement = createElement(liTags[0], 'a', 'repositoryValue');
aElement.href = aUrl;
const aContent = document.createTextNode(lio);
aElement.appendChild(aContent);
appendToLi(liTags[1], 'p', 'description', 'Description:', 'descriptionValue', li1);
appendToLi(liTags[2], 'p', 'forks', 'Forks:', 'forksValue', li2);
appendToLi(liTags[3], 'p', 'updated', 'Updated:', 'updatedValue', li3);
}
function handleContributors(contributors) {
const rightDiv = document.getElementById('rightDiv');
rightDiv.innerHTML = '';
const contributorsTitle = createElement(rightDiv, 'div', 'contributorsTitle');
const contributorsTitleContent = document.createTextNode('Contributors');
contributorsTitle.appendChild(contributorsTitleContent);
contributors.forEach(contributor => {
// create subDiv for each contributor
const subDiv = document.createElement('div');
subDiv.className = 'contributor';
rightDiv.appendChild(subDiv);
const image = document.createElement('img');
image.className = 'image';
image.setAttribute('src', contributor.avatar_url);
subDiv.appendChild(image);
const login = createElement(subDiv, 'p', 'login');
const loginContent = document.createTextNode(contributor.login);
login.appendChild(loginContent);
const contributions = createElement(subDiv, 'p', 'contributions');
const contributionsContent = document.createTextNode(contributor.contributions);
contributions.appendChild(contributionsContent);
});
}
const logContributors = async url => {
try {
const contributorsData = await fetchRepositories(url);
const FooCodingContributors = contributorsData.sort((a, b) =>
a.login.localeCompare(b.login, 'fr', { ignorePunctuation: true }),
);
handleContributors(FooCodingContributors);
} catch (error) {
const errorDiv = createElement(mainDiv, 'div', 'errorDiv');
const errorContent = document.createTextNode('error');
errorDiv.appendChild(errorContent);
}
};
function createPage(repositories) {
// create upperDiv
const upperDiv = createElement(mainDiv, 'div', 'upperDiv');
const upperDivP = createElement(upperDiv, 'p', 'upperDivP');
const upperDivPContent = document.createTextNode('FooCoding Repositories');
upperDivP.appendChild(upperDivPContent);
const select = createElement(upperDiv, 'select', 'repositoriesSelect');
repositories.forEach(createOption);
// create leftRightDivDiv
const leftRightDiv = createElement(mainDiv, 'div', 'leftRightDiv');
// create leftDiv
const leftDiv = createElement(leftRightDiv, 'div', 'leftDiv');
const repositoryDetails = createElement(leftDiv, 'ul', 'repositoryDetails');
const defaultRepository = repositories[0];
const updated = defaultRepository.updated_at;
const formatUpdated = updated.replace(/T/, ', ').replace(/Z/, '');
createList(
repositoryDetails,
'li',
defaultRepository.name,
defaultRepository.description,
defaultRepository.forks,
formatUpdated,
defaultRepository.html_url,
);
// create rightDiv
createElement(leftRightDiv, 'div', 'rightDiv');
// fetch contributors to implement the rightDiv
const defaultContributorsUrl = defaultRepository.contributors_url;
logContributors(defaultContributorsUrl);
// addEventListener on select change
select.addEventListener('change', () => {
const liElement = document.querySelectorAll('li');
for (let i = 0; i < liElement.length; i++) {
liElement[i].parentElement.removeChild(liElement[i]);
}
const selectedRepository = repositories[select.value];
const updatedForSelect = selectedRepository.updated_at;
const formatUpdatedForSelect = updatedForSelect.replace(/T/, ', ').replace(/Z/, '');
createList(
repositoryDetails,
'li',
selectedRepository.name,
selectedRepository.description,
selectedRepository.forks,
formatUpdatedForSelect,
);
const ContributorsUrl = selectedRepository.contributors_url;
logContributors(ContributorsUrl);
});
}
const logData = async url => {
try {
const repositoriesData = await fetchRepositories(url);
const FooCodingRepositories = repositoriesData.sort((a, b) =>
a.name.localeCompare(b.name, 'fr', { ignorePunctuation: true }),
);
createPage(FooCodingRepositories);
} catch (error) {
const errorDiv = createElement(mainDiv, 'div', 'errorDiv');
const errorContent = document.createTextNode('error');
errorDiv.appendChild(errorContent);
}
};
logData('https://api.github.com/orgs/foocoding/repos?per_page=100');