From 0695adec71923bc7e978bd7086cae81abb639f74 Mon Sep 17 00:00:00 2001 From: ChaotiCryptidz Date: Fri, 21 Jan 2022 21:56:09 +0000 Subject: [PATCH] remove setErrorText and replace it with proper error text & notifs --- src/pageUtils.ts | 20 ------ src/ui/elements/ErrorMessage.tsx | 63 ++++++++++++++++ src/ui/pages/Access/AccessHome.tsx | 8 +-- src/ui/pages/Access/Auth/AuthHome.tsx | 13 ++-- src/ui/pages/Access/Auth/AuthViewConfig.tsx | 15 +++- .../Auth/userpass/UserPassUserDelete.tsx | 20 +++++- .../Access/Auth/userpass/UserPassUserEdit.tsx | 25 +++++-- .../Access/Auth/userpass/UserPassUserNew.tsx | 14 ++-- .../Access/Auth/userpass/UserPassUserView.tsx | 11 ++- .../Auth/userpass/UserPassUsersList.tsx | 11 ++- src/ui/pages/Home.tsx | 18 ++++- src/ui/pages/Login.tsx | 2 +- src/ui/pages/Login_Token.tsx | 25 +++++-- src/ui/pages/Login_Username.tsx | 16 ++++- src/ui/pages/Me.tsx | 28 +++++--- src/ui/pages/Policies/PoliciesHome.tsx | 22 +++--- src/ui/pages/Policies/PolicyDelete.tsx | 33 ++++++--- src/ui/pages/Policies/PolicyEdit.tsx | 66 +++++++++-------- src/ui/pages/Policies/PolicyNew.tsx | 57 +++++++++------ src/ui/pages/Policies/PolicyView.tsx | 16 +++-- src/ui/pages/PwGen.tsx | 1 + src/ui/pages/Secrets/DeleteSecretsEngine.tsx | 13 ++-- .../pages/Secrets/KeyValue/KeyValueDelete.tsx | 32 +++++++-- .../pages/Secrets/KeyValue/KeyValueEdit.tsx | 72 +++++++++++-------- .../pages/Secrets/KeyValue/KeyValueList.tsx | 36 ++++++---- src/ui/pages/Secrets/KeyValue/KeyValueNew.tsx | 60 +++++++++------- .../Secrets/KeyValue/KeyValueVersions.tsx | 12 +++- .../pages/Secrets/KeyValue/KeyValueView.tsx | 14 +++- .../pages/Secrets/NewEngines/NewKVEngine.tsx | 14 ++-- .../Secrets/NewEngines/NewTOTPEngine.tsx | 14 ++-- .../Secrets/NewEngines/NewTransitEngine.tsx | 14 ++-- src/ui/pages/Secrets/SecretsHome.tsx | 22 +++--- src/ui/pages/Secrets/TOTP/TOTPDelete.tsx | 20 +++++- src/ui/pages/Secrets/TOTP/TOTPList.tsx | 60 ++++++++-------- src/ui/pages/Secrets/TOTP/TOTPNew.tsx | 11 ++- .../pages/Secrets/TOTP/TOTPNewGenerated.tsx | 9 ++- .../pages/Secrets/Transit/TransitDecrypt.tsx | 15 ++-- .../pages/Secrets/Transit/TransitEncrypt.tsx | 14 ++-- src/ui/pages/Secrets/Transit/TransitList.tsx | 40 ++++++----- src/ui/pages/Secrets/Transit/TransitNew.tsx | 18 +++-- .../pages/Secrets/Transit/TransitRewrap.tsx | 25 +++++-- src/ui/pages/Secrets/Transit/TransitView.tsx | 11 ++- src/ui/pages/SetLanguage.tsx | 3 - src/ui/pages/SetVaultURL.tsx | 2 +- src/ui/pages/Unseal.tsx | 13 ++-- 45 files changed, 695 insertions(+), 333 deletions(-) create mode 100644 src/ui/elements/ErrorMessage.tsx diff --git a/src/pageUtils.ts b/src/pageUtils.ts index 4190924..fe4bfff 100644 --- a/src/pageUtils.ts +++ b/src/pageUtils.ts @@ -60,23 +60,3 @@ export function addClipboardNotifications(clipboard: ClipboardJS, timeout = 1000 ); }); } - -export function setErrorText(text: string): void { - const errorTextElement = document.querySelector("#errorText"); - if (errorTextElement) { - /* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */ - const p = document.querySelector("#errorText") as HTMLParagraphElement; - p.innerText = `Error: ${text}`; - /* eslint-enable @typescript-eslint/no-unnecessary-type-assertion */ - } - UIkit.notification({ - message: `Error: ${text}`, - status: "danger", - pos: "top-center", - timeout: 2000, - }); -} - -export function notImplemented(): void { - setErrorText(i18next.t("not_implemented")); -} diff --git a/src/ui/elements/ErrorMessage.tsx b/src/ui/elements/ErrorMessage.tsx new file mode 100644 index 0000000..9dc293a --- /dev/null +++ b/src/ui/elements/ErrorMessage.tsx @@ -0,0 +1,63 @@ +import { Component, createRef } from "preact"; +import UIkit from "uikit"; +import i18next from "i18next"; + +type ErrorMessageState = { + errorMessage: string; +}; + +// Use only when a error text element can't be used +// e.g inside componentDidMount +export function sendErrorNotification(errorMessage: string) { + UIkit.notification({ + message: `Error: ${errorMessage}`, + status: "danger", + pos: "top-center", + timeout: 2000, + }); +} + +export function notImplementedNotification(): void { + sendErrorNotification(i18next.t("not_implemented")); +} + +export class ErrorMessage extends Component { + public setErrorMessage(errorMessage: string) { + this.setState({ + errorMessage: `Error: ${errorMessage}`, + }); + + sendErrorNotification(errorMessage); + + // make browser focus on the change. + this.errorMessageRef.current.focus(); + } + + public clear() { + this.setState({ errorMessage: "" }); + } + + errorMessageRef = createRef(); + + render() { + return ( +

