From 002c048d0b910bf14a000a6a32b94755c2e18bbf Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sun, 4 Apr 2021 20:42:50 +0200 Subject: [PATCH] web/elements: rewrite SpinnerButton to promises, fix spinner button with forms after errors Signed-off-by: Jens Langhammer --- web/src/authentik.css | 9 ----- web/src/elements/buttons/ActionButton.ts | 12 ++---- web/src/elements/buttons/SpinnerButton.ts | 10 +++-- web/src/elements/forms/ConfirmationForm.ts | 9 +++-- web/src/elements/forms/DeleteForm.ts | 9 +++-- web/src/elements/forms/ModalForm.ts | 43 +++++++++++----------- web/src/pages/users/UserActiveForm.ts | 4 +- 7 files changed, 44 insertions(+), 52 deletions(-) diff --git a/web/src/authentik.css b/web/src/authentik.css index 6a81f4eb9..1b4b9defc 100644 --- a/web/src/authentik.css +++ b/web/src/authentik.css @@ -101,15 +101,6 @@ body { --pf-c-page__main-section--m-light--BackgroundColor: var(--ak-dark-background-darker); --pf-global--link--Color: var(--ak-dark-foreground-link); } - - paper-input { - /* --paper-input-container-input-color: var(--ak-dark-foreground); */ - --primary-text-color: var(--ak-dark-foreground); - } - paper-checkbox { - --primary-text-color: var(--ak-dark-foreground); - } - /* Global page background colour */ .pf-c-page { --pf-c-page--BackgroundColor: var(--ak-dark-background); diff --git a/web/src/elements/buttons/ActionButton.ts b/web/src/elements/buttons/ActionButton.ts index e0d7d85b5..ded130b77 100644 --- a/web/src/elements/buttons/ActionButton.ts +++ b/web/src/elements/buttons/ActionButton.ts @@ -1,5 +1,4 @@ import { customElement, property } from "lit-element"; -import { ERROR_CLASS, SUCCESS_CLASS } from "../../constants"; import { SpinnerButton } from "./SpinnerButton"; import { showMessage } from "../messages/MessageContainer"; import { MessageLevel } from "../messages/Message"; @@ -16,14 +15,9 @@ export class ActionButton extends SpinnerButton { // eslint-disable-next-line @typescript-eslint/no-explicit-any apiRequest: () => Promise = () => { throw new Error(); }; - callAction = (): void => { - if (this.isRunning === true) { - return; - } + callAction = (): Promise => { this.setLoading(); - this.apiRequest().then(() => { - this.setDone(SUCCESS_CLASS); - }).catch((e: Error | Response) => { + return this.apiRequest().catch((e: Error | Response) => { if (e instanceof Error) { showMessage({ level: MessageLevel.error, @@ -37,7 +31,7 @@ export class ActionButton extends SpinnerButton { }); }); } - this.setDone(ERROR_CLASS); + throw e; }); }; } diff --git a/web/src/elements/buttons/SpinnerButton.ts b/web/src/elements/buttons/SpinnerButton.ts index 73d5b3074..5476ea27f 100644 --- a/web/src/elements/buttons/SpinnerButton.ts +++ b/web/src/elements/buttons/SpinnerButton.ts @@ -4,7 +4,7 @@ import PFButton from "@patternfly/patternfly/components/Button/button.css"; import PFSpinner from "@patternfly/patternfly/components/Spinner/spinner.css"; import AKGlobal from "../../authentik.css"; import { SpinnerSize } from "../Spinner"; -import { PRIMARY_CLASS, PROGRESS_CLASS } from "../../constants"; +import { ERROR_CLASS, PRIMARY_CLASS, PROGRESS_CLASS, SUCCESS_CLASS } from "../../constants"; @customElement("ak-spinner-button") export class SpinnerButton extends LitElement { @@ -12,7 +12,7 @@ export class SpinnerButton extends LitElement { isRunning = false; @property() - callAction?: () => void; + callAction?: () => Promise; static get styles(): CSSResult[] { return [ @@ -60,7 +60,11 @@ export class SpinnerButton extends LitElement { } this.setLoading(); if (this.callAction) { - this.callAction(); + this.callAction().then(() => { + this.setDone(SUCCESS_CLASS); + }).catch(() => { + this.setDone(ERROR_CLASS); + }); } }}> ${this.isRunning diff --git a/web/src/elements/forms/ConfirmationForm.ts b/web/src/elements/forms/ConfirmationForm.ts index e004154c8..1872f30f8 100644 --- a/web/src/elements/forms/ConfirmationForm.ts +++ b/web/src/elements/forms/ConfirmationForm.ts @@ -20,8 +20,8 @@ export class ConfirmationForm extends ModalButton { @property({attribute: false}) onConfirm!: () => Promise; - confirm(): void { - this.onConfirm().then(() => { + confirm(): Promise { + return this.onConfirm().then(() => { this.onSuccess(); this.open = false; this.dispatchEvent( @@ -32,6 +32,7 @@ export class ConfirmationForm extends ModalButton { ); }).catch((e) => { this.onError(e); + throw e; }); } @@ -65,13 +66,13 @@ export class ConfirmationForm extends ModalButton {