forked from Technigo/project-github-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
399 lines (254 loc) · 10.8 KB
/
script.js
File metadata and controls
399 lines (254 loc) · 10.8 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// List to do
// A list of all repos that are forked ones from Technigo DONE
// Your username and profile picture - DONE
// Most recent update (push) for each repo DONE
// Name of your default branch for each repo - DONE
// URL to the actual GitHub repo - DONE
// Number of commit messages for each repo DONE
// All pull requests DONE
// A chart of how many projects you've done so far, compared to how many you will do using Chart.js.
// Filter board DONE
// Search Bar DONE
///////// DOM MANIPULATION //////////
const user = document.querySelector(".user-info");
const projects = document.querySelector("#projects");
const comments = document.querySelector(".comments");
const commits = document.querySelector(".commits");
const closeModal = document.querySelector(".close-modal-btn");
const modalBackground = document.querySelector(".see-more");
const filterBtn = document.getElementById("filter");
const searchBtn = document.getElementById("search");
///////// GLOBAL VARIABLES //////////
let reponame;
let colorNumber = 0;
const colors = ["#F3E0F0", "#D6CDE9"];
///////// ABOUT API /////////
const username = "sukiphan97";
const API_URL = `https://api.github.com/users/${username}/repos`;
const options = {
method: "GET",
headers: {
Authorization: `token ${API_TOKEN}`
}
}
///////// FETCH FIRST API TO GET USER INFO AND PROJECTS //////////
fetch (API_URL)
.then(res => res.json())
.then(data => {
//To get data and display user infor
userInfo(data[0]);
//To get only projects forked from Technigo
const datafilter = data.filter(item => item.name.startsWith("project") && item.fork)
// Draw the chart
updateChart(datafilter.length);
// Adding event to filter button
filterBtn.addEventListener("change", () => {
// To get access to different option groups
const optgroup = filterBtn.options[filterBtn.selectedIndex].parentNode.label;
if (optgroup === "language") {
filterLanguage(datafilter, filterBtn.value);
} else {
filterTime(datafilter)
}
}
)
//////// Search Button ////////
searchBtn.addEventListener("search", (e) => {
searchRepo(datafilter, searchBtn.value)
}
)
/////// Generate main dashboard when user reload the page ///////
data.forEach(project => {
reponame = project.name;
if (datafilter) {
gitDashBoard(project)
}
})
})
/////// Function to filter language ///////
const filterLanguage = (datafilter, language) => {
projects.innerHTML = "";
// This variable is to store the filtered value below
let repoLanguage = [];
if (language === "JavaScript" || language === "HTML") {
repoLanguage = datafilter.filter(item => item.language === language);
} else {
repoLanguage = datafilter;
}
// To loop over the filtered array and display only selected language
repoLanguage.forEach(item => {
reponame = item.name;
gitDashBoard(item);
})
}
/////// Function to filter lasted-update projects ///////
const filterTime = (datafilter) => {
// To clear the board before applying new filter
projects.innerHTML = "";
// To organize the projects' card from newest to oldes one
datafilter.sort((a,b) => {
return new Date(a.pushed_at) < new Date(b.pushed_at) ? 1 : -1;
})
// Display the filtered card
datafilter.forEach(item => {
reponame = item.name;
gitDashBoard(item);
})
}
/////// Seach bar ///////
const searchRepo = (data,input) => {
projects.innerHTML="";
let inputName = input.replaceAll(" ", "-")
const projectName = data.filter(item => item.name === inputName.toLowerCase())
projectName.forEach(item => {
reponame = item.name;
gitDashBoard(item);
getColor(item);
countTime(item);
technigoPR(reponame)
})
}
/////// Function to display username and other user info ///////
const userInfo = (data) => {
const userPicture = data.owner.avatar_url;
user.innerHTML += `
<div class="header-container">
<div>
<img src=${userPicture}/>
<a href=${data.owner.html_url} class="user-name"> ${username} </a>
</div>
<a href="https://github.com/sukiphan97" class="cta-btn" target="_blank"> CONTACT </a>
</div>
<h1> Welcome to my Github Tracker <span class="hand-waiving"> 👋 <span></h1>
`
}
/////// Function to generate project board ///////
const gitDashBoard = (project) => {
reponame = project.name;
// This condition is to display only forked projects from Technigo
if (project.fork === true && reponame.startsWith("project-")) {
const repoName = reponame.split("-");
const repoTitle = repoName.map(letter => letter[0].toUpperCase() + letter.slice(1));
// Update HTML
projects.innerHTML += `
<div class="repo" id=${reponame}>
<div class="repo-header">
<a class="repo-title" href=${project.html_url}> ${repoTitle.join(" ")} - ${project.default_branch}</a>
<p id="${reponame}-time" class="time">Updated </p>
</div>
<div>
<span class= "${reponame}-language language"> ${project.language} </span>
<button id= "${reponame}-modal-btn" class="open-modal-btn" type="button" > See more </button>
</div>
</div>
`
}
// All functions to display other datas and style the projects'card
getColor(project);
countTime(project);
styling(project);
technigoPR(reponame)
}
///////// FETCH TECHNIGO'S PULL REQUEST //////////
const technigoPR = (reponame) => {
fetch(`https://api.github.com/repos/Technigo/${reponame}/pulls?per_page=100`, options)
.then(res => res.json())
.then(data => {
// Adding event for "See more" button, when user clicks it, a modal will popup
document.getElementById(`${reponame}-modal-btn`).addEventListener("click", () =>{
data.forEach(item => {
if (item.user.login === username) {
getCommits(item.commits_url, reponame)
getComments(item.review_comments_url, reponame)
}
else if (item.user.login === "emmahogberg88" && reponame === "project-weather-app") {
getComments(item.review_comments_url, reponame)
}
})
})
})
}
/////// Function to fetch comments's API link ///////
const getComments = (comments_url, reponame) => {
fetch(comments_url)
.then (res => res.json())
.then (data => {
comments.innerHTML = "";
if (data.length === 0) {
comments.innerHTML = `<p> No comment for this project </p> `
} else if (data.length > 0) {
data.forEach(item => comments.innerHTML += `<li> ${item.body} </li>`)
}
// To remove the hidden class and show the modal
document.querySelector(".see-more").style.display = "flex";
})
}
/////// Function to fetch commits's API link ///////
const getCommits = (commits_url, reponame) => {
fetch(commits_url,options)
.then(res => res.json())
.then(data => {
if (data.length > 0) {
commits.innerHTML = `📌 Number of commits: ${data.length}`;
} else {
commits.innerHTML = `📌 Number of commits: 0`;
}
})
}
/////// Function to count since when the project was updated ///////
const countTime = (project) => {
// Get pushed time
const pushDate = new Date(project.pushed_at);
// Get current time
const today = new Date();
// Count time, seconds, minutes, hours, days
const sinceTime = (today - pushDate);
const seconds = Math.round( sinceTime / 1000 );
const minutes = Math.round( seconds / 60 );
const hours = Math.round( minutes/60 );
const days = Math.round( hours/24 );
const months = Math.round( days/30 );
const updateTime = document.getElementById(`${reponame}-time`);
//Filter the time and how to display it
if (minutes < 1) {
updateTime.innerHTML += `${seconds} ago`;
}
else if ( minutes >= 1 && minutes < 60) {
updateTime.innerHTML += `${minutes} ago`;
}
else if ( minutes >= 60 && hours < 24) {
updateTime.innerHTML += `${hours} hours ago`;
}
else if (hours >= 24 && days <= 30) {
updateTime.innerHTML += `${days} days ago`;
}
else if (days > 30 && months < 12) {
updateTime.innerHTML += `${months} months ago`;
}
else {
updateTime.innerHTML += `${sinceTime.toDateString()}`;
}
}
/////// Function apply different colors for the projects'card///////
const getColor = (project) => {
colorNumber++;
if (colorNumber > colors.length - 1) {
colorNumber = 0;
}
document.getElementById(reponame).style.background = colors[colorNumber];
}
/////// Function style the language tag///////
const styling = (project) => {
if(project.language === "JavaScript") {
document.querySelector(`.${reponame}-language`).style.background = "#7365e5"
} else {
document.querySelector(`.${reponame}-language`).style.background = "#333"
}
}
/////// Adding event for modal close button///////
closeModal.addEventListener("click", () => {
modalBackground.style.display = "none";
})
modalBackground.addEventListener("click", () => {
modalBackground.style.display = "none";
})