+ {this.state.errorMessage || ""} +

+ ); + } +} diff --git a/src/ui/pages/Access/AccessHome.tsx b/src/ui/pages/Access/AccessHome.tsx index 8a8c5b3..c055e27 100644 --- a/src/ui/pages/Access/AccessHome.tsx +++ b/src/ui/pages/Access/AccessHome.tsx @@ -3,7 +3,7 @@ import { DefaultPageProps } from "../../../types/DefaultPageProps"; import { Grid, GridSizes } from "../../elements/Grid"; import { PageTitle } from "../../elements/PageTitle"; import { Tile } from "../../elements/Tile"; -import { notImplemented } from "../../../pageUtils"; +import { notImplementedNotification } from "../../elements/ErrorMessage"; import { route } from "preact-router"; import i18next from "i18next"; @@ -23,19 +23,19 @@ export class AccessHomePage extends Component { title={i18next.t("access_entities_title")} description={i18next.t("access_entities_description")} icon="user" - onclick={async () => notImplemented()} + onclick={async () => notImplementedNotification()} /> notImplemented()} + onclick={async () => notImplementedNotification()} /> notImplemented()} + onclick={async () => notImplementedNotification()} /> diff --git a/src/ui/pages/Access/Auth/AuthHome.tsx b/src/ui/pages/Access/Auth/AuthHome.tsx index bcb3954..e3b2453 100644 --- a/src/ui/pages/Access/Auth/AuthHome.tsx +++ b/src/ui/pages/Access/Auth/AuthHome.tsx @@ -4,7 +4,7 @@ import { Component, JSX } from "preact"; import { DefaultPageProps } from "../../../../types/DefaultPageProps"; import { PageTitle } from "../../../elements/PageTitle"; import { authViewConfigURL, userPassUserListURL } from "../../pageLinks"; -import { notImplemented } from "../../../../pageUtils"; +import { notImplementedNotification, sendErrorNotification } from "../../../elements/ErrorMessage"; import { objectToMap } from "../../../../utils"; import { route } from "preact-router"; import i18next from "i18next"; @@ -46,7 +46,7 @@ export function AuthListElement(props: AuthListElementProps): JSX.Element {