-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.js
More file actions
318 lines (314 loc) · 9.71 KB
/
Copy pathadmin.js
File metadata and controls
318 lines (314 loc) · 9.71 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
const showadminregistration = (isShowing = true, email = null, uiid = null) => {
loadingBox();
checkSessionValidation(
client.admin,
(_) => {
const confirmLogout = new Dialog();
getSessionUserData(client.admin).then((data) => {
confirmLogout.setDisplay(
"Already Logged In.",
`You are currently logged in as <b class="active">${data.username} (${data.id})</b>.
You need to log out before creating a new account. Confirm log out?`
);
confirmLogout.createActions(
["Stay logged in", "Log out"],
[actionType.positive, actionType.negative]
);
confirmLogout.onButtonClick([
() => {
confirmLogout.hide();
},
() => {
confirmLogout.loader();
finishSession(client.admin, (_) => {
showadminregistration(true);
});
},
]);
confirmLogout.show();
});
},
(_) => {
const regDial = new Dialog();
regDial.setDisplay(
"Create Admin Account",
"Create a new account with a working email address (individual or institution).",
"/graphic/illustrations/adminview.svg"
);
regDial.createActions(
["Next", "Cancel"],
[actionType.positive, actionType.negative]
);
regDial.createInputs(
[
"Your name",
"Email Address",
"New Password",
"Unique Institution ID - UIID",
],
[
"Shravan Kumar, or something?",
"youremail@example.domain",
"Strong password",
"A unique ID for your institution",
],
["text", "email", "password", "text"],
[
validType.name,
validType.email,
validType.password,
validType.username,
],
[null, email, null, uiid],
["name", "email", "new-password", "username"],
[true, false, false, false],
["words", "off", "off", "off"]
);
regDial.validate(0, (_) => {
regDial.getInput(1).focus();
});
regDial.validate(1, (_) => {
regDial.getInput(2).focus();
});
regDial.validate(2, (_) => {
regDial.getInput(3).focus();
});
regDial.validate(3);
regDial.onButtonClick([
() => {
if (!regDial.allValid()) {
regDial.validateNow(0, (_) => {
regDial.getInput(1).focus();
});
regDial.validateNow(1, (_) => {
regDial.getInput(2).focus();
});
regDial.validateNow(2, (_) => {
regDial.getInput(3).focus();
});
regDial.validateNow(3);
} else {
regDial.normalize();
regDial.loader();
createAccount(
regDial,
regDial.getInputValue(0).trim(),
regDial.getInputValue(1).trim(),
regDial.getInputValue(2),
regDial.getInputValue(3).trim()
);
}
},
() => {
regDial.hide();
},
]);
regDial.existence(isShowing);
}
);
};
const createAccount = (dialog, adminname, email, password, uiid) => {
postJsonData(post.admin.auth, {
action: "signup",
username: adminname,
email: email,
password: password,
uiid: uiid,
})
.then((result) => {
if (result.event == code.auth.ACCOUNT_CREATED) {
saveDataLocally(result.user);
return relocate(locate.admin.session, {
u: result.user.uid,
target: locate.admin.target.register,
});
}
dialog.loader(false);
switch (result.event) {
case code.auth.USER_EXIST: {
dialog.inputField[1].showError("Account already exists.");
return snackBar("Try signing in?", "Login", true, (_) => {
refer(locate.admin.login, { email: email, uiid: uiid });
});
}
case code.server.UIID_TAKEN:
return dialog.inputField[3].showError(
"This UIID is not available. Try something different."
);
case code.auth.EMAIL_INVALID:
return dialog.inputField[1].showError("Invalid email address.");
//todo: check invalidity and show suggesstions
case code.auth.PASSWORD_INVALID:
return dialog.inputField[2].showError(
"Weak password, try something better."
);
case code.auth.NAME_INVALID:
return dialog.inputField[0].showError(
"This doesn't seem like a name."
);
default:
return snackBar(`${result.event}:${result.msg}`, "Report");
}
})
.catch((error) => {
dialog.loader(false);
clog(error);
snackBar(error, "Report");
});
};
function linkGenerator(target) {
loadingBox(
true,
"Generating Link",
`A link is being created for your to share with ${target}s of ${localStorage.getItem(
"uiid"
)} institute`
);
postJsonData(post.admin.manage, {
type: "invitation",
action: "create",
target: target,
})
.then((response) => {
if (
response.event == code.invite.LINK_EXISTS ||
response.event == code.invite.LINK_CREATED
) {
let linkdialog = new Dialog();
linkdialog.setDisplay(
"Invitation Link",
`<center><a href="${
response.link
}" target="_blank" rel="noreferrer">${response.link}</a>
<br/>This Link will automatically expire on <b>${getProperDate(
String(response.exp)
)}</b><br/><br/>
<div class="switch-view" id="teachereditschedulecontainer">
<span class="switch-text positive">Allow new teachers to add schedule?</span>
<label class="switch-container">
<input type="checkbox" id="teachereditschedulei">
<span class="switch-positive" id="teachereditscheduleview"></span>
</label>
</div>
</center>`,
true
);
new QRCode(getElement(linkdialog.imagedivId), response.link);
this.allowteacherschedule = new Switch("teachereditschedulei");
postJsonData(post.admin.manage, {
type: "preferences",
action: "get",
specific: "allowTeacherAddSchedule",
}).then((allowTeacherAddSchedule) => {
this.allowteacherschedule.turn(allowTeacherAddSchedule);
});
this.allowteacherschedule.onTurnChange(
(_) => {
postJsonData(post.admin.manage, {
type: "preferences",
action: "set",
specific: "allowTeacherAddSchedule",
allow: true,
}).then((resp) => {
this.allowteacherschedule.turn(resp.event == code.OK);
});
},
(_) => {
postJsonData(post.admin.manage, {
type: "preferences",
action: "set",
specific: "allowTeacherAddSchedule",
allow: false,
}).then((resp) => {
this.allowteacherschedule.turn(resp.event != code.OK);
});
}
);
linkdialog.createActions(
["Share", "Disable", "Copy", "Done"],
[
actionType.positive,
actionType.negative,
actionType.positive,
actionType.neutral,
]
);
linkdialog.onButtonClick([
() => {
shareLinkAction("Teacher Invitation", response.link);
},
(_) => {
linkdialog.loader();
revokeLink(target);
},
(_) => {
navigator.clipboard
.writeText(response.link)
.then((_) => {
snackBar("Link copied to clipboard.");
})
.catch((err) => {
snackBar("Failed to copy, please do it manually.", null, false);
});
},
(_) => {
linkdialog.hide();
},
]);
linkdialog.show();
}
switch (response.event) {
case code.invite.LINK_EXISTS:
return snackBar("This link already exists and can be shared.");
case code.invite.LINK_CREATED:
return snackBar("Share this with teachers of your institution.");
case code.invite.LINK_CREATION_FAILED:
return snackBar(`Unable to generate link:${response.msg}`, "Report");
default:
return snackBar(`Error:${response.event}:${response.msg}`, "Report");
}
})
.catch((error) => {
clog(error);
loadingBox(false);
});
}
function revokeLink(target) {
postJsonData(post.admin.manage, {
type: "invitation",
action: "disable",
target: target,
})
.then((response) => {
if (response.event == code.invite.LINK_DISABLED) {
snackBar("All links are inactive now.", null, false);
let nolinkdialog = new Dialog();
nolinkdialog.setDisplay(
"Generate Link",
`Create a link to share with ${target}s of ${localStorage.getItem(
"uiid"
)} institute,
so that they can access and take part in schedule management.`
);
nolinkdialog.createActions(
["Create Link", "Abort"],
[actionType.positive, actionType.negative]
);
nolinkdialog.onButtonClick([
(_) => {
nolinkdialog.hide();
this.linkGenerator(target);
},
(_) => {
nolinkdialog.hide();
},
]);
nolinkdialog.show();
} else {
snackBar(`Link couldn't be disabled.`, "Try again", false, (_) => {
this.revokeLink(target);
});
}
})
.catch((error) => {});
}