2021-10-28 07:48:51 +00:00
|
|
|
import { CSSResult, LitElement, TemplateResult, css, html } from "lit";
|
2021-11-04 21:34:48 +00:00
|
|
|
import { customElement, property } from "lit/decorators.js";
|
2021-09-21 09:31:37 +00:00
|
|
|
|
|
|
|
import AKGlobal from "../../authentik.css";
|
2021-03-17 16:50:44 +00:00
|
|
|
import PFButton from "@patternfly/patternfly/components/Button/button.css";
|
|
|
|
import PFSpinner from "@patternfly/patternfly/components/Spinner/spinner.css";
|
2021-09-21 09:31:37 +00:00
|
|
|
import PFBase from "@patternfly/patternfly/patternfly-base.css";
|
|
|
|
|
2021-09-19 13:57:12 +00:00
|
|
|
import { ERROR_CLASS, PROGRESS_CLASS, SUCCESS_CLASS } from "../../constants";
|
2021-09-21 09:31:37 +00:00
|
|
|
import { PFSize } from "../Spinner";
|
2020-11-29 17:10:12 +00:00
|
|
|
|
2020-12-05 21:08:42 +00:00
|
|
|
@customElement("ak-spinner-button")
|
2020-11-29 17:10:12 +00:00
|
|
|
export class SpinnerButton extends LitElement {
|
2021-08-03 15:52:21 +00:00
|
|
|
@property({ type: Boolean })
|
2020-11-29 17:10:12 +00:00
|
|
|
isRunning = false;
|
|
|
|
|
2020-11-29 17:46:45 +00:00
|
|
|
@property()
|
2021-09-24 08:44:23 +00:00
|
|
|
callAction?: () => Promise<unknown>;
|
2021-03-18 00:43:12 +00:00
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
static get styles(): CSSResult[] {
|
2020-11-29 17:10:12 +00:00
|
|
|
return [
|
2021-03-17 16:50:44 +00:00
|
|
|
PFBase,
|
|
|
|
PFButton,
|
|
|
|
PFSpinner,
|
2021-03-20 19:47:53 +00:00
|
|
|
AKGlobal,
|
2020-11-29 17:10:12 +00:00
|
|
|
css`
|
|
|
|
button {
|
|
|
|
/* Have to use !important here, as buttons with pf-m-progress have transition already */
|
|
|
|
transition: all var(--pf-c-button--m-progress--TransitionDuration) ease 0s !important;
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
setLoading(): void {
|
2020-11-29 17:10:12 +00:00
|
|
|
this.isRunning = true;
|
|
|
|
this.classList.add(PROGRESS_CLASS);
|
|
|
|
this.requestUpdate();
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
setDone(statusClass: string): void {
|
2020-11-29 17:10:12 +00:00
|
|
|
this.isRunning = false;
|
|
|
|
this.classList.remove(PROGRESS_CLASS);
|
2021-09-19 13:57:12 +00:00
|
|
|
this.classList.add(statusClass);
|
2020-11-29 17:10:12 +00:00
|
|
|
this.requestUpdate();
|
|
|
|
setTimeout(() => {
|
2021-09-19 13:57:12 +00:00
|
|
|
this.classList.remove(statusClass);
|
2020-11-29 17:10:12 +00:00
|
|
|
this.requestUpdate();
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
render(): TemplateResult {
|
2020-11-29 17:10:12 +00:00
|
|
|
return html`<button
|
|
|
|
class="pf-c-button pf-m-progress ${this.classList.toString()}"
|
2021-04-03 21:09:52 +00:00
|
|
|
@click=${() => {
|
|
|
|
if (this.isRunning === true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setLoading();
|
2021-04-03 21:23:29 +00:00
|
|
|
if (this.callAction) {
|
2021-08-03 15:52:21 +00:00
|
|
|
this.callAction()
|
|
|
|
.then(() => {
|
|
|
|
this.setDone(SUCCESS_CLASS);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
this.setDone(ERROR_CLASS);
|
|
|
|
});
|
2021-04-03 21:23:29 +00:00
|
|
|
}
|
2021-08-03 15:52:21 +00:00
|
|
|
}}
|
|
|
|
>
|
2020-11-29 17:10:12 +00:00
|
|
|
${this.isRunning
|
2021-08-05 20:04:45 +00:00
|
|
|
? html`<span class="pf-c-button__progress">
|
2021-08-03 15:52:21 +00:00
|
|
|
<ak-spinner size=${PFSize.Medium}></ak-spinner>
|
|
|
|
</span>`
|
2021-02-19 17:43:57 +00:00
|
|
|
: ""}
|
2020-11-29 17:10:12 +00:00
|
|
|
<slot></slot>
|
|
|
|
</button>`;
|
|
|
|
}
|
|
|
|
}
|