Skip to content

NW6 | Nazanin Saedi | API PROJECT \ #19

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
76 changes: 58 additions & 18 deletions codewars-badge.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
// This native web component fetches data from the Codewars API and renders it as a badge
// 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

// Existing component to display user's rank and honor points
class CodeWarsBadge extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
this.userName = "CodeYourFuture";
this.userData = [];
this.userData = {};
}

connectedCallback() {
this.fetchActivity()
this.fetchUserData()
.then(() => {
this.render();
})
Expand All @@ -20,32 +17,75 @@ class CodeWarsBadge extends HTMLElement {
});
}

// fetch the data from the Codewars API
async fetchActivity() {
async fetchUserData() {
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
this.userData = await response.json();
}

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

customElements.define("codewars-badge", CodeWarsBadge);

// New component to display user's username
class CodeWarsName extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
this.userName = "CodeYourFuture"; // Set default username
this.userData = {};
}

connectedCallback() {
this.fetchUserData()
.then(() => {
this.render();
})
.catch((error) => {
console.error(error);
});
}

async fetchUserData() {
const response = await fetch(
`https://www.codewars.com/api/v1/users/${this.userName}`
);
this.userData = await response.json();
}

render() {
this.shadowRoot.innerHTML = `
<style>
:host {
font: 600 100%/1 system-ui, sans-serif;
}
span {
color: #333;
}
</style>
<span>${this.userData.username}</span>`;
}
}

customElements.define("codewars-name", CodeWarsName);
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</head>
<body>
<codewars-badge></codewars-badge>
<codewars-name></codewars-name>
<script async defer type="module" src="./codewars-badge.js"></script>
</body>
</html>
Loading