import { t } from "@lingui/macro"; import { CSSResult, html, TemplateResult } from "lit"; import { customElement, property } from "lit/decorators"; import { until } from "lit/directives/until"; import PFList from "@patternfly/patternfly/components/List/list.css"; import { UsedBy, UsedByActionEnum } from "@goauthentik/api"; import { EVENT_REFRESH } from "../../constants"; import { ModalButton } from "../buttons/ModalButton"; import "../buttons/SpinnerButton"; import { MessageLevel } from "../messages/Message"; import { showMessage } from "../messages/MessageContainer"; @customElement("ak-forms-delete") export class DeleteForm extends ModalButton { static get styles(): CSSResult[] { return super.styles.concat(PFList); } @property({ attribute: false }) obj?: Record; @property() objectLabel?: string; @property({ attribute: false }) usedBy?: () => Promise; @property({ attribute: false }) delete!: () => Promise; confirm(): Promise { 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; }); } onSuccess(): void { showMessage({ message: t`Successfully deleted ${this.objectLabel} ${this.obj?.name}`, level: MessageLevel.success, }); } onError(e: Error): void { showMessage({ message: t`Failed to delete ${this.objectLabel}: ${e.toString()}`, level: MessageLevel.error, }); } renderModalInner(): TemplateResult { let objName = this.obj?.name; if (objName) { objName = ` "${objName}"`; } else { objName = ""; } return html`

${t`Delete ${this.objectLabel}`}

${t`Are you sure you want to delete ${this.objectLabel} ${objName} ?`}

${this.usedBy ? until( this.usedBy().then((usedBy) => { if (usedBy.length < 1) { return html``; } return html`

${t`The following objects use ${objName} `}

    ${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`
  • ${t`${ub.name} (${consequence})`}
  • `; })}
`; }), ) : html``} `; } }