aboutsummaryrefslogtreecommitdiff
path: root/static/widgets/password-dialog/index.js
blob: 6dbba12c9beec2c28117955771f98a3d0327f2cd (plain)
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
import * as api from 'api';
import icons from 'icons';
import * as sfw from 'sfw';
const { Div, Input } = sfw.element.native;

const css = await sfw.css(import.meta.url, './index.css');

export default class PasswordDialog extends sfw.element.Container {
	#current
	#new
	#confirm
	#error

	constructor() {
		super({ css });

		this.callclose = () => this.close();

		document.body.addEventListener('click', this.callclose);
		this.onclick = (e) => e.stopPropagation();

		this.body.append(Div.new({
			id: 'container',
			children: [
				Div.new({ id: 'title', innerText: 'Change Password' }),
				Div.new({ id: 'close', children: [ icons.close ], onclick: () => this.close() }),
				this.#current = Input.new({ placeholder: 'Current Password', type: 'password' }),
				this.#new = Input.new({ placeholder: 'New Password', type: 'password' }),
				this.#confirm = Input.new({ placeholder: 'Confirm Password', type: 'password' }),
				this.#error = Div.new({ id: 'error', className: 'hidden' }),
				Div.new({
					id: 'button',
					innerText: 'Update',
					onclick: async () => {
						if (this.#new.value !== this.#confirm.value) {
							this.#error.innerText = 'Passwords do not match';
							this.#error.className = '';
							return;
						}

						if (!await api.profile.update_password(this.#current.value, this.#new.value)) {
							this.#error.innerText = 'invalid password';
							this.#error.className = '';
							return;
						}

						this.close();
					},
				}),
			],
		}));
	}

	close() {
		this.parentNode.removeChild(this);
		document.body.removeEventListener('click', this.callclose);
	}
}