-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
168 lines (122 loc) · 5.99 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
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
let button = document.querySelector(".submit");
let searchBar = document.querySelector(".search-bar");
let spinner = document.querySelector(".spinners");
let userImage = document.querySelector(".img-profile")
let name = document.querySelector(".Username-name");
let userName = document.querySelector(".Username-username");
let userDetails = document.querySelector(".user-details");
let followers = document.querySelector(".followers");
let following = document.querySelector(".following");
let company = document.querySelector(".company");
let companyName = document.querySelector(".company-name");
let place = document.querySelector(".location");
let locationName = document.querySelector(".location-name");
let website = document.querySelector(".website");
let websiteName = document.querySelector(".website-name");
let twitter = document.querySelector(".twitter");
let twitterName = document.querySelector(".twitter-name");
let repoStat = document.querySelector(".repo-stat");
let main = document.querySelector(".container-color");
let repositoriesMain = document.querySelector(".repositories-stat");
const userProfile = () => {
main.style.display = "none";
spinner.style.display = "block";
async function getUsers() {
try{
const users = await fetch(`https://api.github.com/users/${searchBar.value}`);
const data = await users.json();
userImage.setAttribute("src", data.avatar_url);
name.innerHTML = data.name;
userName.innerHTML = data.login;
userDetails.innerHTML = data.bio;
followers.innerHTML = data.followers;
following.innerHTML = data.following;
repoStat.innerHTML = data.public_repos;
if (data.company !== null) {
companyName.innerHTML = data.company;
} else {
company.innerHTML= "";
}
if (data.location !== null) {
locationName.innerHTML = data.location;
} else {
place.innerHTML= "";
}
if (data.hasOwnProperty(website)) {
websiteName.innerHTML = data.blog;
console.log(data.website);
} else {
website.innerHTML= "";
}
if (data.twitter_username !== null) {
twitterName.innerHTML = data.twitter_username;
} else {
twitter.innerHTML= "";
}
const userRepo = await fetch(`https://api.github.com/users/${searchBar.value}/repos`);
const RepoDatas = await userRepo.json();
repositoriesMain.innerHTML= "";
RepoDatas.map(RepoData => {
let createRepoDiv = document.createElement("div");
createRepoDiv.setAttribute("class", "RepoDataDiv");
let RepoName = document.createElement("h3");
RepoName.setAttribute("class", "repo-name");
let RepoNameText = document.createTextNode(RepoData.name);
let starForkDiv = document.createElement("div");
starForkDiv.setAttribute("class", "starForkDiv");
let StarDiv = document.createElement("div");
StarDiv.setAttribute("class", "StarDiv");
let Star = document.createElement("i");
Star.setAttribute("class", "far fa-star repo-star");
let StarNumP = document.createElement("p");
let StarNum = document.createTextNode(RepoData.stargazers_count);
let ForkDiv = document.createElement("div");
ForkDiv.setAttribute("class", "ForkDiv");
let Fork = document.createElement("i");
Fork.setAttribute("class", "fas fa-code-branch repo-fork");
let ForkNumP = document.createElement("p");
let ForkNum = document.createTextNode(RepoData.forks);
let WatchDiv = document.createElement("div");
WatchDiv.setAttribute("class", "WatchDiv");
let Watch = document.createElement("i");
Watch.setAttribute("class", "fas fa-eye repo-eye");
let WatchNumP = document.createElement("p");
let WatchNum = document.createTextNode(RepoData.watchers);
RepoName.appendChild(RepoNameText);
createRepoDiv.appendChild(RepoName);
StarNumP.appendChild(StarNum);
StarDiv.appendChild(Star);
StarDiv.appendChild(StarNumP);
ForkNumP.appendChild(ForkNum);
ForkDiv.appendChild(Fork);
ForkDiv.appendChild(ForkNumP);
WatchNumP.appendChild(WatchNum);
WatchDiv.appendChild(Watch);
WatchDiv.appendChild(WatchNumP);
starForkDiv.appendChild(StarDiv);
starForkDiv.appendChild(ForkDiv);
starForkDiv.appendChild(WatchDiv);
createRepoDiv.appendChild(starForkDiv);
repositoriesMain.appendChild(createRepoDiv);
})
}catch(error){
main.innerHTML = "";
let errorDiv = document.createElement("div");
errorDiv.setAttribute("class", "errorDiv");
let errorName = document.createElement("h3");
let errorNameText = document.createTextNode(error);
let errorHelp = document.createElement("p");
let errorHelpText = document.createTextNode("Hey! Please crosscheck the username entered, your internet settings and try again.");
errorName.appendChild(errorNameText);
errorHelp.appendChild(errorHelpText);
errorDiv.appendChild(errorName);
errorDiv.appendChild(errorHelp);
main.appendChild(errorDiv)
}
spinner.style.display = "none";
main.style.display = "block";
searchBar.value = "";
}
getUsers();
}
button.addEventListener("click", userProfile);