-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
71 lines (63 loc) · 2.13 KB
/
script.js
File metadata and controls
71 lines (63 loc) · 2.13 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
const userimage = document.getElementById("imageId");
const follower = document.getElementById("followerId");
const following = document.getElementById("followingId");
const userName = document.getElementById("usernameId");
const input = document.getElementById("inputId");
const userLocation = document.getElementById("locationId");
const names = document.getElementById("nameId");
const bio = document.getElementById("bioId");
const caption = document.getElementById("caption");
const repos = document.getElementById("reposId");
const main = document.querySelector(".main");
const errorId = document.getElementById("errorId");
function displayUI(data) {
if (data.name) {
console.log(data);
userimage.src = data.avatar_url;
caption.innerHTML = `<a href="${data.html_url}" target="_blank">Go to profile</a>`;
names.innerHTML = data.name;
bio.innerHTML = data.bio ? data.bio : "Not available";
userName.innerHTML = data.login;
repos.innerHTML = data.public_repos;
userLocation.innerHTML = data.location;
follower.innerHTML = data.followers;
following.innerHTML = data.following;
} else {
main.classList.add("invisible");
errorId.innerText = "No user found, enter valid username";
setTimeout(() => {
init();
errorId.innerText = "";
}, 2000);
}
}
// ${event.target.value}
function handleChange() {
main.classList.remove("invisible");
if (event.keyCode === 13) {
let xrh = new XMLHttpRequest();
xrh.open("GET", `https://api.github.com/users/${event.target.value}`);
xrh.onload = function () {
let userData = JSON.parse(xrh.response);
displayUI(userData);
};
xrh.onerror = function () {
console.log("Something happened....");
};
xrh.send();
event.target.value = "";
}
}
input.addEventListener("keyup", handleChange);
async function init() {
main.classList.remove("invisible");
try {
const response = await fetch("https://api.github.com/users/torvalds");
const data = await response.json();
return displayUI(data);
} catch (error) {
const err = new Error("Fetch error: " + error);
console.log(err);
}
}
init();