2021-04-03 17:26:43 +00:00
|
|
|
import { t } from "@lingui/macro";
|
2021-03-30 15:33:11 +00:00
|
|
|
import { customElement, html, property, TemplateResult } from "lit-element";
|
2021-03-29 17:20:21 +00:00
|
|
|
import { EVENT_REFRESH } from "../../constants";
|
2021-03-25 13:27:16 +00:00
|
|
|
import { ModalButton } from "../buttons/ModalButton";
|
|
|
|
import { Form } from "./Form";
|
2021-04-03 12:47:34 +00:00
|
|
|
import "../buttons/SpinnerButton";
|
2021-08-10 22:00:07 +00:00
|
|
|
import "../LoadingOverlay";
|
2021-03-25 13:27:16 +00:00
|
|
|
|
|
|
|
@customElement("ak-forms-modal")
|
2021-03-25 20:39:49 +00:00
|
|
|
export class ModalForm extends ModalButton {
|
2021-03-30 15:33:11 +00:00
|
|
|
@property({ type: Boolean })
|
|
|
|
closeAfterSuccessfulSubmit = true;
|
|
|
|
|
2021-08-24 18:13:05 +00:00
|
|
|
@property({ type: Boolean })
|
|
|
|
showSubmitButton = true;
|
|
|
|
|
2021-08-10 22:00:07 +00:00
|
|
|
@property({ type: Boolean })
|
|
|
|
loading = false;
|
|
|
|
|
2021-08-24 18:13:05 +00:00
|
|
|
@property({ type: String })
|
|
|
|
cancelText = t`Cancel`;
|
|
|
|
|
2021-08-03 15:52:21 +00:00
|
|
|
confirm(): Promise<void> {
|
2021-04-04 18:42:50 +00:00
|
|
|
const form = this.querySelector<Form<unknown>>("[slot=form]");
|
|
|
|
if (!form) {
|
|
|
|
return Promise.reject(t`No form found`);
|
|
|
|
}
|
|
|
|
const formPromise = form.submit(new Event("submit"));
|
|
|
|
if (!formPromise) {
|
|
|
|
return Promise.reject(t`Form didn't return a promise for submitting`);
|
|
|
|
}
|
2021-08-11 17:48:19 +00:00
|
|
|
return formPromise
|
|
|
|
.then(() => {
|
|
|
|
if (this.closeAfterSuccessfulSubmit) {
|
|
|
|
this.open = false;
|
|
|
|
form?.resetForm();
|
|
|
|
}
|
|
|
|
this.loading = false;
|
2021-08-21 17:19:23 +00:00
|
|
|
this.locked = false;
|
2021-08-11 17:48:19 +00:00
|
|
|
this.dispatchEvent(
|
|
|
|
new CustomEvent(EVENT_REFRESH, {
|
|
|
|
bubbles: true,
|
|
|
|
composed: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch((exc) => {
|
|
|
|
this.loading = false;
|
2021-08-21 17:19:23 +00:00
|
|
|
this.locked = false;
|
2021-08-11 17:48:19 +00:00
|
|
|
throw exc;
|
|
|
|
});
|
2021-03-25 13:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderModalInner(): TemplateResult {
|
2021-08-21 17:26:06 +00:00
|
|
|
return html`${this.loading
|
|
|
|
? html`<ak-loading-overlay ?topMost=${true}></ak-loading-overlay>`
|
|
|
|
: html``}
|
2021-08-10 22:00:07 +00:00
|
|
|
<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">
|
|
|
|
<slot name="header"></slot>
|
|
|
|
</h1>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
<section class="pf-c-page__main-section pf-m-light">
|
|
|
|
<slot name="form"></slot>
|
|
|
|
</section>
|
|
|
|
<footer class="pf-c-modal-box__footer">
|
2021-08-24 18:13:05 +00:00
|
|
|
${this.showSubmitButton
|
|
|
|
? html`<ak-spinner-button
|
|
|
|
.callAction=${() => {
|
|
|
|
this.loading = true;
|
|
|
|
this.locked = true;
|
|
|
|
return this.confirm();
|
|
|
|
}}
|
|
|
|
class="pf-m-primary"
|
|
|
|
>
|
|
|
|
<slot name="submit"></slot> </ak-spinner-button
|
|
|
|
> `
|
|
|
|
: html``}
|
2021-08-03 15:52:21 +00:00
|
|
|
<ak-spinner-button
|
|
|
|
.callAction=${async () => {
|
|
|
|
this.resetForms();
|
|
|
|
this.open = false;
|
|
|
|
}}
|
|
|
|
class="pf-m-secondary"
|
|
|
|
>
|
2021-08-24 18:13:05 +00:00
|
|
|
${this.cancelText}
|
2021-08-03 15:52:21 +00:00
|
|
|
</ak-spinner-button>
|
|
|
|
</footer>`;
|
2021-03-25 13:27:16 +00:00
|
|
|
}
|
|
|
|
}
|