-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
62 lines (60 loc) · 1.65 KB
/
app.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
//Init Github
const github = new Github();
//Init UI
const ui = new UI();
//Search input
const searchUser = document.getElementById("searchUser");
//Pagination
let page = 1;
//to stop fetch req if no more pages available
let repo_count = 0;
//fetch profile and repo on every keystroke
searchUser.addEventListener("keyup", (e) => {
const userText = e.target.value;
ui.clearAlert();
if (userText !== "") {
//Fetch Profile
ui.showSpinner();
github.getUser(userText).then((data) => {
//remove spinner once get the response
ui.removeSpinner();
if (data.profile.message === "Not Found") {
//show alert
ui.showAlert("User not found", "alert alert-danger");
} else {
//show profile
page = 1;
repo_count = 0;
ui.showProfile(data.profile);
repo_count = data.profile.public_repos;
github
.getRepos(userText, page)
.then((data) => ui.showRepos(data.repos, true));
}
});
} else {
//clear profile and repo
ui.clearProfile();
//remove spinner
ui.removeSpinner();
//reseting repo variables
page = 1;
lastData = 0;
repoLimit = false;
}
});
//Pagination on scroll
window.addEventListener("scroll", () => {
const { scrollTop, scrollHeight, clientHeight } = document.documentElement;
if (page * 5 >= repo_count) {
return;
}
if (scrollTop + clientHeight >= scrollHeight - 5) {
ui.showLoader();
page++;
github.getRepos(searchUser.value, page).then((data) => {
ui.removeLoader();
setTimeout(() => ui.showRepos(data.repos), 200);
});
}
});