-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.js
137 lines (116 loc) · 4.27 KB
/
settings.js
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
function registerButtonHandlers() {
$('#remove-account').on('click', deleteSelfUser);
$('#colour-update').on('click', updateSelfUserColour);
$('#password-update').on('click', updateSelfUserPassword);
// register same callback for enter on password field
$('#password-field').keypress(function (event) {
let keycode = (event.keyCode ? event.keyCode : event.which);
if (keycode == '13') {
updateSelfUserPassword();
}
});
}
function fillPreferenceTable(userdata) {
// display name
$('#name-td').text(capitalizeFirstLetter(getUserName()));
// display colour
$('#colour-field').attr('value', '#' + userdata.preferredColour);
// display role
let role = userdata.role;
$('#role-td').text(capitalizeFirstLetter(role.split('_')[1]));
}
function getUserDataAndFillPreferenceTable() {
// get json bundle with all user data as JSON from api backend
fetch(getContextPath() + '/api/users/' + getUserName() + '?access_token=' + getAccessToken())
.then(result => result.json())
.then(json => {
if (json.error)
throw Error(json.error);
else {
console.log("Retrieved account details are:" + json);
fillPreferenceTable(json)
}
})
.catch(error => { // logout, redirect to login in case the credentials were rejected.
console.log(error);
logout();
});
}
/**
* Sends an API request to update the colour of an existing user. Only for use by the user owning the account to be
* modified.
*/
function updateSelfUserColour() {
//extract color from users colour picker
let nextColour = document.getElementById('colour-field').value.slice(1).toUpperCase();
console.log('Changing preferred colour of ' + getUserName() + ' to: ' + nextColour);
// build a payload object that complies to the format expected by the backend:
let postdata = {"colour": nextColour};
// actually send request to API
fetch(getContextPath() + '/api/users/' + getUserName() + '/colour?access_token=' + getAccessToken(), {
headers: {
'Content-Type': 'application/json'
},
method: 'post',
body: JSON.stringify(postdata)
})
.then(reply => {
if (reply.ok)
// update page content
location.reload();
else {
reply.text()
.then(text => alert(text));
}
})
}
/**
* Sends an API request to update the password of an existing user. Only for use by the user owning the account to be
* modified.
*/
function updateSelfUserPassword() {
//extract next password from input field
let passwordField = document.getElementById('password-field');
let nextPassword = passwordField.value;
console.log('Changing password of ' + getUserName() + ' to: ----------');
// build a payload object that complies to the format expected by the backend:
// Note: old password is not needed, for an admin token is used to authenticate the request.
let postdata = {"nextPassword": nextPassword, "oldPassword": "---"};
// actually send request to API
fetch(getContextPath() + '/api/users/' + getUserName() + '/password?access_token=' + getAccessToken(), {
headers: {
'Content-Type': 'application/json'
},
method: 'post',
body: JSON.stringify(postdata)
})
.then(reply => {
if (reply.ok)
// update list of displayed users
location.reload();
else {
passwordField.value='';
reply.text()
.then(text => alert(text));
}
})
}
/**
* Sends a request to api to delete an existing user by name. Only for use by the user owning the account to be
* modified.
* @param user
*/
function deleteSelfUser() {
console.log("Deleting user: " + getUserName());
fetch(getContextPath() + '/api/users/' + getUserName() + '?access_token=' + getAccessToken(), {
method: 'delete',
})
.then(reply => {
if (reply.ok) {
logout();
} else {
reply.text()
.then(text => alert(text));
}
})
}