-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
99 lines (83 loc) · 3.3 KB
/
Copy pathscript.js
File metadata and controls
99 lines (83 loc) · 3.3 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
87
88
89
90
91
92
93
94
95
96
97
98
99
const bodyEl = document.querySelector('body');
const toggleThemeBtn = document.getElementById('toggle-theme-btn');
const formEl = document.querySelector('form');
const inputEl = document.getElementById('input');
const resultEl = document.querySelector('.result');
const notFoundEl = document.querySelector('.not-found');
const profileImg = document.getElementById('profile-img');
const fullName = document.getElementById('name');
const username = document.getElementById('username');
const joined = document.getElementById('joined');
const bio = document.getElementById('bio');
const reposCount = document.getElementById('repos-count');
const followersCount = document.getElementById('followers-count');
const followingCount = document.getElementById('following-count');
const locationEl = document.getElementById('location');
const twitterEl = document.getElementById('twitter');
const websiteEl = document.getElementById('website');
const companyEl = document.getElementById('company');
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
let inDarkMode = true;
formEl.addEventListener('submit', (e) => {
e.preventDefault();
let input = inputEl.value;
getUserInfo(input);
formEl.reset();
})
toggleThemeBtn.onclick = () => {
if (inDarkMode) {
bodyEl.classList.replace('dark', 'light');
inDarkMode = false;
}
else {
bodyEl.classList.replace('light', 'dark');
inDarkMode = true;
}
}
async function getUserInfo(input) {
const res = await fetch(`https://api.github.com/users/${input}`);
const data = await res.json();
renderData(data);
}
function renderData(data) {
if (data.message) {
resultEl.style.display = 'none';
notFoundEl.style.display = 'flex';
}
else {
notFoundEl.style.display = 'none';
resultEl.style.display = 'flex';
const date = new Date(data['created_at']);
profileImg.src = data['avatar_url'];
fullName.textContent = data['name'];
username.href = data['html_url'];
username.textContent = `@${data['login']}`;
joined.textContent = `Joined ${date.getDate()} ${months[date.getMonth()]} ${date.getFullYear()}`;
bio.textContent = data['bio'] === null ? "This dev doesn't have a bio" : data['bio'];
reposCount.textContent = data['public_repos'];
followersCount.textContent = data['followers'];
followingCount.textContent = data['following'];
renderInfoOf(locationEl, data['location']);
renderInfoOf(twitterEl, data['twitter_username'], true);
renderInfoOf(websiteEl, data['blog'], true);
renderInfoOf(companyEl, data['company']);
}
}
function renderInfoOf(element, value, hyperlink = false) {
urls = ["https://twitter.com/", ""]
if (value === null || value == '') {
element.classList.add('disabled');
if (hyperlink) {
element.href = '';
}
element.textContent = 'Not Available';
}
else {
element.classList.remove('disabled');
if (hyperlink) {
element.href = urls[element.dataset.urlindex] + value;
}
element.textContent = value;
}
}
getUserInfo('himanshuat');