From cf92f9aefc2ee42128e5e88ade5fd49ff2e7fdab Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Fri, 24 Sep 2021 10:44:23 +0200 Subject: [PATCH] web/elements: fix token copy error in safari closes #1219 Signed-off-by: Jens Langhammer --- web/src/elements/buttons/ActionButton.ts | 5 +- web/src/elements/buttons/SpinnerButton.ts | 2 +- web/src/elements/buttons/TokenCopyButton.ts | 57 +++++++++++++++++---- 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/web/src/elements/buttons/ActionButton.ts b/web/src/elements/buttons/ActionButton.ts index 1bf36b967..3828e658b 100644 --- a/web/src/elements/buttons/ActionButton.ts +++ b/web/src/elements/buttons/ActionButton.ts @@ -7,12 +7,11 @@ import { SpinnerButton } from "./SpinnerButton"; @customElement("ak-action-button") export class ActionButton extends SpinnerButton { @property({ attribute: false }) - // eslint-disable-next-line @typescript-eslint/no-explicit-any - apiRequest: () => Promise = () => { + apiRequest: () => Promise = () => { throw new Error(); }; - callAction = (): Promise => { + callAction = (): Promise => { this.setLoading(); return this.apiRequest().catch((e: Error | Response) => { if (e instanceof Error) { diff --git a/web/src/elements/buttons/SpinnerButton.ts b/web/src/elements/buttons/SpinnerButton.ts index d76ea5b9d..5b0b58d74 100644 --- a/web/src/elements/buttons/SpinnerButton.ts +++ b/web/src/elements/buttons/SpinnerButton.ts @@ -15,7 +15,7 @@ export class SpinnerButton extends LitElement { isRunning = false; @property() - callAction?: () => Promise; + callAction?: () => Promise; static get styles(): CSSResult[] { return [ diff --git a/web/src/elements/buttons/TokenCopyButton.ts b/web/src/elements/buttons/TokenCopyButton.ts index 7ae9417f4..5ef5c62e2 100644 --- a/web/src/elements/buttons/TokenCopyButton.ts +++ b/web/src/elements/buttons/TokenCopyButton.ts @@ -1,9 +1,11 @@ +import { html, TemplateResult } from "lit"; import { customElement, property } from "lit/decorators"; import { CoreApi } from "@goauthentik/api"; import { DEFAULT_CONFIG } from "../../api/Config"; import { ERROR_CLASS, SECONDARY_CLASS, SUCCESS_CLASS } from "../../constants"; +import { PFSize } from "../Spinner"; import { ActionButton } from "./ActionButton"; @customElement("ak-token-copy-button") @@ -27,21 +29,58 @@ export class TokenCopyButton extends ActionButton { if (!token.key) { return Promise.reject(); } - return navigator.clipboard.writeText(token.key).then(() => { - this.buttonClass = SUCCESS_CLASS; - setTimeout(() => { - this.buttonClass = SECONDARY_CLASS; - }, 1500); - }); + setTimeout(() => { + this.buttonClass = SECONDARY_CLASS; + }, 1500); + this.buttonClass = SUCCESS_CLASS; + return token.key; }) - .catch((err: Response | undefined) => { + .catch((err: Error | Response | undefined) => { this.buttonClass = ERROR_CLASS; - return err?.json().then((errResp) => { - throw new Error(errResp["detail"]); + if (err instanceof Error) { setTimeout(() => { this.buttonClass = SECONDARY_CLASS; }, 1500); + throw err; + } + return err?.json().then((errResp) => { + setTimeout(() => { + this.buttonClass = SECONDARY_CLASS; + }, 1500); + throw new Error(errResp["detail"]); }); }); }; + + render(): TemplateResult { + return html``; + } }