settings.js (2070B) download
1function toFullColor(col) {
2 // expecting either '#rrggbb' or '#rgb' and converting to '#rrggbb'
3
4 if (col.length == 7)
5 return col;
6
7 let r = col[1],
8 g = col[2],
9 b = col[3];
10
11 return '#' + r + r + g + g + b + b;
12}
13
14function updateLogin() {
15 let span = document.getElementById('login');
16
17 getUser().then(user => {
18 if (!user) {
19 window.location.href = 'login.html?m=restricted';
20 }
21
22 span.innerText = `Logged in as ${user.name}`;
23 });
24}
25
26function updateForm() {
27 getUser().then(user => {
28 callBackend('GET', `api/player/${user.id}/email`).then(mail => {
29 document.getElementById('email').value = mail;
30 });
31
32 callBackend('GET', `api/player/${user.id}/preferences`).then(({ color_found, color_closed, preferred_api }) => {
33 if (!preferred_api)
34 preferred_api = 'none';
35 if (!color_found)
36 color_found = '#00ff00';
37 if (!color_closed)
38 color_closed = '#ffff00';
39
40 document.getElementById('color-found').value = toFullColor(color_found);
41 document.getElementById('color-closed').value = toFullColor(color_closed);
42 document.getElementById('api').value = preferred_api;
43 });
44 });
45}
46
47document.getElementById('submit').addEventListener('click', () => {
48 getUser().then(user => {
49 let set_pref = callBackend('POST', `api/player/${user.id}/preferences`, {
50 id: user.id,
51 color_found: document.getElementById('color-found').value,
52 color_closed: document.getElementById('color-closed').value,
53 api: document.getElementById('api').value
54 });
55
56 let set_email = callBackend('PUT', `api/player/${user.id}/email`, {
57 email: document.getElementById('email').value
58 });
59
60 return Promise.all([ set_email, set_pref ]);
61 }).then(() => {
62 window.location.href = 'settings.html?m=success'
63 });
64});
65
66function updateMessage() {
67 let span = document.getElementById('message');
68
69 if (!window.location.search.startsWith('?'))
70 return;
71
72 let code = new URLSearchParams(window.location.search).get('m');
73
74 switch (code) {
75 case 'success':
76 span.innerText = 'Succeed';
77 break;
78 }
79}
80
81updateMessage();
82updateLogin();
83updateForm();