2021-05-03 09:25:42 +01:00
|
|
|
import { CodeJar } from "codejar";
|
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 { getSecret } from "../../api/kv/getSecret";
|
2021-05-07 22:23:52 +01:00
|
|
|
import { makeElement } from "../../htmlUtils";
|
2021-05-08 03:03:34 +01:00
|
|
|
import { pageState } from "../../globalPageState";
|
|
|
|
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-08 03:03:34 +01:00
|
|
|
goBack(): void {
|
2021-04-20 22:49:33 +01:00
|
|
|
changePage("KEY_VALUE_SECRET");
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
2021-05-08 03:03:34 +01:00
|
|
|
render(): void {
|
2021-04-15 13:01:58 +01:00
|
|
|
setTitleElement(pageState);
|
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-12 16:01:04 +01:00
|
|
|
setPageContent(
|
|
|
|
makeElement({
|
|
|
|
tag: "div",
|
|
|
|
children: [
|
|
|
|
loadingText,
|
|
|
|
editor,
|
|
|
|
makeElement({
|
|
|
|
tag: "p",
|
|
|
|
id: "errorText",
|
|
|
|
class: ["uk-text-danger", "uk-margin-top"],
|
|
|
|
}),
|
|
|
|
saveButton,
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
2021-05-10 11:35:14 +01:00
|
|
|
void getSecret(
|
2021-05-05 21:56:25 +01:00
|
|
|
pageState.currentBaseMount,
|
|
|
|
pageState.currentMountType,
|
|
|
|
pageState.currentSecretPath,
|
|
|
|
pageState.currentSecret,
|
2021-05-12 16:01:04 +01:00
|
|
|
).then((secretInfo) => {
|
2021-04-15 13:01:58 +01:00
|
|
|
loadingText.remove();
|
|
|
|
|
2021-05-08 03:03:34 +01:00
|
|
|
const secretsJSON = JSON.stringify(sortedObjectMap(secretInfo), null, 4);
|
2021-04-15 13:01:58 +01:00
|
|
|
|
2021-05-12 16:01:04 +01:00
|
|
|
const jar = CodeJar(editor, () => {}, { tab: " ".repeat(4) });
|
2021-04-15 13:01:58 +01:00
|
|
|
jar.updateCode(secretsJSON);
|
|
|
|
saveButton.onclick = function () {
|
|
|
|
if (!verifyJSONString(jar.toString())) {
|
2021-04-20 23:02:18 +01:00
|
|
|
setErrorText(i18next.t("kv_sec_edit_invalid_json_err"));
|
2021-04-15 13:01:58 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-05-08 03:03:34 +01:00
|
|
|
|
2021-04-15 13:01:58 +01:00
|
|
|
createOrUpdateSecret(
|
|
|
|
pageState.currentBaseMount,
|
2021-05-08 03:03:34 +01:00
|
|
|
pageState.currentMountType,
|
2021-04-15 13:01:58 +01:00
|
|
|
pageState.currentSecretPath,
|
|
|
|
pageState.currentSecret,
|
2021-05-12 16:01:04 +01:00
|
|
|
JSON.parse(jar.toString()),
|
|
|
|
)
|
|
|
|
.then((_) => {
|
|
|
|
changePage("KEY_VALUE_SECRET");
|
|
|
|
return;
|
|
|
|
})
|
|
|
|
.catch((e: Error) => {
|
|
|
|
setErrorText(e.message);
|
|
|
|
});
|
2021-04-15 13:01:58 +01:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-08 03:03:34 +01:00
|
|
|
get titleSuffix(): string {
|
2021-04-20 23:02:18 +01:00
|
|
|
return i18next.t("kv_sec_edit_suffix");
|
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
|
|
|
}
|