2021-05-08 00:38:02 +01:00
|
|
|
import { Margin } from "../../elements/Margin";
|
|
|
|
import { MarginInline } from "../../elements/MarginInline";
|
2021-05-07 23:33:58 +01:00
|
|
|
import { Page } from "../../types/Page";
|
2021-05-09 11:18:18 +01:00
|
|
|
import { addNewTOTP } from "../../api/totp/addNewTOTP";
|
2021-05-07 23:21:38 +01:00
|
|
|
import { changePage, setErrorText, setPageContent, setTitleElement } from "../../pageUtils";
|
2021-05-07 22:23:52 +01:00
|
|
|
import { makeElement } from "../../htmlUtils";
|
2021-05-08 02:56:08 +01:00
|
|
|
import { pageState } from "../../globalPageState";
|
2021-05-12 16:01:04 +01:00
|
|
|
import i18next from "i18next";
|
2021-04-15 13:01:58 +01:00
|
|
|
|
2021-05-08 02:56:08 +01:00
|
|
|
function replaceAll(str: string, replace: string, replaceWith: string): string {
|
2021-05-12 16:01:04 +01:00
|
|
|
return str.replace(new RegExp(replace, "g"), replaceWith);
|
2021-05-08 02:56:08 +01:00
|
|
|
}
|
|
|
|
function removeDashSpaces(str: string): string {
|
|
|
|
str = replaceAll(str, "-", "");
|
|
|
|
str = replaceAll(str, " ", "");
|
|
|
|
return str;
|
|
|
|
}
|
2021-05-07 11:53:26 +01:00
|
|
|
|
2021-04-15 13:01:58 +01:00
|
|
|
export class NewTOTPPage extends Page {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
2021-05-12 17:37:09 +01:00
|
|
|
async goBack(): Promise<void> {
|
|
|
|
await changePage("TOTP");
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
2021-05-12 17:37:09 +01:00
|
|
|
async render(): Promise<void> {
|
2021-04-15 13:01:58 +01:00
|
|
|
setTitleElement(pageState);
|
|
|
|
|
2021-05-08 02:56:08 +01:00
|
|
|
const totpForm = makeElement({
|
2021-04-15 13:01:58 +01:00
|
|
|
tag: "form",
|
|
|
|
children: [
|
2021-05-12 16:01:04 +01:00
|
|
|
Margin(
|
|
|
|
makeElement({
|
|
|
|
tag: "input",
|
|
|
|
class: ["uk-input", "uk-form-width-medium"],
|
|
|
|
attributes: {
|
|
|
|
required: "true",
|
|
|
|
type: "text",
|
|
|
|
placeholder: i18next.t("totp_new_name_text"),
|
|
|
|
name: "name",
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
),
|
2021-04-15 13:01:58 +01:00
|
|
|
makeElement({
|
|
|
|
tag: "p",
|
2021-05-12 16:01:04 +01:00
|
|
|
text: i18next.t("totp_new_info"),
|
2021-04-15 13:01:58 +01:00
|
|
|
}),
|
2021-05-12 16:01:04 +01:00
|
|
|
Margin(
|
|
|
|
makeElement({
|
|
|
|
tag: "input",
|
|
|
|
class: ["uk-input", "uk-form-width-medium"],
|
|
|
|
attributes: {
|
|
|
|
type: "text",
|
|
|
|
placeholder: i18next.t("totp_new_uri_input"),
|
|
|
|
name: "uri",
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
Margin(
|
|
|
|
makeElement({
|
|
|
|
tag: "input",
|
|
|
|
class: ["uk-input", "uk-form-width-medium"],
|
|
|
|
attributes: {
|
|
|
|
type: "text",
|
|
|
|
placeholder: i18next.t("totp_new_key_input"),
|
|
|
|
name: "key",
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
),
|
2021-04-15 13:01:58 +01:00
|
|
|
makeElement({
|
|
|
|
tag: "p",
|
|
|
|
id: "errorText",
|
2021-05-12 16:01:04 +01:00
|
|
|
class: "uk-text-danger",
|
2021-04-15 13:01:58 +01:00
|
|
|
}),
|
2021-05-12 16:01:04 +01:00
|
|
|
MarginInline(
|
|
|
|
makeElement({
|
|
|
|
tag: "button",
|
|
|
|
class: ["uk-button", "uk-button-primary"],
|
|
|
|
text: i18next.t("totp_new_add_btn"),
|
|
|
|
attributes: {
|
|
|
|
type: "submit",
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
],
|
2021-05-08 02:56:08 +01:00
|
|
|
}) as HTMLFormElement;
|
2021-04-15 13:01:58 +01:00
|
|
|
setPageContent(totpForm);
|
|
|
|
|
|
|
|
totpForm.addEventListener("submit", function (e) {
|
|
|
|
e.preventDefault();
|
2021-05-08 02:56:08 +01:00
|
|
|
const formData = new FormData(totpForm);
|
|
|
|
const parms = {
|
|
|
|
url: formData.get("uri") as string,
|
|
|
|
key: removeDashSpaces(formData.get("key") as string).toUpperCase(),
|
|
|
|
name: formData.get("name") as string,
|
2021-05-12 16:01:04 +01:00
|
|
|
generate: false,
|
2021-04-15 13:01:58 +01:00
|
|
|
};
|
2021-05-12 16:01:04 +01:00
|
|
|
addNewTOTP(pageState.currentBaseMount, parms)
|
|
|
|
.then((_) => {
|
2021-05-12 17:37:09 +01:00
|
|
|
void changePage("TOTP");
|
2021-05-12 16:01:04 +01:00
|
|
|
})
|
|
|
|
.catch((e: Error) => {
|
|
|
|
setErrorText(`API Error: ${e.message}`);
|
|
|
|
});
|
2021-04-15 13:01:58 +01:00
|
|
|
});
|
|
|
|
}
|
2021-04-17 11:06:34 +01:00
|
|
|
|
2021-05-08 02:56:08 +01:00
|
|
|
get titleSuffix(): string {
|
2021-04-20 23:23:17 +01:00
|
|
|
return i18next.t("totp_new_suffix");
|
2021-04-17 11:06:34 +01:00
|
|
|
}
|
|
|
|
|
2021-05-08 02:56:08 +01:00
|
|
|
get name(): string {
|
2021-04-20 23:23:17 +01:00
|
|
|
return i18next.t("totp_new_title");
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
2021-05-12 16:01:04 +01:00
|
|
|
}
|