-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
124 lines (106 loc) · 3.19 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
const API_LINK = 'https://api.github.com/users';
let search_btn = document.querySelector('.search');
let search_term = document.querySelector('#search-term');
let searching = document.querySelector('.searching');
let repoUl = document.querySelector('#repo');
var repo = new Array(0);
search_term.focus();
search_btn.addEventListener("click", (e)=>{
e.preventDefault();
if(search_term.value){
searching.innerHTML= "Searching...";
setTimeout(() => {
getUserDetails(`${API_LINK}/${search_term.value}`)
}, 2000);
setTimeout(() => {
getRepoDetails(`${API_LINK}/${search_term.value}/repos`)
}, 1000);
}else{
alert("Please enter any github username")
search_term.focus();
}
})
//Function to show data on browser screen
function showUserDetails(data) {
let box = document.querySelector('.box-body');
searching.innerHTML= "";
var j = ``;
repo.forEach((r)=> {
r.forEach((res)=> {
j+= `<a href="${res.html_url}" target="_blank"><li>${res.name}</li></a>`;
})
})
box.innerHTML=(`
<div class="profile-box">
<div class="row">
<div class="image">
<img src="${data.avatar_url}" alt="">
</div>
<div class="about">
<div class="details">
<h1 class="name">${data.name}</h1>
<h3 class="username">@${data.login}</h3>
<p class="country"><span><ion-icon name="pin"></ion-icon></span>${data.location===null ? 'Unknown': data.location}</p>
</div>
<div class="btn-profile">
<a href="${data.html_url}" target="_blank">Visit Profile</a>
</div>
</div>
</div>
<div class="bio">
<h3>About</h3>
<p>${data.bio===null ? 'Bio description is not available' : data.bio}</p>
</div>
<div class="row-followers">
<div class="col">
<h3 class="heading">Followers</h3>
<p>${data.followers}</p>
</div>
<div class="col">
<h3 class="heading">Following</h3>
<p>${data.following}</p>
</div>
<div class="col">
<h3 class="heading">Repos</h3>
<p>${data.public_repos}</p>
</div>
</div>
<h3 class="repo-heading">Repositories</h3>
<div class="repos-row">
<ul id="repo">
${j}
</ul>
</div>
</div>
`);
}
//Fetching user details with this function
async function getUserDetails(api){
let query = await fetch(api)
.then(async (query)=> {
return await query.json();
})
.then((result)=> {
if(result.name==null){
alert("User not found")
location.reload();
}else{
// console.log(result);
showUserDetails(result);
}
}).catch((error)=> {
console.log(error)
})
}
//Fetching user Repositiory with this function
async function getRepoDetails(repo_api){
let repo_query = await fetch(repo_api)
.then(async (repo_query)=> {
return await repo_query.json();
})
.then((repo_result)=> {
repo.push(repo_result);
}).catch((error)=> {
console.log(error);
})
}