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

105 lines
2.9 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-07 11:53:26 +01:00
import { addNewTOTP } from "../../api/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";
2021-04-20 23:23:17 +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 {
return str.replace(new RegExp(replace, 'g'), replaceWith);
}
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-08 02:56:08 +01:00
goBack(): void {
2021-04-17 11:24:43 +01:00
changePage("TOTP");
2021-04-15 13:01:58 +01:00
}
2021-05-08 02:56:08 +01:00
render(): 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",
2021-04-20 23:23:17 +01:00
placeholder: i18next.t("totp_new_name_text"),
2021-04-15 13:01:58 +01:00
name: "name"
}
})),
makeElement({
tag: "p",
2021-04-20 23:23:17 +01:00
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",
2021-04-20 23:23:17 +01:00
placeholder: i18next.t("totp_new_uri_input"),
2021-04-15 13:01:58 +01:00
name: "uri"
}
})),
Margin(makeElement({
tag: "input",
class: ["uk-input", "uk-form-width-medium"],
attributes: {
type: "text",
2021-04-20 23:23:17 +01:00
placeholder: i18next.t("totp_new_key_input"),
2021-04-15 13:01:58 +01:00
name: "key"
}
})),
makeElement({
tag: "p",
id: "errorText",
class: "uk-text-danger"
}),
MarginInline(makeElement({
tag: "button",
class: ["uk-button", "uk-button-primary"],
2021-04-20 23:23:17 +01:00
text: i18next.t("totp_new_add_btn"),
2021-04-15 13:01:58 +01:00
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-04-15 13:01:58 +01:00
generate: false
};
2021-04-18 10:42:57 +01:00
addNewTOTP(pageState.currentBaseMount, parms).then(_ => {
2021-04-17 11:24:43 +01:00
changePage("TOTP");
2021-04-15 13:01:58 +01:00
}).catch(e => {
setErrorText(`API Error: ${e.message}`);
});
});
}
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
}
}