forked from noflo/noflo-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoflo-account-settings.html
182 lines (182 loc) · 6.3 KB
/
noflo-account-settings.html
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
<polymer-element name="noflo-account-settings" attributes="user gridToken plan theme" class="modal-content" on-click="{{ close }}">
<template>
<style>
.plan strong {
text-transform: uppercase;
color: black;
background-color: hsl(190, 90%, 45%);
text-decoration: none;
padding: 2px;
border-radius: 2px;
}
.plan .free,
.plan .beta,
.plan .backer {
background-color: hsl(135,90%,45%);
}
button.refresh {
border: none;
background-color: transparent;
}
button.relogin {
background-color: transparent;
color: hsl(190, 100%, 30%);
border: 1px solid hsl(190, 100%, 30%);
padding: 2px;
border-radius: 3px;
font-family: "SourceCodePro",Helvetica,Arial,sans-serif;
}
</style>
<div class="modal-container" on-click="{{ bgClick }}">
<h1>Account settings for {{ user.name }}</h1>
<form>
<label>
User Identifier
<input type="text" readonly value="{{ user.uuid }}">
</label>
<label>
Flowhub plan
<div class="plan"><strong class="{{ plan }}">{{ plan }}</strong> <button id="refresh" class="refresh" on-click="{{ checkPlan }}"><i class="fa fa-refresh"></i></button></div>
<template if="{{ statusText }}">
<div>
<span>{{ statusText }}</span>
<template if="{{ requestLogin }}">
<button class="relogin" on-click="{{ relogin }}">Login</button>
</template>
</div>
</template>
</label>
<label>
Theme
<select name="type" value="{{ theme }}">
<option value="dark">Lazer</option>
<option value="light">Tube</option>
</select>
</label>
<div class="toolbar">
<button on-click="{{ send }}">Save</button>
<a on-click="{{ close }}">Cancel</a>
</div>
</form>
</div>
</template>
<script>
Polymer('noflo-account-settings', {
user: null,
statusText: '',
requestLogin: false,
newPlan: '',
theme: '',
gridToken: '',
attached: function () {
document.getElementById('container').classList.add('blur');
},
detached: function () {
document.getElementById('container').classList.remove('blur');
},
checkFlowhubUser: function (callback) {
var req = new XMLHttpRequest();
req.onreadystatechange = function () {
if (req.readyState !== 4) {
return;
}
if (req.status !== 200) {
return callback(new Error(req.responseText));
}
callback(null, JSON.parse(req.responseText));
};
req.open('GET', 'https://api.flowhub.io/user', true);
req.setRequestHeader('Authorization', 'Bearer ' + this.gridToken);
req.send(null);
},
checkGithubUser: function (token, callback) {
var req = new XMLHttpRequest();
req.onreadystatechange = function () {
if (req.readyState !== 4) {
return;
}
if (req.status !== 200) {
return callback(new Error(req.responseText));
}
callback(null, req.getResponseHeader('X-OAuth-Scopes').split(', '));
};
req.open('GET', 'https://api.github.com/user?' + Date.now(), true);
req.setRequestHeader('Authorization', 'token ' + token);
req.send(null);
},
checkPlan: function (event) {
event.preventDefault();
event.stopPropagation();
this.$.refresh.classList.add('fa-spin');
this.checkFlowhubUser(function (err, user) {
if (err) {
this.statusText = 'Failed to fetch Flowhub user information, please re-login';
this.requestLogin = true;
this.$.refresh.classList.remove('fa-spin');
return;
}
this.newPlan = user.plan.type;
if (this.newPlan !== this.plan) {
this.statusText = 'Your Flowhub plan has been changed to ' + this.newPlan + ', please re-login';
this.requestLogin = true;
return;
}
if (!user.github || !user.github.token) {
if (user.plan.type !== 'free') {
this.statusText = 'Your GitHub token doesn\'t have private repository access, please re-login';
this.requestLogin = true;
return;
}
this.statusText = 'Your GitHub token doesn\'t have repository access, please re-login';
this.requestLogin = true;
return;
}
this.checkGithubUser(user.github.token, function (err, scopes) {
console.log(this.newPlan, scopes);
this.$.refresh.classList.remove('fa-spin');
if (err) {
this.statusText = 'Failed to fetch GitHub user information, please re-login';
this.requestLogin = true;
return;
}
if (user.plan.type === 'free' && (scopes.indexOf('public_repo') === -1 && scopes.indexOf('repo') === -1)) {
this.statusText = 'Your GitHub token doesn\'t have repository access, please re-login';
this.requestLogin = true;
return;
}
if (user.plan.type !== 'free' && scopes.indexOf('repo') === -1) {
this.statusText = 'Your GitHub token doesn\'t have private repository access, please re-login';
this.requestLogin = true;
return;
}
this.statusText = 'Everything up to date';
this.requestLogin = false;
}.bind(this));
}.bind(this));
},
relogin: function (event) {
event.preventDefault();
event.stopPropagation();
this.fire('relogin', this.newPlan);
},
send: function (event) {
event.preventDefault();
event.stopPropagation();
this.fire('updated', {
theme: this.theme
});
this.close();
},
bgClick: function (event) {
// Don't close if clicking within container
event.stopPropagation();
},
close: function () {
if (!this.parentNode) {
return;
}
this.parentNode.removeChild(this);
}
});
</script>
</polymer-element>