-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
184 lines (179 loc) · 5.6 KB
/
index.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
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
//Variables
const searchbar = document.querySelector(".searchbar-container");
const profilecontainer = document.querySelector(".profile-container");
const root = document.documentElement.style;
const get = (param) => document.getElementById(`${param}`);
const url = "https://api.github.com/users/";
const noresults = get("no-results");
const btnmode = get("btn-mode");
const modetext = get("mode-text");
const modeicon = get("mode-icon");
const btnsubmit = get("submit");
const input = get("input");
const avatar = get("avatar");
const userName = get("name");
const user = get("user");
const date = get("date");
const months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
const bio = get("bio");
const repos = get("repos");
const followers = get("followers");
const following = get("following");
const user_location = get("location");
const page = get("page");
const twitter = get("twitter");
const company = get("company");
let darkMode = false;
// Event Listeners
btnsubmit.addEventListener("click", function () {
if (input.value !== "") {
getUserData(url + input.value);
}
});
input.addEventListener(
"keydown",
function (e) {
if (!e) {
var e = window.event;
}
if (e.key == "Enter") {
if (input.value !== "") {
getUserData(url + input.value);
}
}
},
false
);
input.addEventListener("input", function () {
noresults.style.display = "none";
profilecontainer.classList.remove("active");
searchbar.classList.add("active");
});
btnmode.addEventListener("click", function () {
if (darkMode == false) {
darkModeProperties();
} else {
lightModeProperties();
}
});
// Functions
function getUserData(gitUrl) {
fetch(gitUrl)
.then((response) => response.json())
.then((data) => {
console.log(data);
updateProfile(data);
})
.catch((error) => {
throw error;
});
}
function updateProfile(data) {
if (data.message !== "Not Found") {
noresults.style.display = "none";
function checkNull(param1, param2) {
if (param1 === "" || param1 === null) {
param2.style.opacity = 0.5;
param2.previousElementSibling.style.opacity = 0.5;
return false;
} else {
return true;
}
}
avatar.src = `${data.avatar_url}`;
userName.innerText = data.name === null ? data.login : data.name;
user.innerText = `@${data.login}`;
user.href = `${data.html_url}`;
datesegments = data.created_at.split("T").shift().split("-");
date.innerText = `Joined ${datesegments[2]} ${
months[datesegments[1] - 1]
} ${datesegments[0]}`;
bio.innerText =
data.bio == null ? "This profile has no bio" : `${data.bio}`;
repos.innerText = `${data.public_repos}`;
followers.innerText = `${data.followers}`;
following.innerText = `${data.following}`;
user_location.innerText = checkNull(data.location, user_location)
? data.location
: "Not Available";
page.innerText = checkNull(data.blog, page) ? data.blog : "Not Available";
page.href = checkNull(data.blog, page) ? data.blog : "#";
twitter.innerText = checkNull(data.twitter_username, twitter)
? data.twitter_username
: "Not Available";
twitter.href = checkNull(data.twitter_username, twitter)
? `https://twitter.com/${data.twitter_username}`
: "#";
company.innerText = checkNull(data.company, company)
? data.company
: "Not Available";
searchbar.classList.toggle("active");
profilecontainer.classList.toggle("active");
} else {
noresults.style.display = "block";
}
}
// This code checks if the user's device has a preference for dark mode
const prefersDarkMode =
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").matches;
// Check if there is a value for "dark-mode" in the user's localStorage
if (localStorage.getItem("dark-mode") === null) {
// If there is no value for "dark-mode" in localStorage, check the device preference
if (prefersDarkMode) {
// If the device preference is for dark mode, apply dark mode properties
darkModeProperties();
} else {
// If the device preference is not for dark mode, apply light mode properties
lightModeProperties();
}
} else {
// If there is a value for "dark-mode" in localStorage, use that value instead of device preference
if (localStorage.getItem("dark-mode") === "true") {
// If the value is "true", apply dark mode properties
darkModeProperties();
} else {
// If the value is not "true", apply light mode properties
lightModeProperties();
}
}
function darkModeProperties() {
root.setProperty("--lm-bg", "#141D2F");
root.setProperty("--lm-bg-content", "#1E2A47");
root.setProperty("--lm-text", "white");
root.setProperty("--lm-text-alt", "white");
root.setProperty("--lm-shadow-xl", "rgba(70,88,109,0.15)");
modetext.innerText = "LIGHT";
modeicon.src = "./assets/images/sun-icon.svg";
root.setProperty("--lm-icon-bg", "brightness(1000%)");
darkMode = true;
localStorage.setItem("dark-mode", true);
}
function lightModeProperties() {
root.setProperty("--lm-bg", "#F6F8FF");
root.setProperty("--lm-bg-content", "#FEFEFE");
root.setProperty("--lm-text", "#4B6A9B");
root.setProperty("--lm-text-alt", "#2B3442");
root.setProperty("--lm-shadow-xl", "rgba(70, 88, 109, 0.25)");
modetext.innerText = "DARK";
modeicon.src = "./assets/images/moon-icon.svg";
root.setProperty("--lm-icon-bg", "brightness(100%)");
darkMode = false;
localStorage.setItem("dark-mode", false);
}
profilecontainer.classList.toggle("active");
searchbar.classList.toggle("active");
getUserData(url + "luxprajapati");