-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
86 lines (74 loc) · 2.38 KB
/
index.js
File metadata and controls
86 lines (74 loc) · 2.38 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
let userAvatar = document.querySelector('.user');
let userURL = document.querySelector('.user-url')
let githubName = document.querySelector('h2');
let username = document.querySelector('a');
let followers = document.querySelector('.followers');
let following = document.querySelector('.following');
let input = document.querySelector('input');
let catButton = document.querySelector('button');
let catImg = document.querySelector('.cat-img');
function fetch(url, successHandler) {
let xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = () => successHandler(JSON.parse(xhr.response));
xhr.onerror = function() {
console.error('Something went wrong 🙁');
};
xhr.send();
}
// GitHub User Finder
function displayFollowers(username) {
followers.innerHTML = "";
fetch(`https://api.github.com/users/${username}/followers`, function(followersList){
let topFive = followersList.slice(0,5);
topFive.forEach(info => {
let li = document.createElement('li');
let img = document.createElement('img');
img.src = info.avatar_url;
img.href = info.html_url;
li.append(img);
followers.append(li);
})
});
}
function displayFollowing(username) {
following.innerHTML = "";
fetch(`https://api.github.com/users/${username}/following`, function(followersList){
let topFive = followersList.slice(0,5);
topFive.forEach(info => {
let li = document.createElement('li');
let img = document.createElement('img');
img.src = info.avatar_url;
img.href = info.html_url;
li.append(img);
following.append(li);
})
});
}
function createUI(data) {
userAvatar.src = data.avatar_url;
githubName.innerText = data.name;
username.innerText = `@${data.login}`;
userURL.href = data.html_url;
displayFollowers(data.login);
displayFollowing(data.login);
}
function handleEnter(event) {
if(event.keyCode === 13 && input.value) {
let user = event.target.value;
const url = `https://api.github.com/users/${user}`;
fetch(url, createUI);
input.value = "";
}
}
input.addEventListener('keyup', handleEnter);
// Get New Cat
catButton.addEventListener('click', () => {
let cat = new XMLHttpRequest();
cat.open('GET', 'https://api.thecatapi.com/v1/images/search?limit=1&size=full');
cat.onload = function() {
let catData = JSON.parse(cat.response);
catImg.src = catData[0].url;
}
cat.send();
});