|
| 1 | +// https://cors-anywhere.herokuapp.com/corsdemo request temperory access here for the code to work |
| 2 | + |
| 3 | +document.addEventListener("DOMContentLoaded", function () { |
| 4 | + const searchButton = document.getElementById("search-btn"); |
| 5 | + const usernameInput = document.getElementById("user-input"); |
| 6 | + const statsContainer = document.querySelector(".stats-container"); |
| 7 | + const easyProgressCircle = document.querySelector(".easy-progress"); |
| 8 | + const mediumProgressCircle = document.querySelector(".medium-progress"); |
| 9 | + const hardProgressCircle = document.querySelector(".hard-progress"); |
| 10 | + const easyLabel = document.getElementById("easy-label"); |
| 11 | + const mediumLabel = document.getElementById("medium-label"); |
| 12 | + const hardLabel = document.getElementById("hard-label"); |
| 13 | + const cardStatsContainer = document.querySelector(".stats-cards"); |
| 14 | + |
| 15 | + //return true or false based on a regex |
| 16 | + function validateUsername(username) { |
| 17 | + if (username.trim() === "") { |
| 18 | + alert("Username should not be empty"); |
| 19 | + return false; |
| 20 | + } |
| 21 | + const regex = /^[a-zA-Z0-9_-]{1,15}$/; |
| 22 | + const isMatching = regex.test(username); |
| 23 | + if (!isMatching) { |
| 24 | + alert("Invalid Username"); |
| 25 | + } |
| 26 | + return isMatching; |
| 27 | + } |
| 28 | + |
| 29 | + async function fetchUserDetails(username) { |
| 30 | + try { |
| 31 | + searchButton.textContent = "Searching..."; |
| 32 | + searchButton.disabled = true; |
| 33 | + //statsContainer.classList.add("hidden"); |
| 34 | + |
| 35 | + // const response = await fetch(url); |
| 36 | + const proxyUrl = "https://cors-anywhere.herokuapp.com/"; |
| 37 | + const targetUrl = "https://leetcode.com/graphql/"; |
| 38 | + |
| 39 | + const myHeaders = new Headers(); |
| 40 | + myHeaders.append("content-type", "application/json"); |
| 41 | + |
| 42 | + const graphql = JSON.stringify({ |
| 43 | + query: |
| 44 | + "\n query userSessionProgress($username: String!) {\n allQuestionsCount {\n difficulty\n count\n }\n matchedUser(username: $username) {\n submitStats {\n acSubmissionNum {\n difficulty\n count\n submissions\n }\n totalSubmissionNum {\n difficulty\n count\n submissions\n }\n }\n }\n}\n ", |
| 45 | + variables: { username: `${username}` }, |
| 46 | + }); |
| 47 | + const requestOptions = { |
| 48 | + method: "POST", |
| 49 | + headers: myHeaders, |
| 50 | + body: graphql, |
| 51 | + }; |
| 52 | + |
| 53 | + const response = await fetch(proxyUrl + targetUrl, requestOptions); |
| 54 | + if (!response.ok) { |
| 55 | + throw new Error("Unable to fetch the User details"); |
| 56 | + } |
| 57 | + const parsedData = await response.json(); |
| 58 | + console.log("Logging data: ", parsedData); |
| 59 | + |
| 60 | + displayUserData(parsedData); |
| 61 | + } catch (error) { |
| 62 | + statsContainer.innerHTML = `<p>${error.message}</p>`; |
| 63 | + } finally { |
| 64 | + searchButton.textContent = "Search"; |
| 65 | + searchButton.disabled = false; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + function updateProgress(solved, total, label, circle) { |
| 70 | + const progressDegree = (solved / total) * 100; |
| 71 | + circle.style.setProperty("--progress-degree", `${progressDegree}%`); |
| 72 | + label.textContent = `${solved}/${total}`; |
| 73 | + } |
| 74 | + |
| 75 | + function displayUserData(parsedData) { |
| 76 | + const totalQues = parsedData.data.allQuestionsCount[0].count; |
| 77 | + const totalEasyQues = parsedData.data.allQuestionsCount[1].count; |
| 78 | + const totalMediumQues = parsedData.data.allQuestionsCount[2].count; |
| 79 | + const totalHardQues = parsedData.data.allQuestionsCount[3].count; |
| 80 | + |
| 81 | + const solvedTotalQues = |
| 82 | + parsedData.data.matchedUser.submitStats.acSubmissionNum[0].count; |
| 83 | + const solvedTotalEasyQues = |
| 84 | + parsedData.data.matchedUser.submitStats.acSubmissionNum[1].count; |
| 85 | + const solvedTotalMediumQues = |
| 86 | + parsedData.data.matchedUser.submitStats.acSubmissionNum[2].count; |
| 87 | + const solvedTotalHardQues = |
| 88 | + parsedData.data.matchedUser.submitStats.acSubmissionNum[3].count; |
| 89 | + |
| 90 | + updateProgress( |
| 91 | + solvedTotalEasyQues, |
| 92 | + totalEasyQues, |
| 93 | + easyLabel, |
| 94 | + easyProgressCircle |
| 95 | + ); |
| 96 | + updateProgress( |
| 97 | + solvedTotalMediumQues, |
| 98 | + totalMediumQues, |
| 99 | + mediumLabel, |
| 100 | + mediumProgressCircle |
| 101 | + ); |
| 102 | + updateProgress( |
| 103 | + solvedTotalHardQues, |
| 104 | + totalHardQues, |
| 105 | + hardLabel, |
| 106 | + hardProgressCircle |
| 107 | + ); |
| 108 | + |
| 109 | + const cardsData = [ |
| 110 | + { |
| 111 | + label: "Overall Submissions", |
| 112 | + value: |
| 113 | + parsedData.data.matchedUser.submitStats.totalSubmissionNum[0] |
| 114 | + .submissions, |
| 115 | + }, |
| 116 | + { |
| 117 | + label: "Overall Easy Submissions", |
| 118 | + value: |
| 119 | + parsedData.data.matchedUser.submitStats.totalSubmissionNum[1] |
| 120 | + .submissions, |
| 121 | + }, |
| 122 | + { |
| 123 | + label: "Overall Medium Submissions", |
| 124 | + value: |
| 125 | + parsedData.data.matchedUser.submitStats.totalSubmissionNum[2] |
| 126 | + .submissions, |
| 127 | + }, |
| 128 | + { |
| 129 | + label: "Overall Hard Submissions", |
| 130 | + value: |
| 131 | + parsedData.data.matchedUser.submitStats.totalSubmissionNum[3] |
| 132 | + .submissions, |
| 133 | + }, |
| 134 | + ]; |
| 135 | + |
| 136 | + console.log("card ka data: ", cardsData); |
| 137 | + |
| 138 | + cardStatsContainer.innerHTML = cardsData |
| 139 | + .map( |
| 140 | + (data) => |
| 141 | + `<div class="card"> |
| 142 | + <h4>${data.label}</h4> |
| 143 | + <p>${data.value}</p> |
| 144 | + </div>` |
| 145 | + ) |
| 146 | + .join(""); |
| 147 | + } |
| 148 | + |
| 149 | + searchButton.addEventListener("click", function () { |
| 150 | + const username = usernameInput.value; |
| 151 | + console.log("logggin username: ", username); |
| 152 | + if (validateUsername(username)) { |
| 153 | + fetchUserDetails(username); |
| 154 | + } |
| 155 | + }); |
| 156 | +}); |
0 commit comments