-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodewars-badge.js
More file actions
117 lines (102 loc) · 2.87 KB
/
codewars-badge.js
File metadata and controls
117 lines (102 loc) · 2.87 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
// Existing component to display user's rank and honor points
class CodeWarsBadge extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
this.userName = "Della%20Bella%20";
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 {
display: block; /* Already block, which is needed */
padding: 1.5em;
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
background-color: #ffffff;
color: #333;
border: 1px solid #e0e0e0;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.08);
box-sizing: border-box;
/* --- Centering Styles --- */
max-width: 600px; /* Or choose a width like 'width: 80%;' or 'width: 500px;' */
margin-left: auto;
margin-right: auto;
/* --- End Centering Styles --- */
margin-bottom: 1em; /* Keep the space below */
}
/* Add other styles for elements inside your component */
h2 {
margin-top: 0;
color: #111;
}
p {
line-height: 1.5;
}
p > br {
display: none;
}
</style>
<h1> Codewars Component CYF </h1>
<data value="${this.userData.ranks.overall.score}">
${this.userData.ranks.overall.name}
</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 = "Della%20Bella%20"; // 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: #black;
}
</style>
<span>${this.userData.username}</span>`;
}
}
customElements.define("codewars-name", CodeWarsName);