-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
90 lines (75 loc) · 2.85 KB
/
main.js
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
const APIURL = "https://api.github.com/users/";
const Main = document.querySelector("main");
const form = document.getElementById("form");
const search = document.getElementById("search");
// GET FOR USERNAME
async function getData(user) {
const resp = await fetch(APIURL + user);
const respData = await resp.json();
createCard(respData);
getRepos(user);
}
// GET REPO
async function getRepos(userName) {
const resp = await fetch(APIURL + userName + "/repos");
const respData = await resp.json();
if (respData.message == "Not Found") {
Main.innerHTML = "<h1>Not Found</h1>";
}
addRepostoCrad(respData);
}
// USERNAME CALL HERE
getData("ARSHADKHAN615");
// CREATE USER CARD
function createCard(user) {
const { avatar_url, name, bio, followers, following, public_repos } = user;
if (user.name !== null) {
const cardHtml =
`<div class="card">
<div>
<img src="${avatar_url}" alt="${name}" class="avatar">
</div>
<div class="info">
<div>
<h1>${name}</h1>
<p>${bio}</p>
</div>
<ul>
<li><b>Followers</b><ion-icon name="eye-outline" class="eye"></ion-icon><b>${followers}</b></li>
<li><b>Following</b><ion-icon name="heart" class="heart"></ion-icon><b>${following}</b></li>
<li><b>Repository</b><ion-icon name="albums"></ion-icon><b>${public_repos}</b></li>
</ul>
</div>
</div>
<div class="repos" id="repos">
<h1> TOP 10-GITHUB REPOSITORY</h1>
</div>
`
Main.innerHTML = cardHtml;
} else {
Main.innerHTML = "<h1>Result Not Found</h1>";
}
}
// CREATE REPO LINKS BOX
function addRepostoCrad(repoData) {
const reposEl = document.getElementById("repos");
repoData.sort((a, b) =>
b.stargazers_count - a.stargazers_count
).slice(0, 10)
.forEach(e => {
const repoEl = document.createElement("a");
repoEl.classList.add("repo");
repoEl.href = e.html_url;
repoEl.target = "_arshad";
repoEl.innerText = e.name;
reposEl.appendChild(repoEl);
});
console.log(repoData);
}
// SEARCH USERNAME
form.addEventListener("submit", function(e) {
e.preventDefault();
const userValue = search.value;
getData(userValue);
search.value = "";
})