2020-10-22 16:50:40 +00:00
|
|
|
import { serializeForm, _post, _get, _delete, addAttr, rmAttr } from "./modules/common.js";
|
|
|
|
import { BS5 } from "./modules/bs5.js";
|
|
|
|
import { BS4 } from "./modules/bs4.js";
|
|
|
|
|
|
|
|
interface formWindow extends Window {
|
|
|
|
usernameEnabled: boolean;
|
|
|
|
validationStrings: pwValStrings;
|
|
|
|
}
|
|
|
|
|
|
|
|
declare var window: formWindow;
|
|
|
|
|
2020-10-20 22:00:30 +00:00
|
|
|
interface pwValString {
|
|
|
|
singular: string;
|
|
|
|
plural: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface pwValStrings {
|
|
|
|
length, uppercase, lowercase, number, special: pwValString;
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultPwValStrings: pwValStrings = {
|
|
|
|
length: {
|
|
|
|
singular: "Must have at least {n} character",
|
|
|
|
plural: "Must have a least {n} characters"
|
|
|
|
},
|
|
|
|
uppercase: {
|
|
|
|
singular: "Must have at least {n} uppercase character",
|
|
|
|
plural: "Must have at least {n} uppercase characters"
|
|
|
|
},
|
|
|
|
lowercase: {
|
|
|
|
singular: "Must have at least {n} lowercase character",
|
|
|
|
plural: "Must have at least {n} lowercase characters"
|
|
|
|
},
|
|
|
|
number: {
|
|
|
|
singular: "Must have at least {n} number",
|
|
|
|
plural: "Must have at least {n} numbers"
|
|
|
|
},
|
|
|
|
special: {
|
|
|
|
singular: "Must have at least {n} special character",
|
|
|
|
plural: "Must have at least {n} special characters"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 22:33:32 +00:00
|
|
|
const toggleSpinner = (): void => {
|
|
|
|
const submitButton = document.getElementById('submitButton') as HTMLButtonElement;
|
|
|
|
if (document.getElementById('createAccountSpinner')) {
|
|
|
|
submitButton.innerHTML = `<span>Create Account</span>`;
|
|
|
|
submitButton.disabled = false;
|
|
|
|
} else {
|
|
|
|
submitButton.innerHTML = `
|
|
|
|
<span id="createAccountSpinner" class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>Creating...
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-22 16:50:40 +00:00
|
|
|
for (let key in window.validationStrings) {
|
|
|
|
if (window.validationStrings[key].singular == "" || !(window.validationStrings[key].plural.includes("{n}"))) {
|
|
|
|
window.validationStrings[key].singular = defaultPwValStrings[key].singular;
|
2020-10-20 22:00:30 +00:00
|
|
|
}
|
2020-10-22 16:50:40 +00:00
|
|
|
if (window.validationStrings[key].plural == "" || !(window.validationStrings[key].plural.includes("{n}"))) {
|
|
|
|
window.validationStrings[key].plural = defaultPwValStrings[key].plural;
|
2020-10-20 22:00:30 +00:00
|
|
|
}
|
|
|
|
let el = document.getElementById(key) as HTMLUListElement;
|
|
|
|
if (el) {
|
|
|
|
const min: number = +el.getAttribute("min");
|
|
|
|
let text = "";
|
|
|
|
if (min == 1) {
|
2020-10-22 16:50:40 +00:00
|
|
|
text = window.validationStrings[key].singular.replace("{n}", "1");
|
2020-10-20 22:00:30 +00:00
|
|
|
} else {
|
2020-10-22 16:50:40 +00:00
|
|
|
text = window.validationStrings[key].plural.replace("{n}", min.toString());
|
2020-10-20 22:00:30 +00:00
|
|
|
}
|
|
|
|
(document.getElementById(key).children[0] as HTMLDivElement).textContent = text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 16:50:40 +00:00
|
|
|
window.BS = window.bs5 ? new BS5 : new BS4;
|
|
|
|
var successBox: BSModal = window.BS.newModal('successBox');;
|
2020-10-20 22:00:30 +00:00
|
|
|
|
|
|
|
var code = window.location.href.split('/').pop();
|
|
|
|
|
|
|
|
(document.getElementById('accountForm') as HTMLFormElement).addEventListener('submit', (event: any): boolean => {
|
|
|
|
event.preventDefault();
|
|
|
|
const el = document.getElementById('errorMessage');
|
|
|
|
if (el) {
|
|
|
|
el.remove();
|
|
|
|
}
|
|
|
|
toggleSpinner();
|
|
|
|
let send: Object = serializeForm('accountForm');
|
|
|
|
send["code"] = code;
|
2020-10-22 16:50:40 +00:00
|
|
|
if (!window.usernameEnabled) {
|
2020-10-20 22:00:30 +00:00
|
|
|
send["email"] = send["username"];
|
|
|
|
}
|
|
|
|
_post("/newUser", send, function (): void {
|
|
|
|
if (this.readyState == 4) {
|
|
|
|
toggleSpinner();
|
|
|
|
let data: Object = this.response;
|
|
|
|
const errorGiven = ("error" in data)
|
|
|
|
if (errorGiven || data["success"] === false) {
|
|
|
|
let errorMessage = "Unknown Error";
|
|
|
|
if (errorGiven && errorGiven != true) {
|
|
|
|
errorMessage = data["error"];
|
|
|
|
}
|
|
|
|
document.getElementById('errorBox').innerHTML += `
|
|
|
|
<button id="errorMessage" class="btn btn-outline-danger" disabled>${errorMessage}</button>
|
|
|
|
`;
|
|
|
|
} else {
|
|
|
|
let valid = true;
|
|
|
|
for (let key in data) {
|
|
|
|
if (data.hasOwnProperty(key)) {
|
|
|
|
const criterion = document.getElementById(key);
|
|
|
|
if (criterion) {
|
|
|
|
if (data[key] === false) {
|
|
|
|
valid = false;
|
|
|
|
addAttr(criterion, "list-group-item-danger");
|
|
|
|
rmAttr(criterion, "list-group-item-success");
|
|
|
|
} else {
|
|
|
|
addAttr(criterion, "list-group-item-success");
|
|
|
|
rmAttr(criterion, "list-group-item-danger");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (valid) {
|
|
|
|
successBox.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-20 22:33:32 +00:00
|
|
|
}, true);
|
2020-10-20 22:00:30 +00:00
|
|
|
return false;
|
|
|
|
});
|