2021-05-03 09:25:42 +01:00
|
|
|
import { CodeJar } from "codejar";
|
2021-04-17 10:45:42 +01:00
|
|
|
import { Page } from "../../types/Page.js";
|
2021-05-03 09:25:42 +01:00
|
|
|
import { changePage, setErrorText, setPageContent, setTitleElement } from "../../pageUtils.js";
|
2021-05-07 11:53:26 +01:00
|
|
|
import { createOrUpdateSecret } from "../../api/createOrUpdateSecret.js";
|
|
|
|
import { getSecret } from "../../api/getSecret.js";
|
2021-04-17 10:45:42 +01:00
|
|
|
import { makeElement } from "../../htmlUtils.js";
|
2021-05-07 11:07:03 +01:00
|
|
|
import { pageState } from "../../globalPageState.js";
|
2021-05-03 09:25:42 +01:00
|
|
|
import { verifyJSONString } from "../../utils.js";
|
2021-04-20 23:02:18 +01:00
|
|
|
import i18next from 'i18next';
|
|
|
|
|
2021-04-20 22:49:33 +01:00
|
|
|
export class KeyValueSecretEditPage extends Page {
|
2021-04-15 13:01:58 +01:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
goBack() {
|
2021-04-20 22:49:33 +01:00
|
|
|
changePage("KEY_VALUE_SECRET");
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
|
|
|
render() {
|
|
|
|
setTitleElement(pageState);
|
2021-04-20 23:02:18 +01:00
|
|
|
let loadingText = makeElement({
|
|
|
|
tag: "p",
|
|
|
|
text: i18next.t("kv_sec_edit_loading")
|
|
|
|
});
|
2021-04-15 13:01:58 +01:00
|
|
|
let editor = makeElement({
|
|
|
|
tag: "div",
|
|
|
|
class: ["editor", "language-json"]
|
|
|
|
});
|
|
|
|
let saveButton = makeElement({
|
|
|
|
tag: "button",
|
|
|
|
class: ["uk-button", "uk-button-primary"],
|
2021-04-20 23:02:18 +01:00
|
|
|
text: i18next.t("kv_sec_edit_btn")
|
2021-04-15 13:01:58 +01:00
|
|
|
});
|
|
|
|
setPageContent(makeElement({
|
|
|
|
tag: "div",
|
|
|
|
children: [
|
|
|
|
loadingText,
|
|
|
|
editor,
|
|
|
|
makeElement({
|
|
|
|
tag: "p",
|
|
|
|
id: "errorText",
|
|
|
|
class: ["uk-text-danger", "uk-margin-top"]
|
|
|
|
}),
|
|
|
|
saveButton
|
|
|
|
]
|
|
|
|
}));
|
2021-05-05 21:56:25 +01:00
|
|
|
getSecret(
|
|
|
|
pageState.currentBaseMount,
|
|
|
|
pageState.currentMountType,
|
|
|
|
pageState.currentSecretPath,
|
|
|
|
pageState.currentSecret,
|
|
|
|
).then(secretInfo => {
|
2021-04-15 13:01:58 +01:00
|
|
|
loadingText.remove();
|
|
|
|
|
|
|
|
const secretsJSON = JSON.stringify(Object.fromEntries(new Map(Object.entries(secretInfo).sort())), null, 4);
|
|
|
|
|
|
|
|
let jar = CodeJar(editor, () => { }, { tab: ' '.repeat(4) });
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
createOrUpdateSecret(
|
|
|
|
pageState.currentBaseMount,
|
|
|
|
pageState.currentSecretPath,
|
|
|
|
pageState.currentSecret,
|
|
|
|
JSON.parse(jar.toString())
|
2021-04-18 10:42:57 +01:00
|
|
|
).then(_ => {
|
2021-04-20 22:49:33 +01:00
|
|
|
changePage("KEY_VALUE_SECRET");
|
2021-04-15 13:01:58 +01:00
|
|
|
return;
|
|
|
|
}).catch(e => {
|
|
|
|
setErrorText(e.message);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-20 20:32:57 +01:00
|
|
|
get titleSuffix() {
|
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
|
|
|
|
|
|
|
get name() {
|
2021-04-20 23:02:18 +01:00
|
|
|
return i18next.t("kv_sec_edit_title");
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
|
|
|
}
|