-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
50 lines (44 loc) · 1.32 KB
/
script.js
File metadata and controls
50 lines (44 loc) · 1.32 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
$("#search-button").click(() => {
displayContent().catch((e) => {
$("#exist").hide();
$("#notExist").show();
});
$('#search').val("");
});
const get = async (url) => {
let response = await fetch(url, {
method: "GET",
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
}
const displayContent = async () => {
let user = $("#search").val();
const urlUser = get(`https://api.github.com/users/${user}`);
const urlRepo = get(`https://api.github.com/users/${user}/repos`);
let urls = await Promise.all([urlUser, urlRepo]);
generateUserData(urls[0]);
generateReposData(urls[1]);
$("#notExist").hide();
$("#exist").show();
}
const generateUserData = (userData) => {
$("#avatar").attr("src", userData.avatar_url);
$("#userName").text('@' + userData.login);
$("#name").text(userData.name);
$("#bio").text(userData.bio);
}
const generateReposData = (dataArr) => {
$("#list").html("");
if (dataArr.length > 0) {
dataArr.forEach(data => {
let $li = $("<li></li>").appendTo("#list");
$li.html(`${data.name}<span><i class="fas fa-star"></i>${data.stargazers_count}<i class="fas fa-code-branch"></i>${data.forks}</span>`);
});
} else {
let $li = $("<li></li>").appendTo("#list");
$li.html("<span class='noRepo'>This user has no repositories</span>");
}
}