-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathscript.js
135 lines (123 loc) · 4.99 KB
/
script.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
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
//https://api.github.com/users/user_name/repos
//
const user_img = document.querySelector(".user_img");
const userName = document.querySelector(".user_name h1");
const followers_ = document.querySelector(".followers_ span");
const follow_ = document.querySelector(".follow_ span");
const repo_details = document.querySelector(".repo_details");
const btn_submit = document.querySelector(".btn_submit");
let user_name = '';
//When user Writer user name in text Box
function inputFunction() {
let input_user = document.querySelector(".input_user").value.trim();
//trim method will replace before and after white space of the given value
if (input_user.length <= 0) {
alert("Please enter github user name");
document.querySelector(".input_user").value = "";
document.querySelector(".input_user").focus();
return false;
}
else {
user_name = input_user.split("").join("");
//if everything is ok run fetch user Function
fetchUser();
//clear the input box and focused it for next
document.querySelector(".input_user").value = "";
document.querySelector(".input_user").focus();
}
};
btn_submit.addEventListener("click", function () {
inputFunction()
});
//If user press "enter" it should be submit
document.querySelector(".input_user").addEventListener("keyup", function (e) {
if (e.keyCode === 13) {
//alert("you have pressed enter key ")
inputFunction()
}
})
//fetching user from github api
function fetchUser() {
fetch(`https://api.github.com/users/${user_name}`)
.then(response => response.json())
.then(function (data) {
console.log(data);
if (data.message === "Not Found") {
alert("user not found");
return false;
} else {
user_img.innerHTML = `<img src="${data.avatar_url}">`;
userName.innerHTML = data.login;
followers_.innerHTML = data.followers;
follow_.innerHTML = data.following;
}
})
//fetching repo
fetch(`https://api.github.com/users/${user_name}/repos`)
.then(response => response.json())
.then(function (repo_data) {
console.log(repo_data);
//if user type random name which is user but not have repository
if (repo_data.length <= 0) {
repo_details.innerHTML = `
<div class ="item_">
<div class ="repo_name">No Repo Found</div>
</div>
`
} else {
//when you type random user name if user and repo both not found
if (repo_data.message === "Not Found") {
repo_details.innerHTML = `
<div class="item_">
<div class="repo_name">Repo name</div>
<div class="repo_details_">
<div class="info_star">
<i class="fa fa-star-o"></i>-
</div>
<div class="info_fork">
<p><i class="fa fa-code-fork"></i>-</p>
</div>
<div class="info_size">
<p><i class="fa fa-file"></i>-kb</p>
</div>
</div>
</div>
`
user_img.innerHTML = `<img src="images/github_logo.png">`;
userName.innerHTML = `Not Found`;
followers_.innerHTML = "-";
follow_.innerHTML = "-";
} else {
let repo_Data = repo_data.map(item => {
console.log(item);
return (
`
<div class="item_">
<div class="repo_name">${item.name}</div>
<div class="repo_details_">
<div class="info_star">
<i class="fa fa-star-o"></i>
${item.watchers}
</div>
<div class="info_fork">
<p><i class="fa fa-code-fork"></i>
${item.forks}
</p>
</div>
<div class="info_size">
<p><i class="fa fa-file"></i>
${item.size}kb
</p>
</div>
</div>
</div>
`
);
})
//max repos taken is 6
// can take according as many repos to your requirenmnet
repo_details.innerHTML = repo_Data.slice(0, 6).join("");
}
}
});
}