1
0
Fork 0
VaultUI/src/pages/Secrets/KeyValue/KeyValueSecretsEdit.ts

90 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-05-03 09:25:42 +01:00
import { CodeJar } from "codejar";
import { Page } from "../../../types/Page";
2021-05-19 10:27:00 +01:00
import { SecretTitleElement } from "../SecretTitleElement";
import { createOrUpdateSecret } from "../../../api/kv/createOrUpdateSecret";
import { getSecret } from "../../../api/kv/getSecret";
import { makeElement } from "z-makeelement";
import { setErrorText } from "../../../pageUtils";
import { sortedObjectMap, verifyJSONString } from "../../../utils";
import i18next from "i18next";
export class KeyValueSecretEditPage extends Page {
2021-04-15 13:01:58 +01:00
constructor() {
super();
}
2021-05-12 17:37:09 +01:00
async goBack(): Promise<void> {
await this.router.changePage("KEY_VALUE_SECRET");
2021-04-15 13:01:58 +01:00
}
2021-05-12 17:37:09 +01:00
async render(): Promise<void> {
2021-05-08 03:03:34 +01:00
const loadingText = makeElement({
tag: "p",
text: i18next.t("kv_sec_edit_loading"),
});
2021-05-08 03:03:34 +01:00
const editor = makeElement({
2021-04-15 13:01:58 +01:00
tag: "div",
class: ["editor", "language-json"],
2021-04-15 13:01:58 +01:00
});
2021-05-08 03:03:34 +01:00
const saveButton = makeElement({
2021-04-15 13:01:58 +01:00
tag: "button",
class: ["uk-button", "uk-button-primary"],
text: i18next.t("kv_sec_edit_btn"),
2021-04-15 13:01:58 +01:00
});
await this.router.setPageContent(
makeElement({
tag: "div",
children: [
loadingText,
editor,
makeElement({
tag: "p",
id: "errorText",
class: ["uk-text-danger", "uk-margin-top"],
}),
saveButton,
],
}),
);
const secretInfo = await getSecret(
this.state.currentBaseMount,
this.state.currentMountType,
this.state.currentSecretPath,
this.state.currentSecret,
);
loadingText.remove();
2021-04-15 13:01:58 +01:00
const secretsJSON = JSON.stringify(sortedObjectMap(secretInfo), null, 4);
2021-04-15 13:01:58 +01:00
const jar = CodeJar(editor, () => {}, { tab: " ".repeat(4) });
jar.updateCode(secretsJSON);
saveButton.onclick = async () => {
if (!verifyJSONString(jar.toString())) {
setErrorText(i18next.t("kv_sec_edit_invalid_json_err"));
return;
}
2021-05-08 03:03:34 +01:00
try {
await createOrUpdateSecret(
this.state.currentBaseMount,
this.state.currentMountType,
this.state.currentSecretPath,
this.state.currentSecret,
JSON.parse(jar.toString()),
);
await this.router.changePage("KEY_VALUE_SECRET");
} catch (e: unknown) {
const error = e as Error;
setErrorText(error.message);
}
};
2021-04-15 13:01:58 +01:00
}
async getPageTitle(): Promise<Element | string> {
return await SecretTitleElement(this.router, i18next.t("kv_sec_edit_suffix"));
}
2021-04-15 13:01:58 +01:00
2021-05-08 03:03:34 +01:00
get name(): string {
return i18next.t("kv_sec_edit_title");
2021-04-15 13:01:58 +01:00
}
}