-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
133 lines (122 loc) · 4.78 KB
/
app.js
File metadata and controls
133 lines (122 loc) · 4.78 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
const apiBaseURL = `https://api.github.com/users`;
const errorMessage = (status) => {
const messageDiv = document.querySelector('#message');
let errmsg = ``;
if (status === 404) {
errmsg = `<div class="alert-danger text-center"> Profile doesn't exist.</div>`;
}
messageDiv.innerHTML = errmsg;
setTimeout(() => (messageDiv.innerHTML = ``), 5000);
};
const getGitHubProfile = async (login) => {
try {
const response = await fetch(`${apiBaseURL}/${login}`);
if (response.status !== 200) {
if (response.status === 404) {
errorMessage(response.status);
}
new Error(`Something went wrong! Status Code: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.log(error);
}
};
const getGitRepos = async (login) => {
try {
const response = await fetch(`${apiBaseURL}/${login}/repos`);
if (response.status !== 200) {
new Error(`Something went wrong! Status Code: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.log(error);
}
};
// render github profile
const renderProfile = (data) => {
let profileSnnipet = ``;
profileSnnipet += `<div class="profile-userpic">
<img src="${data.avatar_url}" class="d-block">
</div>
<div class="profile-usertitle">`;
if (data.name !== null) {
profileSnnipet += `<div class="profile-usertitle-name">${data.name}</div>`;
}
profileSnnipet += `<div class="profile-usertitle-job">
${data.login}
</div>
</div>
<div class="portlet light bordered">
<!-- STAT -->
<div class="row list-separated profile-stat">
<div class="col-md-6 col-sm-6 col-xs-6">
<div class="uppercase profile-stat-title">${data.followers}</div>
<div class="uppercase profile-stat-text"> Followers </div>
</div>
<div class="col-md-6 col-sm-6 col-xs-6">
<div class="uppercase profile-stat-title">${data.following}</div>
<div class="uppercase profile-stat-text"> Following </div>
</div>
</div>`;
if (data.bio !== null) {
profileSnnipet += `<div><h4 class="profile-desc-title">About ${data.name}</h4>
<span class="profile-desc-text">${data.bio}</span></div>`;
}
if (data.twitter_username !== null) {
profileSnnipet += `<div class="margin-top-20 profile-desc-link">
<i class="fab fa-twitter"></i>
<a target="_blank" href="https://www.twitter.com/${data.twitter_username}">@${data.twitter_username}</a>
</div>`;
}
profileSnnipet += `</div>`;
document.querySelector("#profile").innerHTML = profileSnnipet;
};
// list git repos
const listRepos = (repos) => {
let reposList = ``;
if (repos.length > 0) {
repos.forEach((repo) => {
reposList += `<li class="mb-3 d-flex flex-content-stretch col-12 col-md-6 col-lg-6">
<div class="card" style="width: 22.5rem;">
<div class="card-body">
<h5 class="card-title"><a target="_blank" href="${repo.html_url}">${
repo.name
}</a></h5>
<p class="card-text">${
repo.description !== null ? repo.description : ""
}</p>
<p>`;
if (repo.language !== null) {
reposList += `<i class="fas fa-circle ${
repo.language ? repo.language.toLowerCase() : ""
}"></i> ${repo.language}`;
}
reposList += `<i class="far fa-star"></i> ${repo.stargazers_count}</p>
</div>
</div>
</li>`;
});
}
document.querySelector("#repos").innerHTML = reposList;
};
document.addEventListener("DOMContentLoaded", () => {
const searchForm = document.querySelector("#searchForm");
searchForm.addEventListener("submit", async (e) => {
e.preventDefault();
const searchInput = document.querySelector("#searchInput");
const gitHubLogin = searchInput.value.trim();
if (gitHubLogin.length > 0) {
const userProfile = await getGitHubProfile(gitHubLogin);
if (userProfile.login) {
const gitRepos = await getGitRepos(gitHubLogin);
renderProfile(userProfile);
listRepos(gitRepos);
document.querySelector(".searchblock").style.display = "none";
document.querySelector(".profile").style.display = "block";
}
}
});
});