1
0
Fork 0
VaultUI/src/pages/KeyValue/KeyValueNew.ts

92 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-05-07 23:33:58 +01:00
import { Page } from "../../types/Page";
2021-05-07 23:21:38 +01:00
import { changePage, setErrorText, setPageContent, setTitleElement } from "../../pageUtils";
2021-05-09 11:18:18 +01:00
import { createOrUpdateSecret } from "../../api/kv/createOrUpdateSecret";
import { makeElement } from "../../htmlUtils";
2021-05-08 03:10:21 +01:00
import { pageState } from "../../globalPageState";
2021-04-20 20:38:30 +01:00
import i18next from 'i18next';
2021-04-15 13:01:58 +01:00
export class KeyValueNewPage extends Page {
constructor() {
super();
}
2021-05-08 03:10:21 +01:00
goBack(): void {
2021-04-17 11:24:43 +01:00
changePage("KEY_VALUE_VIEW");
2021-04-15 13:01:58 +01:00
}
2021-05-08 03:10:21 +01:00
addKVNewForm: HTMLFormElement;
render(): void {
2021-04-15 13:01:58 +01:00
setTitleElement(pageState);
this.addKVNewForm = makeElement({
tag: "form",
id: "addKVNewForm",
children: [
makeElement({
tag: "div",
class: "uk-margin",
children: makeElement({
tag: "input",
class: ["uk-input", "uk-form-width-medium"],
attributes: {
2021-05-10 11:35:14 +01:00
required: "true",
2021-04-15 13:01:58 +01:00
type: "text",
2021-04-20 20:38:30 +01:00
placeholder: i18next.t("kv_new_path"),
2021-04-15 13:01:58 +01:00
name: "path"
}
})
}),
makeElement({
tag: "p",
id: "errorText",
class: "uk-text-danger"
}),
makeElement({
tag: "button",
class: ["uk-button", "uk-button-primary"],
2021-04-20 20:38:30 +01:00
text: i18next.t("kv_new_create_btn"),
2021-04-15 13:01:58 +01:00
attributes: {
type: "submit",
}
})
]
2021-05-08 03:10:21 +01:00
}) as HTMLFormElement;
2021-04-15 13:01:58 +01:00
setPageContent(this.addKVNewForm);
2021-05-10 11:35:14 +01:00
this.addKVNewForm.addEventListener("submit", function (e: Event) {
2021-04-15 13:01:58 +01:00
e.preventDefault();
2021-05-10 11:35:14 +01:00
(this as KeyValueNewPage).newKVSecretHandleForm();
}.bind(this));
2021-04-15 13:01:58 +01:00
}
2021-05-08 03:10:21 +01:00
newKVSecretHandleForm(): void {
const formData = new FormData(this.addKVNewForm);
const path = formData.get("path") as string;
2021-04-15 13:01:58 +01:00
let keyData = {};
if (["kv-v1", "cubbyhole"].includes(pageState.currentMountType)) {
keyData = { "key": "value" };
}
createOrUpdateSecret(
pageState.currentBaseMount,
pageState.currentMountType,
pageState.currentSecretPath,
2021-05-05 17:09:38 +01:00
path,
2021-04-15 13:01:58 +01:00
keyData
).then(_ => {
2021-04-17 11:24:43 +01:00
changePage("KEY_VALUE_VIEW");
2021-04-15 13:01:58 +01:00
return;
2021-05-10 11:35:14 +01:00
}).catch((e: Error) => {
2021-04-15 13:01:58 +01:00
setErrorText(e.message);
});
}
2021-05-08 03:10:21 +01:00
get titleSuffix(): string {
2021-04-20 20:38:30 +01:00
return i18next.t("kv_new_suffix");
}
2021-04-15 13:01:58 +01:00
2021-05-08 03:10:21 +01:00
get name(): string {
2021-04-20 20:38:30 +01:00
return i18next.t("kv_new_title");
2021-04-15 13:01:58 +01:00
}
}