-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
140 lines (122 loc) · 5.1 KB
/
script.js
File metadata and controls
140 lines (122 loc) · 5.1 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
document.addEventListener("DOMContentLoaded", function () {
const usernameInput = document.getElementById("user-input");
const searchButton = document.getElementById("search-btn");
const statsContainer = document.querySelector(".stats-container");
const easyProgressCircle = document.querySelector(".easy-progress");
const mediumProgressCircle = document.querySelector(".medium-progress");
const hardProgressCircle = document.querySelector(".hard-progress");
const errorMessage = document.getElementById("error-message");
const easyLabel = document.getElementById("easy-label");
const mediumLabel = document.getElementById("medium-label");
const hardLabel = document.getElementById("hard-label");
const cardStatsContainer = document.querySelector(".stats-cards");
//return true/false based on regex(by genAI)
function validateUsername(username) {
if (username.trim() === "") {
alert("Username should not be empty.");
return false;
}
const regex = /^[A-Za-z0-9_-]{3,20}$/;
const isMatching = regex.test(username);
if (!isMatching) {
alert("Invalid Username!");
}
return isMatching;
}
async function fetchUserDetails(username) {
const url = `https://leetcode-stats-api.herokuapp.com/${username}`;
try {
searchButton.textContent = "Searching...";
searchButton.disabled = true;
// Show container but hide content while loading
statsContainer.classList.remove("hidden");
document.querySelector(".progress").classList.add("hidden");
document.querySelector(".stats-cards").classList.add("hidden");
errorMessage.classList.add("hidden");
const response = await fetch(url); //get request - async operation
if (!response.ok) {
//response status not ok
throw new Error("Unable to fetch the user details!");
}
const data = await response.json(); //parse json --> async
console.log("Logging data: ", data);
// Check for API-specific error
if (data.status === "error") {
throw new Error(data.message || "User not found");
}
displayUserData(data);
} catch (error) {
// Show error state
statsContainer.classList.remove("hidden");
document.querySelector(".progress").classList.add("hidden");
document.querySelector(".stats-cards").classList.add("hidden");
errorMessage.classList.remove("hidden");
errorMessage.textContent = error.message;
} finally {
searchButton.textContent = "Search";
searchButton.disabled = false;
}
}
function updateProgress(solved, total, label, circle) {
const progressDegree = (solved / total) * 100;
circle.style.setProperty("--progress-degree", `${progressDegree}%`);
label.textContent = `${solved}/${total}`;
}
function displayUserData(data) {
// Show all elements
statsContainer.classList.remove("hidden");
document.querySelector(".progress").classList.remove("hidden");
document.querySelector(".stats-cards").classList.remove("hidden");
errorMessage.classList.add("hidden");
const totalQues = data.totalQuestions;
const totalEasyQues = data.totalEasy;
const totalMediumQues = data.totalMedium;
const totalHardQues = data.totalHard;
const solvedTotalQues = data.totalSolved;
const solvedTotalEasyQues = data.easySolved;
const solvedTotalMediumQues = data.mediumSolved;
const solvedTotalHardQues = data.hardSolved;
updateProgress(
solvedTotalEasyQues,
totalEasyQues,
easyLabel,
easyProgressCircle
);
updateProgress(
solvedTotalMediumQues,
totalMediumQues,
mediumLabel,
mediumProgressCircle
);
updateProgress(
solvedTotalHardQues,
totalHardQues,
hardLabel,
hardProgressCircle
);
const cardsData = [
{ label: "Acceptance Rate", value: data.acceptanceRate },
{ label: "Questions solved ranking", value: data.ranking },
{ label: "contribution Points", value: data.contributionPoints },
{ label: "reputation", value: data.reputation },
];
console.log("card data:", cardsData);
cardStatsContainer.innerHTML = cardsData
.map(
(data) =>
`<div class="card">
<h4>${data.label}</h4>
<p>${data.value}</p>
</div>`
)
.join("");
}
searchButton.addEventListener("click", function () {
const username = usernameInput.value;
console.log("loggin username: ", username);
if (validateUsername(username)) {
//fetch data via API call
fetchUserDetails(username);
}
});
});