Skip to content

WM5 | Karam Ali | JS2 | Codewars -API - Project #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 70 additions & 41 deletions codewars-badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,79 @@
// Here is some information about web component https://developer.mozilla.org/en-US/docs/Web/Web_Components
// Here is the link to the Codewars API Docs: https://dev.codewars.com/#get-user

class CodeWarsBadge extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
this.userName = "CodeYourFuture";
this.userData = [];
}
function extractCodeWarsAPI() {
return fetch("https://www.codewars.com/api/v1/users/karam-ali").then(
(res) => {
if (!res.ok) {
return "Error";
}
return res.json();
}
);
}

connectedCallback() {
this.fetchActivity()
.then(() => {
this.render();
})
.catch((error) => {
console.error(error);
});
}
const cardLayout = document.querySelector(".card_layout");

// fetch the data from the Codewars API
async fetchActivity() {
const response = await fetch(
`https://www.codewars.com/api/v1/users/${this.userName}`
);
const data = await response.json();
this.userData = data; // set the userData property with the fetched data
function creatingElements(tag, classList, textContent) {
const creatingElement = document.createElement(tag);
if (classList) {
creatingElement.classList.add(classList);
}

render() {
this.shadowRoot.innerHTML = `
<style>
:host {
--rank: ${this.userData.ranks.overall.color};
font: 600 100%/1 system-ui, sans-serif;
}
data {
color: var(--rank);
border: 3px solid;
padding: .25em .5em;
}
</style>
<data value="${this.userData.ranks.overall.score}">
${this.userData.ranks.overall.name}
</data>`;
if (textContent) {
creatingElement.textContent = textContent;
}
return creatingElement;
}

customElements.define("codewars-badge", CodeWarsBadge);
function renderUserData(data) {
const rankAndHonor = creatingElements("div", "rank__honor");
const rank = creatingElements("h1", "", `Rank: ${data.ranks.overall.name}`);
const honor = creatingElements("h1", "", `Honor: ${data.honor}`);

rankAndHonor.appendChild(rank);
rankAndHonor.appendChild(honor);

cardLayout.appendChild(rankAndHonor);
}

function renderMainInfo(data) {
const mainCardInfo = creatingElements("div", "main__details");
const username = creatingElements("h1", "", `Username: ${data.username}`);
const userID = creatingElements("h1", "", `User ID: ${data.id}`);
const languages = creatingElements("h1", "", "Languages: JavaScript");
const score = creatingElements(
"h1",
"",
`Score: ${data.ranks.overall.score}`
);
const totalCompleted = creatingElements(
"h1",
"",
`Total Completed: ${data.codeChallenges.totalCompleted}`
);

mainCardInfo.appendChild(username);
mainCardInfo.appendChild(userID);
mainCardInfo.appendChild(languages);
mainCardInfo.appendChild(score);
mainCardInfo.appendChild(totalCompleted);

cardLayout.appendChild(mainCardInfo);
}

function renderCardLabel() {
const cardTitle = creatingElements("div", "comp__title", "CodeWars Tracker");
cardLayout.insertAdjacentElement("beforeend", cardTitle);
}

function render() {
extractCodeWarsAPI()
.then((data) => {
renderUserData(data);
renderMainInfo(data);
})
.then(() => renderCardLabel());
}

window.onload = render;

3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
content="Extending HTML to create a new component"
/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="style.css">
</head>
<body>
<codewars-badge></codewars-badge>
<main class="card_layout"></main>
<script async defer type="module" src="./codewars-badge.js"></script>
</body>
</html>
37 changes: 37 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}

body {
color: #f7edb2;
font-family: "Inconsolata", sans-serif;
padding: 3rem;
}

.card_layout {
width: 52rem;
height: 33rem;
background-color: #625f69;
border-radius: 12px;
display: grid;
grid-template-rows: 6.6rem 1fr 3rem;
align-items: center;
}

.rank__honor {
display: flex;
justify-content: space-between;
padding: 0 2rem;
}

.main__details {
padding: 0 5.8rem;
line-height: 3.2rem;
}

.comp__title {
justify-self: end;
padding: 0 2rem;
}