-
Notifications
You must be signed in to change notification settings - Fork 23
/
edit.js
47 lines (44 loc) 路 1.3 KB
/
edit.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
const FIELDS = [
'socials_instagram',
// 'socials_snapchat',
'privacy_hide_image',
'privacy_hide_email',
'privacy_hide_room',
'privacy_hide_phone',
'privacy_hide_address',
'privacy_hide_major',
'privacy_hide_birthday'
];
const submit = document.getElementById('submit');
const error = document.getElementById('error');
const success = document.getElementById('success');
submit.onclick = async (e) => {
error.innerText = '';
success.innerText = '';
const data = {};
for(const field of FIELDS) {
const elem = document.getElementById(field);
if(elem.type === 'checkbox') data[field] = elem.checked;
else if(elem.type === 'text') data[field] = elem.value;
}
let response;
try {
response = await fetch('/edit', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
} catch(e) {
console.error(e);
error.innerText = 'An unexpected error has occurred.';
}
if(response.status === 200) {
success.innerText = 'Your profile has been updated';
} else {
console.error(await response.text());
const json = await response.json();
error.innerText = json.message;
}
}