2021-07-20 12:44:23 +00:00
|
|
|
/*
|
2022-01-08 16:46:05 +00:00
|
|
|
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
2021-07-20 12:44:23 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-10-08 09:54:29 +00:00
|
|
|
import registerShowPassword from "../../components/showPassword/";
|
|
|
|
import CopyIcon from "../../components/clipboard/";
|
|
|
|
import createError from "../../components/error/";
|
2021-07-21 15:14:22 +00:00
|
|
|
|
2021-10-08 09:54:29 +00:00
|
|
|
import emailExists from "../../auth/register/ts/emailExists";
|
|
|
|
import userExists from "../../auth/register/ts/userExists";
|
2021-07-21 15:14:22 +00:00
|
|
|
|
2021-10-08 09:54:29 +00:00
|
|
|
import LazyElement from "../../utils/lazyElement";
|
|
|
|
import isBlankString from "../../utils/isBlankString";
|
|
|
|
import getFormUrl from "../../utils/getFormUrl";
|
|
|
|
import genJsonPayload from "../../utils/genJsonPayload";
|
2021-07-21 15:14:22 +00:00
|
|
|
|
2021-10-08 09:54:29 +00:00
|
|
|
import VIEWS from "../../views/v1/routes";
|
2021-07-20 12:44:23 +00:00
|
|
|
|
2021-10-08 09:54:29 +00:00
|
|
|
const SECRET_COPY_ICON = "settings__secret-copy";
|
|
|
|
const SECRET_COPY_DONE_ICON = "settings__secret-copy-done";
|
2021-07-20 12:44:23 +00:00
|
|
|
|
2021-07-21 15:14:22 +00:00
|
|
|
// form IDs
|
2021-10-08 09:54:29 +00:00
|
|
|
const DELETE_FORM = "settings__delete-form";
|
|
|
|
const EMAIL_FORM = "settings__email-form";
|
|
|
|
const USERNAME_FORM = "settings__username-form";
|
|
|
|
const SECRET_FORM = "settings__secret-form";
|
2021-07-21 15:14:22 +00:00
|
|
|
|
|
|
|
// form elements
|
|
|
|
const deleteForm = new LazyElement(DELETE_FORM);
|
|
|
|
const emailForm = new LazyElement(EMAIL_FORM);
|
2021-08-13 13:28:04 +00:00
|
|
|
const usernameForm = new LazyElement(USERNAME_FORM);
|
2021-07-21 15:14:22 +00:00
|
|
|
const secretForm = new LazyElement(SECRET_FORM);
|
|
|
|
|
|
|
|
// field IDs
|
2021-10-08 09:54:29 +00:00
|
|
|
const EMAIL = "email";
|
|
|
|
const USERNAME = "username";
|
2021-07-21 15:14:22 +00:00
|
|
|
|
|
|
|
// field elements
|
|
|
|
const emailField = new LazyElement(EMAIL);
|
2021-08-13 13:28:04 +00:00
|
|
|
const usernameField = new LazyElement(USERNAME);
|
2021-07-21 15:14:22 +00:00
|
|
|
|
|
|
|
// form event handlers
|
|
|
|
const updateEmail = async (e: Event) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const emailElement = <HTMLInputElement>emailField.get();
|
|
|
|
const email = emailElement.value;
|
2021-10-08 09:54:29 +00:00
|
|
|
isBlankString(email, "email", e);
|
2021-07-21 15:14:22 +00:00
|
|
|
if (await emailExists(emailElement)) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
const url = getFormUrl(<HTMLFormElement>emailForm.get());
|
|
|
|
const payload = {
|
|
|
|
email,
|
|
|
|
};
|
|
|
|
|
|
|
|
const res = await fetch(url, genJsonPayload(payload));
|
|
|
|
if (res.ok) {
|
|
|
|
window.location.reload();
|
|
|
|
} else {
|
|
|
|
const err = await res.json();
|
|
|
|
createError(err.error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-07-20 12:44:23 +00:00
|
|
|
|
2021-08-13 13:28:04 +00:00
|
|
|
const updateUsername = async (e: Event) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const usernameElement = <HTMLInputElement>usernameField.get();
|
|
|
|
const username = usernameElement.value;
|
2021-10-08 09:54:29 +00:00
|
|
|
isBlankString(username, "username", e);
|
2021-08-13 13:28:04 +00:00
|
|
|
if (await userExists(usernameElement)) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
const url = getFormUrl(<HTMLFormElement>usernameForm.get());
|
|
|
|
const payload = {
|
|
|
|
username,
|
|
|
|
};
|
|
|
|
const res = await fetch(url, genJsonPayload(payload));
|
|
|
|
if (res.ok) {
|
|
|
|
window.location.reload();
|
|
|
|
} else {
|
|
|
|
const err = await res.json();
|
|
|
|
createError(err.error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-21 15:14:22 +00:00
|
|
|
const updateSecret = (e: Event) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const msg =
|
2021-10-08 09:54:29 +00:00
|
|
|
"WARNING: updating secret will cause service disruption if old secret is still in use post update";
|
2021-07-21 15:14:22 +00:00
|
|
|
if (confirm(msg)) {
|
|
|
|
window.location.assign(VIEWS.updateSecret);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const deleteAccount = (e: Event) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const msg =
|
|
|
|
"WARNING: all CAPTCHA configurations will be lost. This action can't be undone";
|
|
|
|
if (confirm(msg)) {
|
|
|
|
window.location.assign(VIEWS.deleteAccount);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// regist form event handlers
|
|
|
|
const registerForms = () => {
|
2021-10-08 09:54:29 +00:00
|
|
|
deleteForm.get().addEventListener("submit", (e) => deleteAccount(e), true);
|
|
|
|
emailForm.get().addEventListener("submit", (e) => updateEmail(e), true);
|
|
|
|
usernameForm.get().addEventListener("submit", (e) => updateUsername(e), true);
|
2021-08-13 13:28:04 +00:00
|
|
|
console.log(usernameField.get());
|
|
|
|
usernameField
|
|
|
|
.get()
|
2021-10-08 09:54:29 +00:00
|
|
|
.addEventListener("input", async () => await userExists(), false);
|
|
|
|
secretForm.get().addEventListener("submit", (e) => updateSecret(e), true);
|
2021-07-21 15:14:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// set up copying account secret to clipboard
|
|
|
|
const initCopySecret = () => {
|
2021-07-20 12:44:23 +00:00
|
|
|
const secretElement = <HTMLElement>(
|
|
|
|
document.querySelector(`.${SECRET_COPY_ICON}`)
|
|
|
|
);
|
|
|
|
const writeText = secretElement.dataset.secret;
|
2021-07-21 05:12:25 +00:00
|
|
|
new CopyIcon(writeText, secretElement, SECRET_COPY_DONE_ICON);
|
2021-07-20 12:44:23 +00:00
|
|
|
};
|
|
|
|
|
2021-07-21 15:14:22 +00:00
|
|
|
/// TODO email update button should only change if email value has been changed
|
2021-10-08 09:54:29 +00:00
|
|
|
const index = (): void => {
|
2021-07-21 15:14:22 +00:00
|
|
|
registerShowPassword();
|
|
|
|
initCopySecret();
|
|
|
|
registerForms();
|
|
|
|
};
|
|
|
|
|
2021-07-20 12:44:23 +00:00
|
|
|
export default index;
|