2021-04-03 17:26:43 +00:00
|
|
|
import { t } from "@lingui/macro";
|
2021-09-21 09:19:26 +00:00
|
|
|
import { CSSResult, html, TemplateResult } from "lit";
|
|
|
|
import { customElement, property } from "lit/decorators";
|
2021-03-23 14:16:56 +00:00
|
|
|
import { EVENT_REFRESH } from "../../constants";
|
2021-03-18 00:43:12 +00:00
|
|
|
import { ModalButton } from "../buttons/ModalButton";
|
2021-03-27 22:11:44 +00:00
|
|
|
import { MessageLevel } from "../messages/Message";
|
2021-03-18 10:16:13 +00:00
|
|
|
import { showMessage } from "../messages/MessageContainer";
|
2021-04-03 12:47:34 +00:00
|
|
|
import "../buttons/SpinnerButton";
|
2021-08-15 19:32:28 +00:00
|
|
|
import { UsedBy, UsedByActionEnum } from "@goauthentik/api";
|
2021-06-10 09:58:12 +00:00
|
|
|
import PFList from "@patternfly/patternfly/components/List/list.css";
|
2021-09-21 09:19:26 +00:00
|
|
|
import { until } from "lit/directives/until";
|
2021-03-18 00:43:12 +00:00
|
|
|
|
|
|
|
@customElement("ak-forms-delete")
|
|
|
|
export class DeleteForm extends ModalButton {
|
2021-06-10 09:58:12 +00:00
|
|
|
static get styles(): CSSResult[] {
|
|
|
|
return super.styles.concat(PFList);
|
|
|
|
}
|
|
|
|
|
2021-08-03 15:52:21 +00:00
|
|
|
@property({ attribute: false })
|
2021-03-18 00:49:01 +00:00
|
|
|
obj?: Record<string, unknown>;
|
2021-03-18 00:43:12 +00:00
|
|
|
|
|
|
|
@property()
|
|
|
|
objectLabel?: string;
|
|
|
|
|
2021-08-03 15:52:21 +00:00
|
|
|
@property({ attribute: false })
|
2021-06-10 09:58:12 +00:00
|
|
|
usedBy?: () => Promise<UsedBy[]>;
|
|
|
|
|
2021-08-03 15:52:21 +00:00
|
|
|
@property({ attribute: false })
|
2021-03-18 17:12:04 +00:00
|
|
|
delete!: () => Promise<unknown>;
|
2021-03-18 00:43:12 +00:00
|
|
|
|
2021-04-04 18:42:50 +00:00
|
|
|
confirm(): Promise<void> {
|
2021-08-03 15:52:21 +00:00
|
|
|
return this.delete()
|
|
|
|
.then(() => {
|
|
|
|
this.onSuccess();
|
|
|
|
this.open = false;
|
|
|
|
this.dispatchEvent(
|
|
|
|
new CustomEvent(EVENT_REFRESH, {
|
|
|
|
bubbles: true,
|
|
|
|
composed: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
this.onError(e);
|
|
|
|
throw e;
|
|
|
|
});
|
2021-03-18 12:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onSuccess(): void {
|
|
|
|
showMessage({
|
2021-08-03 15:52:21 +00:00
|
|
|
message: t`Successfully deleted ${this.objectLabel} ${this.obj?.name}`,
|
2021-03-27 22:11:44 +00:00
|
|
|
level: MessageLevel.success,
|
2021-03-18 12:09:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onError(e: Error): void {
|
|
|
|
showMessage({
|
2021-04-03 17:26:43 +00:00
|
|
|
message: t`Failed to delete ${this.objectLabel}: ${e.toString()}`,
|
2021-03-27 22:11:44 +00:00
|
|
|
level: MessageLevel.error,
|
2021-03-18 11:14:27 +00:00
|
|
|
});
|
2021-03-18 00:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderModalInner(): TemplateResult {
|
2021-04-06 15:27:53 +00:00
|
|
|
let objName = this.obj?.name;
|
|
|
|
if (objName) {
|
|
|
|
objName = ` "${objName}"`;
|
|
|
|
} else {
|
|
|
|
objName = "";
|
|
|
|
}
|
2021-03-18 00:43:12 +00:00
|
|
|
return html`<section class="pf-c-page__main-section pf-m-light">
|
2021-08-03 15:52:21 +00:00
|
|
|
<div class="pf-c-content">
|
|
|
|
<h1 class="pf-c-title pf-m-2xl">${t`Delete ${this.objectLabel}`}</h1>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
<section class="pf-c-page__main-section pf-m-light">
|
|
|
|
<form class="pf-c-form pf-m-horizontal">
|
|
|
|
<p>${t`Are you sure you want to delete ${this.objectLabel} ${objName} ?`}</p>
|
|
|
|
</form>
|
|
|
|
</section>
|
|
|
|
${this.usedBy
|
|
|
|
? until(
|
|
|
|
this.usedBy().then((usedBy) => {
|
|
|
|
if (usedBy.length < 1) {
|
|
|
|
return html``;
|
|
|
|
}
|
|
|
|
return html`
|
|
|
|
<section class="pf-c-page__main-section">
|
|
|
|
<form class="pf-c-form pf-m-horizontal">
|
|
|
|
<p>${t`The following objects use ${objName} `}</p>
|
|
|
|
<ul class="pf-c-list">
|
|
|
|
${usedBy.map((ub) => {
|
|
|
|
let consequence = "";
|
|
|
|
switch (ub.action) {
|
|
|
|
case UsedByActionEnum.Cascade:
|
|
|
|
consequence = t`object will be DELETED`;
|
|
|
|
break;
|
|
|
|
case UsedByActionEnum.CascadeMany:
|
|
|
|
consequence = t`connecting object will be deleted`;
|
|
|
|
break;
|
|
|
|
case UsedByActionEnum.SetDefault:
|
|
|
|
consequence = t`reference will be reset to default value`;
|
|
|
|
break;
|
|
|
|
case UsedByActionEnum.SetNull:
|
|
|
|
consequence = t`reference will be set to an empty value`;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return html`<li>
|
|
|
|
${t`${ub.name} (${consequence})`}
|
|
|
|
</li>`;
|
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
</form>
|
|
|
|
</section>
|
|
|
|
`;
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
: html``}
|
|
|
|
<footer class="pf-c-modal-box__footer">
|
|
|
|
<ak-spinner-button
|
|
|
|
.callAction=${() => {
|
|
|
|
return this.confirm();
|
|
|
|
}}
|
|
|
|
class="pf-m-danger"
|
|
|
|
>
|
|
|
|
${t`Delete`} </ak-spinner-button
|
|
|
|
>
|
|
|
|
<ak-spinner-button
|
|
|
|
.callAction=${async () => {
|
|
|
|
this.open = false;
|
|
|
|
}}
|
|
|
|
class="pf-m-secondary"
|
|
|
|
>
|
|
|
|
${t`Cancel`}
|
|
|
|
</ak-spinner-button>
|
|
|
|
</footer>`;
|
2021-03-18 00:43:12 +00:00
|
|
|
}
|
|
|
|
}
|