1
0
Fork 0
VaultUI/src/pages/TOTP/NewTOTP.ts

119 lines
3.2 KiB
TypeScript
Raw Normal View History

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";
import { makeElement } from "../../htmlUtils";
2021-05-08 02:56:08 +01:00
import { pageState } from "../../globalPageState";
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 {
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: [
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",
text: i18next.t("totp_new_info"),
2021-04-15 13:01:58 +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",
class: "uk-text-danger",
2021-04-15 13:01:58 +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);
2021-05-12 18:26:32 +01:00
totpForm.addEventListener("submit", async function (e) {
2021-04-15 13:01:58 +01:00
e.preventDefault();
2021-05-12 18:26:32 +01:00
2021-05-08 02:56:08 +01:00
const formData = new FormData(totpForm);
2021-05-12 18:26:32 +01:00
2021-05-08 02:56:08 +01:00
const parms = {
url: formData.get("uri") as string,
key: removeDashSpaces(formData.get("key") as string).toUpperCase(),
name: formData.get("name") as string,
generate: false,
2021-04-15 13:01:58 +01:00
};
2021-05-12 18:26:32 +01:00
try {
await addNewTOTP(pageState.currentBaseMount, parms);
await changePage("TOTP");
} catch (e: unknown) {
const error = e as Error;
setErrorText(`API Error: ${error.message}`);
}
2021-04-15 13:01:58 +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-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
}
}