import { t } from "@lingui/macro"; import { customElement, html, property, TemplateResult } from "lit-element"; import { EVENT_REFRESH } from "../../constants"; import { ModalButton } from "../buttons/ModalButton"; import { MessageLevel } from "../messages/Message"; import { showMessage } from "../messages/MessageContainer"; import "../buttons/SpinnerButton"; @customElement("ak-forms-delete") export class DeleteForm extends ModalButton { @property({attribute: false}) obj?: Record; @property() objectLabel?: string; @property({attribute: false}) delete!: () => Promise; confirm(): void { this.delete().then(() => { this.onSuccess(); this.open = false; this.dispatchEvent( new CustomEvent(EVENT_REFRESH, { bubbles: true, composed: true, }) ); }).catch((e) => { this.onError(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 { return html`

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

${t`Are you sure you want to delete ${this.objectLabel} '${this.obj?.name}'?`}

`; } }