-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
146 lines (119 loc) · 4.95 KB
/
Copy pathscript.js
File metadata and controls
146 lines (119 loc) · 4.95 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
document.addEventListener("DOMContentLoaded", function () {
const searchButton = document.getElementById("search-button");
const usernameInput = document.getElementById("user-input");
const loader = document.getElementById("loader");
const statsContainer = document.getElementById("stats-container");
const easyProgressCircle = document.querySelector(".easy-progress");
const mediumProgressCircle = document.querySelector(".medium-progress");
const hardProgressCircle = document.querySelector(".hard-progress");
const easyLabel = document.getElementById("easy-label");
const mediumLabel = document.getElementById("medium-label");
const hardLabel = document.getElementById("hard-label");
const cardStatsContainer = document.getElementById("stats-cards");
const themeToggle = document.getElementById("theme-toggle");
// Theme management
function initTheme() {
const savedTheme = localStorage.getItem('theme') || 'dark';
document.documentElement.setAttribute('data-theme', savedTheme);
updateThemeIcon(savedTheme);
}
function toggleTheme() {
const currentTheme = document.documentElement.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
document.documentElement.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme);
updateThemeIcon(newTheme);
}
function updateThemeIcon(theme) {
const icon = themeToggle.querySelector('i');
if (theme === 'dark') {
icon.className = 'fas fa-moon';
} else {
icon.className = 'fas fa-sun';
}
}
themeToggle.addEventListener('click', toggleTheme);
function validateUsername(username) {
if (username.trim() === "") {
showError("Username should not be empty");
return false;
}
const regex = /^[a-zA-Z0-9_-]{1,15}$/;
const isMatching = regex.test(username);
if (!isMatching) {
showError("Invalid username format");
}
return isMatching;
}
function showError(message) {
cardStatsContainer.innerHTML = `<p class="error">${message}</p>`;
}
function updateProgress(solved, total, label, circle) {
const progress = total === 0 ? 0 : (solved / total) * 100;
circle.style.setProperty("--progress-degree", `${progress}%`);
label.textContent = `${solved}/${total}`;
}
function clearPreviousData() {
easyLabel.textContent = "";
mediumLabel.textContent = "";
hardLabel.textContent = "";
easyProgressCircle.style.setProperty("--progress-degree", `0%`);
mediumProgressCircle.style.setProperty("--progress-degree", `0%`);
hardProgressCircle.style.setProperty("--progress-degree", `0%`);
cardStatsContainer.innerHTML = "";
}
async function fetchUserDetails(username) {
const url = `https://leetcode-api-faisalshohag.vercel.app/${username}`;
try {
loader.classList.remove("hidden");
searchButton.disabled = true;
searchButton.textContent = "Searching...";
clearPreviousData();
const response = await fetch(url);
const data = await response.json();
if (!data || data.totalSolved === undefined) {
throw new Error("User not found.");
}
displayUserData(data);
} catch (error) {
clearPreviousData();
showError("API error or network issue. Try again.");
} finally {
loader.classList.add("hidden");
searchButton.disabled = false;
searchButton.textContent = "Search";
}
}
function displayUserData(data) {
updateProgress(data.easySolved, data.totalEasy, easyLabel, easyProgressCircle);
updateProgress(data.mediumSolved, data.totalMedium, mediumLabel, mediumProgressCircle);
updateProgress(data.hardSolved, data.totalHard, hardLabel, hardProgressCircle);
const cardData = [
{ label: "Total Solved", value: data.totalSolved },
{ label: "Easy Solved", value: data.easySolved },
{ label: "Medium Solved", value: data.mediumSolved },
{ label: "Hard Solved", value: data.hardSolved },
{ label: "Ranking", value: data.ranking },
{ label: "Contribution Points", value: data.contributionPoint },
];
cardStatsContainer.innerHTML = cardData.map(item => `
<div class="card">
<h4>${item.label}</h4>
<p>${item.value}</p>
</div>
`).join("");
}
searchButton.addEventListener("click", function () {
const username = usernameInput.value.trim();
if (validateUsername(username)) {
fetchUserDetails(username);
}
});
usernameInput.addEventListener("keypress", function (e) {
if (e.key === "Enter") {
searchButton.click();
}
});
// Initialize theme
initTheme();
});