-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathcodewars-badge.js
More file actions
277 lines (247 loc) · 6.82 KB
/
codewars-badge.js
File metadata and controls
277 lines (247 loc) · 6.82 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// 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
class CodeWarsBadge extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
this.userName = "CodeYourFuture";
this.userData = [];
}
connectedCallback() {
this.fetchActivity()
.then(() => {
this.render();
})
.catch((error) => {
console.error(error);
});
}
// 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
}
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>`;
}
}
customElements.define("codewars-badge", CodeWarsBadge);
// ______
// || \\ // ||-----|
/////////// ////|| \\// ||_____
/////////// // || ||-----|
/// /// || || ||
// /// \\-_-_| || ||
/////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
let kataColor, kataValue, kataContent;
const template = document.createElement("template");
const templateContent = `
<div class='container'>
<data id='dataTag' value=${kataValue}></data>
<div class='user-card'>
<h3></h3>
<p id='user-name'></p>
<p id='name-of-user'></p>
<p id='clan'></p>
<ul id='skills'></ul>
<div>
<p id='overall-rank'></p>
<p id='languages'></p>
</div>
</div>
</div>
<style>
:host{
--rank: yellow;
font: 600 100%/1 system-ui, sans-serif;
}
.container{
background-color:#626175;
color:white;
margin:2rem auto 2rem auto;
width:60%;
height:fit-content;
padding:1rem;
}
.user-card{
background-color:#444444;
margin-top:1rem;
padding:1rem;
}
h3{
margin:0;
padding:0;
}
ul{
margin:0;
padding:0;
}
li{
margin-bottom:0.3rem;
}
span{
margin-bottom:.25rem;
}
#dataTag{
color:var(--rank);
border: 3px solid ;
padding: .25em .5em;
margin-bottom:1rem;
}
</style>
`;
template.innerHTML = templateContent;
class SelfCreated extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
this.userName = "CodeYourFuture";
this.userData = [];
this.kataColor = null;
this.kataContent = null;
//this.kataValue = null;
}
connectedCallback() {
const form = document.querySelector("#username-form");
form.addEventListener("submit", (event) => {
event.preventDefault(); // Prevent the default form submission
this.getUserName();
});
this.fetching()
.then(() => this.userData)
.then(() => {
//kataColor = this.userData.ranks.overall.color;
//kataContent = this.userData.ranks.overall.name;
//kataValue = this.userData.ranks.overall.score;
console.log(kataColor, kataContent, kataValue);
this.render();
})
.catch((error) => console.log(error, "<---------- error happened"));
}
async fetching() {
const response = await fetch(
`https://www.codewars.com/api/v1/users/${this.userName}`
);
const data = await response.json();
this.userData = data;
}
getUserName() {
const input = document.querySelector("input");
const value = input.value.trim();
if (value !== "") {
this.userName = value;
this.fetching().then(() => this.render());
} else {
this.userName = "CodeYourFuture";
}
}
render() {
//template.innerHTML = "";
this.shadowRoot.innerHTML = "";
this.shadowRoot.appendChild(template.content.cloneNode(true));
this.shadowRoot.querySelector("h3").textContent = "The User Profile:";
this.shadowRoot.querySelector(
"#user-name"
).textContent = `USER NAME: ${this.userData.username}`;
this.shadowRoot.querySelector(
"#name-of-user"
).textContent = `Name: ${this.userData.name}`;
const skillsList = this.shadowRoot.querySelector("#skills");
skillsList.innerHTML = "";
const skillArray = this.userData.skills;
if (skillArray.length != 0) {
skillArray.forEach((skill) => {
const li = document.createElement("li");
li.textContent = skill;
skillsList.appendChild(li);
});
} else {
skillsList.textContent = "Skills: No Skill To Display!";
}
this.shadowRoot.querySelector(
"#clan"
).textContent = `Clan: ${this.userData.clan}`;
this.shadowRoot.querySelector(
"#overall-rank"
).textContent = `Overall Score: ${this.userData.ranks.overall.score}`;
const languages = this.userData.ranks.languages;
this.shadowRoot.querySelector("#languages").innerHTML = "";
for (const key in languages) {
const span = document.createElement("span");
span.innerText = `${key}: ${languages[key].score}\n `;
this.shadowRoot.querySelector("#languages").appendChild(span);
}
const dataTag = this.shadowRoot.querySelector("#dataTag");
dataTag.textContent = kataContent;
//`${this.userData.ranks.overall.color}`;
}
}
window.customElements.define("self-created", SelfCreated);
const obj = {
username: "some_user",
name: "Some Person",
honor: 544,
clan: "some clan",
leaderboardPosition: 134,
skills: [
"ruby",
"c#",
".net",
"javascript",
"coffeescript",
"nodejs",
"rails",
],
ranks: {
overall: {
rank: -3,
name: "3 kyu",
color: "blue",
score: 2116,
},
languages: {
javascript: {
rank: -3,
name: "3 kyu",
color: "blue",
score: 1819,
},
ruby: {
rank: -4,
name: "4 kyu",
color: "blue",
score: 1005,
},
coffeescript: {
rank: -4,
name: "4 kyu",
color: "blue",
score: 870,
},
},
},
codeChallenges: {
totalAuthored: 3,
totalCompleted: 230,
},
};
// CodeYourFuture