Repository navigation
Expand file tree
/
Copy pathgithub.js
More file actions
157 lines (139 loc) · 4.8 KB
/
Copy pathgithub.js
File metadata and controls
157 lines (139 loc) · 4.8 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
var widget = {
options: {
id: "",
API_URL:"https://api.widgethub.space/",
theme:
{
background1: "indigo-800",
background2: "blue-800",
titleColor: "indigo-50",
titleHoverColor: "red-500",
descriptionColor: "gray-300"
},
edges: true,
customMessage: "",
div: "widgethub-githubdiv"
},
targetHtmlEle: "",
messages: {
missingId: "userid is invalid",
missingTargetHtml: "not found html element with specified id",
missingParams: "not enough params",
errorFetching: "Error fetching data from remote url"
},
setOptions: function(options) {
if (options.id) {
this.options.id = options.id;
}
if (options.theme) {
this.options.theme = options.theme
}
if (options.edges) {
this.options.edges = options.edges;
}
if (options.customMessage) {
this.options.customMessage = options.customMessage;
}
if (options.div) {
this.options.div = options.div;
}
},
validate: function () {
if (!this.options.id) {
console.log(this.messages.missingId);
}
this.targetHtmlEle = document.getElementById(this.options.div);
if(!this.targetHtmlEle) {
console.log(this.messages.missingTargetHtml);
return false;
}
return true;
},
render: async function() {
if (!this.validate()) {
return;
}
// Loader gif
this.targetHtmlEle.innerHTML = '<img class="widgethub-github" src="https://i.pinimg.com/originals/df/d2/68/dfd2683c9701642c776e31d3b0d603a9.gif" />';
//this.applyCSS();
this.targetHtmlEle.innerHTML = this.template(await this.fetch());
},
fetch: function() {
const data = JSON.stringify({
query: `query MyQuery {
getGithubUser(username:"${this.options.id}"){
username
followers
following
repoCount
avatarUrl
pastYearContributions
}
}`
})
//callback(message)
return fetch(this.options.API_URL+"graphql",
{
method: 'POST',
body: data,
headers: {
'Content-Type': 'application/json',
}
}
)
.then((res) => res.json())
.catch(err => console.log(err,"error fetching"))
},
template: function(response) {
console.log(response);
response = response.data.getGithubUser;
theme = this.options.theme;
logo = "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
var html = '<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">';
html+= `<div class="flex items-center">
<div class="max-w-xs">
<div class="bg-gradient-to-r from-${theme.background1} to-${theme.background2} shadow-xl rounded-2xl py-3">
<div class="photo-wrapper p-2">
<img class="w-32 h-32 rounded-full mx-auto" src="${response.avatarUrl}" alt="Avatar">
</div>
<div class="p-2">
<div class="flex items-center justify-center">
<img class="w-6 h-6 rounded-full m-1" src="${logo}" alt="Logo">
<h3 class="text-center text-xl text-${theme.titleColor}
font-medium leading-8 hover:text-${theme.titleHoverColor}"
><a href="https://github.com/${response.username}" target="_blank">${response.username}</a></h3>
</div>
`
if (this.options.customMessage !== "") {
html+=`<div class="text-center text-${theme.descriptionColor} text-xs font-semibold">
<p>${this.options.customMessage}</p>
</div>`
}
html+=`
<table class="text-xs mt-2">
<tbody><tr>
<td class="px-2 py-2 text-${theme.titleColor} font-bold">Followers</td>
<td class="px-2 py-2 text-${theme.descriptionColor}">${response.followers}</td>
</tr>
<tr>
<td class="px-2 py-2 text-${theme.titleColor} font-bold">Following</td>
<td class="px-2 py-2 text-${theme.descriptionColor}">${response.following}</td>
</tr>
<tr>
<td class="px-2 py-2 text-${theme.titleColor} font-bold">Repository Count</td>
<td class="px-2 py-2 text-${theme.descriptionColor}">${response.repoCount}</td>
</tr>
<tr>
<td class="px-2 py-2 text-${theme.titleColor} font-bold">Past Year Contributions</td>
<td class="px-2 py-2 text-${theme.descriptionColor}">${response.pastYearContributions}</td>
</tr>
</tbody></table>
</div>
</div>
</div>
</div>`
return html;
},
applyCss: function() {
}
}