2021-04-03 17:26:43 +00:00
|
|
|
import { t } from "@lingui/macro";
|
2021-03-18 00:43:12 +00:00
|
|
|
import { customElement, html, property, TemplateResult } from "lit-element";
|
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-03-18 00:43:12 +00:00
|
|
|
|
|
|
|
@customElement("ak-forms-delete")
|
|
|
|
export class DeleteForm extends ModalButton {
|
|
|
|
|
2021-03-18 00:49:01 +00:00
|
|
|
@property({attribute: false})
|
|
|
|
obj?: Record<string, unknown>;
|
2021-03-18 00:43:12 +00:00
|
|
|
|
|
|
|
@property()
|
|
|
|
objectLabel?: string;
|
|
|
|
|
|
|
|
@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> {
|
|
|
|
return this.delete().then(() => {
|
2021-03-18 12:09:00 +00:00
|
|
|
this.onSuccess();
|
2021-03-18 00:43:12 +00:00
|
|
|
this.open = false;
|
|
|
|
this.dispatchEvent(
|
2021-03-23 14:16:56 +00:00
|
|
|
new CustomEvent(EVENT_REFRESH, {
|
2021-03-18 00:43:12 +00:00
|
|
|
bubbles: true,
|
|
|
|
composed: true,
|
|
|
|
})
|
|
|
|
);
|
2021-03-18 10:16:13 +00:00
|
|
|
}).catch((e) => {
|
2021-03-18 12:09:00 +00:00
|
|
|
this.onError(e);
|
2021-04-04 18:42:50 +00:00
|
|
|
throw e;
|
2021-03-18 12:09:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onSuccess(): void {
|
|
|
|
showMessage({
|
2021-04-03 17:26:43 +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">
|
|
|
|
<div class="pf-c-content">
|
|
|
|
<h1 class="pf-c-title pf-m-2xl">
|
2021-04-03 17:26:43 +00:00
|
|
|
${t`Delete ${this.objectLabel}`}
|
2021-03-18 00:43:12 +00:00
|
|
|
</h1>
|
|
|
|
</div>
|
|
|
|
</section>
|
2021-04-04 11:44:57 +00:00
|
|
|
<section class="pf-c-page__main-section pf-m-light">
|
|
|
|
<form class="pf-c-form pf-m-horizontal">
|
|
|
|
<p>
|
2021-04-06 15:27:53 +00:00
|
|
|
${t`Are you sure you want to delete ${this.objectLabel} ${objName} ?`}
|
2021-04-04 11:44:57 +00:00
|
|
|
</p>
|
|
|
|
</form>
|
2021-03-18 00:43:12 +00:00
|
|
|
</section>
|
|
|
|
<footer class="pf-c-modal-box__footer">
|
|
|
|
<ak-spinner-button
|
|
|
|
.callAction=${() => {
|
2021-04-04 18:42:50 +00:00
|
|
|
return this.confirm();
|
2021-03-18 00:43:12 +00:00
|
|
|
}}
|
|
|
|
class="pf-m-danger">
|
2021-04-03 17:26:43 +00:00
|
|
|
${t`Delete`}
|
2021-03-18 00:43:12 +00:00
|
|
|
</ak-spinner-button>
|
|
|
|
<ak-spinner-button
|
2021-04-04 18:42:50 +00:00
|
|
|
.callAction=${async () => {
|
2021-03-18 00:43:12 +00:00
|
|
|
this.open = false;
|
|
|
|
}}
|
|
|
|
class="pf-m-secondary">
|
2021-04-03 17:26:43 +00:00
|
|
|
${t`Cancel`}
|
2021-03-18 00:43:12 +00:00
|
|
|
</ak-spinner-button>
|
|
|
|
</footer>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|