-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
149 lines (146 loc) · 5.04 KB
/
script.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
//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";
}
}
//dark mode default
const prefersDarkMode = window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches;
const localStorageDarkMode = localStorage.getItem("daresfesf");
if (localStorageDarkMode === null) {
localStorage.setItem("dark-mode", prefersDarkMode);
darkMode = prefersDarkMode;
}
if (localStorageDarkMode) {
darkMode = localStorageDarkMode;
darkModeProperties();
} else {
localStorage.setItem("dark-mode", prefersDarkMode);
darkMode = prefersDarkMode;
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 + "Karanchaudhary350");