-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
72 lines (65 loc) · 2.4 KB
/
Copy pathscripts.js
File metadata and controls
72 lines (65 loc) · 2.4 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
const form = document.querySelector('.form');
form.addEventListener('submit', handleSubmit);
const searchResults = document.querySelector('.search-results');
async function handleSubmit(e) {
e.preventDefault();
try {
let userName = document.querySelector('.data').value;
const results = await GithubApi(userName);
if (userName != results.login) {
alert('Enter a valid input!');
} else {
displayResults(results);
}
document.querySelector('.data').value = '';
} catch (err) {
console.log(err);
alert('Invalid input');
location.reload();
}
}
async function GithubApi(userName) {
const url = `https://api.github.com/users/${userName}`;
const response = await fetch(url);
if (!response.ok) {
throw Error(response.statusText);
}
const jsonResponse = await response.json();
return jsonResponse;
}
async function displayResults(results) {
searchResults.innerHTML += `
<div class="row mb-2">
<div class="col-md-4 ">
<img class="profil" src="${results.avatar_url}" alt="">
</div>
<div class="col-md-4 p-1">
<li class ="item">Name: ${results.login}</li>
<li class ="item">GitHub: <a href="${results.html_url}" class="btn btn-primary">link</a></li>
<li class ="item">Repos:<span class="btn btn-success">${results.public_repos}</span></li>
</div>
<div class="col-md-4 text-center">
<div class="btn-group">
<button type="button" class="btn btn btn-warning dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Get repos
</button>
<div class="dropdown-menu mt-2">
${await getRepos(results.login)}
</div>
</div>
</div>
</div>
<hr>
`;
}
async function getRepos(userName) {
const url = `https://api.github.com/users/${userName}/repos`;
const repos = await fetch(url);
const reposResponse = await repos.json();
let links = '';
reposResponse.forEach(reposway => {
links += ` <a class="dropdown-item" href="${reposway.html_url}" target="_blank">${reposway.name}</a><div class="dropdown-divider"></div>`
});
return links;
}
//NarcisseObadiah