diff --git a/src/allPages.ts b/src/allPages.ts index 7ba26e6..8e49260 100644 --- a/src/allPages.ts +++ b/src/allPages.ts @@ -23,6 +23,7 @@ import { NewTransitKeyPage } from "./pages/Secrets/Transit/NewTransitKey"; import { Page } from "./types/Page"; import { PoliciesHomePage } from "./pages/Policies/PoliciesHome"; import { PolicyDeletePage } from "./pages/Policies/PolicyDelete"; +import { PolicyEditPage } from "./pages/Policies/PolicyEdit"; import { PolicyNewPage } from "./pages/Policies/PolicyNew"; import { PolicyViewPage } from "./pages/Policies/PolicyView"; import { PwGenPage } from "./pages/PwGen"; @@ -61,6 +62,7 @@ export const allPages: pagesList = { POLICIES_HOME: new PoliciesHomePage(), POLICY_VIEW: new PolicyViewPage(), POLICY_NEW: new PolicyNewPage(), + POLICY_EDIT: new PolicyEditPage(), POLICY_DELETE: new PolicyDeletePage(), ACCESS_HOME: new AccessHomePage(), diff --git a/src/pages/Policies/PolicyEdit.tsx b/src/pages/Policies/PolicyEdit.tsx new file mode 100644 index 0000000..8e3c784 --- /dev/null +++ b/src/pages/Policies/PolicyEdit.tsx @@ -0,0 +1,115 @@ +import { CodeEditor } from "../../elements/CodeEditor"; +import { Component, JSX, render } from "preact"; +import { Margin } from "../../elements/Margin"; +import { MarginInline } from "../../elements/MarginInline"; +import { Page } from "../../types/Page"; +import { createOrUpdatePolicy } from "../../api/sys/policies/createOrUpdatePolicy"; +import { getPolicy } from "../../api/sys/policies/getPolicy"; +import { setErrorText } from "../../pageUtils"; +import i18next from "i18next"; + +type PolicyEditorProps = { + page: Page; + policy_name: string; +}; + +type PolicyEditorState = + | { + dataLoaded: false; + } + | { + dataLoaded: true; + policyData: string; + code: string; + }; + +export class PolicyEditor extends Component { + constructor() { + super(); + this.state = { + dataLoaded: false, + }; + } + + async editorSave(): Promise { + if (!this.state.dataLoaded) return; + + try { + await createOrUpdatePolicy(this.props.policy_name, this.state.code); + await this.props.page.router.changePage("POLICY_VIEW"); + } catch (e: unknown) { + const error = e as Error; + setErrorText(error.message); + } + } + + onCodeUpdate(code: string): void { + this.setState({ + code: code, + }); + } + + async loadData(): Promise { + const policyData = await getPolicy(this.props.policy_name); + this.setState({ + dataLoaded: true, + policyData: policyData, + code: policyData, + }); + return; + } + + componentDidMount(): void { + if (!this.state.dataLoaded) { + void this.loadData(); + } + } + + render(): JSX.Element { + if (!this.state.dataLoaded) { + return

{i18next.t("content_loading")}

; + } + + return ( +
+

+ + this.onCodeUpdate(code)} + /> + + + + +

+ ); + } +} + +export class PolicyEditPage extends Page { + constructor() { + super(); + } + async goBack(): Promise { + await this.router.changePage("POLICY_VIEW"); + } + async render(): Promise { + render( +
+ +
, + this.router.pageContentElement, + ); + } + + get name(): string { + return i18next.t("policy_edit_title", { + policy: this.state.policyItem, + }); + } +} diff --git a/src/pages/Policies/PolicyView.tsx b/src/pages/Policies/PolicyView.tsx index d0d1f19..1062404 100644 --- a/src/pages/Policies/PolicyView.tsx +++ b/src/pages/Policies/PolicyView.tsx @@ -2,7 +2,7 @@ import { CodeBlock } from "../../elements/CodeBlock"; import { Margin } from "../../elements/Margin"; import { Page } from "../../types/Page"; import { getPolicy } from "../../api/sys/policies/getPolicy"; -import { notImplemented, prePageChecks } from "../../pageUtils"; +import { prePageChecks } from "../../pageUtils"; import { render } from "preact"; import i18next from "i18next"; @@ -22,7 +22,12 @@ export class PolicyViewPage extends Page { render(

- {this.state.policyItem !== "default" && ( diff --git a/src/pages/Secrets/KeyValue/KeyValueSecretsEdit.tsx b/src/pages/Secrets/KeyValue/KeyValueSecretsEdit.tsx index 984a6eb..e1a4f0d 100644 --- a/src/pages/Secrets/KeyValue/KeyValueSecretsEdit.tsx +++ b/src/pages/Secrets/KeyValue/KeyValueSecretsEdit.tsx @@ -84,7 +84,7 @@ export class KVEditor extends Component { render(): JSX.Element { if (!this.state.dataLoaded) { - return

{i18next.t("kv_sec_edit_loading")}

; + return

{i18next.t("content_loading")}

; } return ( diff --git a/src/translations/en.js b/src/translations/en.js index 4221393..38aa15e 100644 --- a/src/translations/en.js +++ b/src/translations/en.js @@ -298,15 +298,18 @@ module.exports = { policy_view_edit_btn: "Edit", policy_view_delete_btn: "Delete", - // Policy Delete - policy_delete_title: "Delete Policy ({{policy}})", - policy_delete_text: - "Are you sure you want to delete this policy? It can't be reversed and there is a chance that all permissions will break.", - policy_delete_btn: "Delete Policy", - // Policy New policy_new_title: "Create New Policy", policy_new_name_placeholder: "Policy Name", policy_new_create_btn: "Create", policy_new_already_exists: "This policy already exists.", + + policy_edit_title: "Edit Policy ({{policy}})", + policy_edit_edit_btn: "Edit", + + // Policy Delete + policy_delete_title: "Delete Policy ({{policy}})", + policy_delete_text: + "Are you sure you want to delete this policy? It can't be reversed and there is a chance that all permissions will break.", + policy_delete_btn: "Delete Policy", };