forked from HackYourFuture/JavaScript3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
93 lines (81 loc) · 2.84 KB
/
index.js
File metadata and controls
93 lines (81 loc) · 2.84 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
"use strict";
let username = "HackYourFuture";
let mainRepo;
let url = `https://api.github.com/users/${username}/repos`;
let ul = document.getElementById("list");
let repo_title = document.getElementById("card-title");
let repo_description = document.getElementById("card-description");
let repo_forks = document.getElementById("card-forks");
let repo_updated = document.getElementById("card-updated");
let dropdown = document.getElementById("gitContributors");
dropdown.length = 0;
let defaultOption = document.createElement("option");
defaultOption.text = "Choose Repo";
dropdown.add(defaultOption);
dropdown.selectedIndex = 0;
fetch(url)
.then(response => response.json())
.then(data => {
let option;
for (let i = 0; i < data.length; i++) {
option = document.createElement("option");
option.text = data[i].name;
option.value = i;
dropdown.add(option);
}
mainRepo = data;
})
.catch(error => console.error(error));
// Get Contributors
function getContributors(repo) {
let contributorURL = `https://api.github.com/repos/hackyourfuture/${repo}/contributors`;
//Ul clearing
if (ul.hasChildNodes()) {
while (ul.hasChildNodes()) {
ul.removeChild(ul.firstChild);
}
}
//Repo Information
repo_title.innerHTML = dropdown.options[dropdown.selectedIndex].text;
repo_description.innerHTML =
mainRepo[dropdown.options[dropdown.selectedIndex].value].description;
repo_forks.innerHTML =
mainRepo[dropdown.options[dropdown.selectedIndex].value].forks;
repo_updated.innerHTML = dateConverter(
mainRepo[dropdown.options[dropdown.selectedIndex].value].updated_at
);
//Fetching contributors list
fetch(contributorURL)
.then(response => response.json())
.then(data => {
console.log(data);
data.map( e => {
let a = document.createElement('a');
a.setAttribute('target','_blank');
a.href = e.html_url;
let li = document.createElement("li");
li.className ="list-group-item d-flex justify-content-between align-items-center";
let title = document.createElement("h6");
let span = document.createElement("span");
span.className = "badge badge-success"
span.appendChild(document.createTextNode(e.contributions));
let img = document.createElement('img');
img.src = e.avatar_url;
img.setAttribute("width", "50");
img.setAttribute("height", "50");
img.setAttribute("alt", e.login);
li.appendChild(img);
title.innerHTML = e.login;
li.appendChild(title);
li.appendChild(span);
a.appendChild(li)
ul.appendChild(a);
});
})
.catch(error => console.error(error));
}
//Helping functions
function dateConverter(date) {
let newDate = new Date(date);
return `${newDate.toLocaleDateString()} - ${newDate.toLocaleTimeString()}`;
}