-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
280 lines (258 loc) · 9.58 KB
/
script.js
File metadata and controls
280 lines (258 loc) · 9.58 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// NOTE: Unauthorized users (user is determined by IP)
// can make up to 60 requests per hour, give or take.
// Keep this in mind when fetching data from the GH API.
// Credit to @adamstafa's answer in this GH issue:
// https://github.com/Cloud-CV/EvalAI/issues/1373
// To be rendered selectors.
const devPic = document.querySelector(".main__result--pfp");
const devName = document.querySelector(".main__result--name");
const devUsername = document.querySelector(".main__result--username");
const devJoinDate = document.querySelector(".main__result--date");
const devBio = document.querySelector(".main__result--bio");
const devRepos = document.getElementById("main__result--stats-repos");
const devFollowers = document.getElementById("main__result--stats-followers");
const devFollowing = document.getElementById("main__result--stats-following");
const devLocation = document.querySelector(".main__result--data-location>span");
const devWebsite = document.querySelector(".main__result--data-website>a");
const devTwitter = document.querySelector(".main__result--data-twitter>a");
const devCompany = document.querySelector(".main__result--data-company>a");
// To listen and receive data selectors.
const searchInput = document.getElementById("main__search--input");
const searchSubmitButton = document.getElementById("main__search--submit");
const errorMessage = document.getElementById("main__search--error");
// Light / Dark mode buttons and variables.
const toggleButtons = document.querySelectorAll(".header__theme");
const darkModeButton = toggleButtons[0];
const lightModeButton = toggleButtons[1];
const rootElement = document.querySelector(":root");
const headerElement = document.querySelector(".header__theme");
const mainResult = document.querySelector(".main__result");
// Boolean if already set in the browser, otherwise null.
const localPreference = JSON.parse(localStorage.getItem("dark-theme"));
// Boolean, check OS dark mode.
const defaultDarkMode = window.matchMedia(
"(prefers-color-scheme: dark)"
).matches;
// The nullish coalescing operator (??) is a logical
// operator that returns its right-hand side operand
// when its left-hand side operand is null or undefined,
// and otherwise returns its left-hand side operand.
// boolean; if this evaluates to true, darkMode should be turned on.
let isDarkMode = localPreference ?? defaultDarkMode;
function handleDarkmode(isDark) {
if (isDark) {
rootElement.classList.add("dark");
headerElement.classList.add("dark");
searchInput.classList.add("dark");
mainResult.classList.add("dark");
darkModeButton.classList.add("hidden");
lightModeButton.classList.remove("hidden");
localStorage.setItem("dark-theme", true);
} else {
rootElement.classList.remove("dark");
headerElement.classList.remove("dark");
searchInput.classList.remove("dark");
mainResult.classList.remove("dark");
lightModeButton.classList.add("hidden");
darkModeButton.classList.remove("hidden");
localStorage.setItem("dark-theme", false);
}
}
// Dark mode toggle.
darkModeButton.addEventListener("click", () => {
handleDarkmode(true);
});
// Light mode toggle.
lightModeButton.addEventListener("click", () => {
handleDarkmode(false);
});
// If a GitHub user hasn't added their name,
// show their username where the name would be
// without the '@' symbol.
function renderName(userDataObj) {
if (userDataObj.name === null) {
devName.textContent = userDataObj.login;
} else {
devName.textContent = userDataObj.name;
}
}
// If a GitHub user bio is empty, show the text
// "This profile has no bio" with transparency added.
function renderBio(userDataObj) {
if (userDataObj.bio === null) {
devBio.style.opacity = 0.75;
devBio.textContent = "This profile has no bio";
} else {
devBio.style.opacity = 1;
devBio.textContent = userDataObj.bio;
}
}
// If location is empty, show the text
// "Not Available" with transparency added.
function renderLocation(userDataObj) {
if (userDataObj.location === null) {
devLocation.style.opacity = 0.5;
devLocation.textContent = "Not Available";
} else {
devLocation.style.opacity = 1;
devLocation.textContent = userDataObj.location.trim();
}
}
// If website is empty, show the text
// "Not Available" with transparency added.
function renderWebsite(userDataObj) {
if (userDataObj.blog === "") {
devWebsite.style.opacity = 0.5;
devWebsite.href = "javascript:void(0)";
devWebsite.textContent = "Not Available";
} else {
// With a long website string like
// https://www.freecodecamp.org, the card struggles
// because of narrow card width and "big" font size.
// So, string longer than 19 chars, we trim
// the link render and add an ellipsis (...) at the end.
if (userDataObj.blog.trim().length > 19) {
devWebsite.textContent = userDataObj.blog.trim().slice(0, 16) + "...";
} else {
devWebsite.textContent = userDataObj.blog.trim();
}
devWebsite.style.opacity = 1;
devWebsite.href = userDataObj.blog.trim();
// Add 'https' redirect if the original URL didn't have it.
if (devWebsite.protocol !== "https:") {
devWebsite.href = `https://${userDataObj.blog.trim()}`;
}
}
}
// If twitter is empty, show the text
// "Not Available" with transparency added.
function renderTwitter(userDataObj) {
if (userDataObj.twitter_username === null) {
devTwitter.style.opacity = 0.5;
devTwitter.href = "javascript:void(0)";
devTwitter.textContent = "Not Available";
} else {
devTwitter.style.opacity = 1;
devTwitter.href = `https://twitter.com/${userDataObj.twitter_username.trim()}`;
devTwitter.textContent = `@${userDataObj.twitter_username.trim()}`;
}
}
// If company is empty, show the text
// "Not Available" with transparency added.
function renderCompany(userDataObj) {
if (userDataObj.company === null) {
devCompany.style.opacity = 0.5;
devCompany.href = "javascript:void(0)";
devCompany.textContent = "Not Available";
} else {
// If userDataObj.company starts with an "@" character,
// surely the GitHub company site exists.
// Link the corresponding URL.
if (userDataObj.company[0] === "@") {
devCompany.href = `https://github.com/${userDataObj.company
.trim()
.toLowerCase()
.substring(1)}`;
} else {
// Otherwise, we ignore if it exists or not.
// Rather not redirect to an non-existent GitHub site (404).
devCompany.href = "javascript:void(0)";
}
devCompany.style.opacity = 1;
devCompany.textContent = userDataObj.company.trim();
}
}
// Render user's data in the result card.
function renderUserData(userDataObj) {
renderName(userDataObj);
devUsername.textContent = `@${userDataObj.login}`;
devJoinDate.textContent = new Intl.DateTimeFormat("en-GB", {
year: "numeric",
month: "short",
day: "2-digit",
}).format(new Date(userDataObj.created_at));
renderBio(userDataObj);
devRepos.textContent = userDataObj.public_repos;
devFollowers.textContent = userDataObj.followers;
devFollowing.textContent = userDataObj.following;
renderLocation(userDataObj);
renderWebsite(userDataObj);
renderTwitter(userDataObj);
renderCompany(userDataObj);
devPic.src = userDataObj.avatar_url;
}
// Filter the user search query to get
// a githubUser that can be searched.
function filterQuery(userQuery) {
let githubUser = "";
const trimmedQuery = userQuery.trim();
if (trimmedQuery[0] === "@") {
githubUser = trimmedQuery.toLowerCase().substring(1);
} else {
githubUser = trimmedQuery.trim().toLowerCase();
}
return githubUser;
}
// Display error message if user is not found.
function displayErrorMessage() {
// Display 'No results' messages in search box.
errorMessage.classList.remove("hidden");
// 'Hide' (increase search box padding-left)
// temporarily the search query if it's too long.
searchInput.classList.add("error");
}
// Clear error message if there's any.
function clearErrorMessage() {
// Hide 'No results' messages in search box.
errorMessage.classList.add("hidden");
// Display the search query again.
searchInput.classList.remove("error");
}
// Fetch a user from the GitHub API.
async function fetchUser(userQuery) {
try {
const githubUser = filterQuery(userQuery);
// The GitHub API may require the headers option
// for a successful request.
// Right now it doesn't need, but just in case.
// Credit:
// https://stackoverflow.com/questions/39907742/github-api-is-responding-with-a-403-when-using-requests-request-function
const response = await fetch(`https://api.github.com/users/${githubUser}`, {
headers: {
"User-Agent": "request",
},
});
const userDataObj = await response.json();
const userStatus = response.status;
if (userStatus !== 404) {
clearErrorMessage();
renderUserData(userDataObj);
} else {
displayErrorMessage();
}
} catch (error) {
console.error(error);
}
}
// Check if any key is pressed (including backspace)
// to clear error message.
searchInput.addEventListener("keydown", () => {
clearErrorMessage();
});
// Look up for a user and render info after
// typing your search query when pressing the
// "enter" button on your keyboard.
searchInput.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
fetchUser(searchInput.value);
}
});
// Use search query after pressing the search submit
// button to look up for a user and render info.
searchSubmitButton.addEventListener("click", () =>
fetchUser(searchInput.value)
);
// On first load, detect OS's light/dark preference.
handleDarkmode(isDarkMode);
// On load, fetch @octocat GitHub user.
fetchUser("octocat");