2021-05-03 09:25:42 +01:00
|
|
|
import { CodeJar } from "codejar";
|
2021-05-18 11:10:47 +01:00
|
|
|
import { Page } from "../../../types/Page";
|
2021-05-19 10:27:00 +01:00
|
|
|
import { SecretTitleElement } from "../SecretTitleElement";
|
2021-05-18 11:10:47 +01:00
|
|
|
import { createOrUpdateSecret } from "../../../api/kv/createOrUpdateSecret";
|
|
|
|
import { getSecret } from "../../../api/kv/getSecret";
|
2021-05-16 18:17:12 +01:00
|
|
|
import { makeElement } from "z-makeelement";
|
2021-05-24 10:14:01 +01:00
|
|
|
import { render } from "preact";
|
2021-05-18 11:10:47 +01:00
|
|
|
import { setErrorText } from "../../../pageUtils";
|
|
|
|
import { sortedObjectMap, verifyJSONString } from "../../../utils";
|
2021-05-12 16:01:04 +01:00
|
|
|
import i18next from "i18next";
|
2021-04-20 23:02:18 +01:00
|
|
|
|
2021-04-20 22:49:33 +01:00
|
|
|
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> {
|
2021-05-15 10:54:39 +01:00
|
|
|
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({
|
2021-04-20 23:02:18 +01:00
|
|
|
tag: "p",
|
2021-05-12 16:01:04 +01:00
|
|
|
text: i18next.t("kv_sec_edit_loading"),
|
2021-04-20 23:02:18 +01:00
|
|
|
});
|
2021-05-08 03:03:34 +01:00
|
|
|
const editor = makeElement({
|
2021-04-15 13:01:58 +01:00
|
|
|
tag: "div",
|
2021-05-12 16:01:04 +01:00
|
|
|
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"],
|
2021-05-12 16:01:04 +01:00
|
|
|
text: i18next.t("kv_sec_edit_btn"),
|
2021-04-15 13:01:58 +01:00
|
|
|
});
|
2021-05-15 10:54:39 +01:00
|
|
|
await this.router.setPageContent(
|
2021-05-12 16:01:04 +01:00
|
|
|
makeElement({
|
|
|
|
tag: "div",
|
|
|
|
children: [
|
|
|
|
loadingText,
|
|
|
|
editor,
|
|
|
|
makeElement({
|
|
|
|
tag: "p",
|
|
|
|
id: "errorText",
|
|
|
|
class: ["uk-text-danger", "uk-margin-top"],
|
|
|
|
}),
|
|
|
|
saveButton,
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
2021-05-15 10:54:39 +01:00
|
|
|
const secretInfo = await getSecret(
|
2021-05-20 14:12:12 +01:00
|
|
|
this.state.baseMount,
|
|
|
|
this.state.secretMountType,
|
|
|
|
this.state.secretPath,
|
|
|
|
this.state.secretItem,
|
2021-05-15 10:54:39 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
loadingText.remove();
|
2021-04-15 13:01:58 +01:00
|
|
|
|
2021-05-15 10:54:39 +01:00
|
|
|
const secretsJSON = JSON.stringify(sortedObjectMap(secretInfo), null, 4);
|
2021-04-15 13:01:58 +01:00
|
|
|
|
2021-05-15 10:54:39 +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
|
|
|
|
2021-05-15 10:54:39 +01:00
|
|
|
try {
|
|
|
|
await createOrUpdateSecret(
|
2021-05-20 14:12:12 +01:00
|
|
|
this.state.baseMount,
|
|
|
|
this.state.secretMountType,
|
|
|
|
this.state.secretPath,
|
|
|
|
this.state.secretItem,
|
2021-05-12 16:01:04 +01:00
|
|
|
JSON.parse(jar.toString()),
|
2021-05-15 10:54:39 +01:00
|
|
|
);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-05-24 10:14:01 +01:00
|
|
|
async renderPageTitle(): Promise<void> {
|
|
|
|
render(
|
|
|
|
<SecretTitleElement router={this.router} suffix={i18next.t("kv_sec_edit_suffix")} />,
|
|
|
|
this.router.pageTitleElement,
|
|
|
|
);
|
2021-04-17 11:06:34 +01:00
|
|
|
}
|
2021-04-15 13:01:58 +01:00
|
|
|
|
2021-05-08 03:03:34 +01:00
|
|
|
get name(): string {
|
2021-04-20 23:02:18 +01:00
|
|
|
return i18next.t("kv_sec_edit_title");
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
2021-05-12 16:01:04 +01:00
|
|
|
}
|