Skip to content

A platform for community discussion. Free, open, simple.

DEV: allows to intercept the closeModal early (#36097)

Joffrey JAFFEUX3a02ab0fdfbf
diff --git a/frontend/discourse/app/components/d-modal.gjs b/frontend/discourse/app/components/d-modal.gjs
index 54799d282b3..f0a5b540b23 100644
--- a/frontend/discourse/app/components/d-modal.gjs
+++ b/frontend/discourse/app/components/d-modal.gjs
@@ -180,6 +180,13 @@ export default class DModal extends Component {
return;
}
+ if (this.args.shouldClose) {
+ const shouldClose = await this.args.shouldClose({ initiatedBy }, event);
+ if (shouldClose === false) {
+ return;
+ }
+ }
+
try {
this.animating = true;
diff --git a/frontend/discourse/app/components/modal/spreadsheet-editor.gjs b/frontend/discourse/app/components/modal/spreadsheet-editor.gjs
index 6b97b2f8a63..684ff973a69 100644
--- a/frontend/discourse/app/components/modal/spreadsheet-editor.gjs
+++ b/frontend/discourse/app/components/modal/spreadsheet-editor.gjs
@@ -77,15 +77,15 @@ export default class SpreadsheetEditor extends Component {
}
@action
- interceptCloseModal() {
+ async interceptCloseModal() {
if (this._hasChanges()) {
- this.dialog.yesNoConfirm({
+ return await this.dialog.yesNoConfirm({
message: i18n("table_builder.modal.confirm_close"),
- didConfirm: () => this.args.closeModal(),
+ didConfirm: () => true,
});
- } else {
- this.args.closeModal();
}
+
+ return true;
}
@action
@@ -349,7 +349,8 @@ export default class SpreadsheetEditor extends Component {
<template>
<DModal
@title={{i18n this.modalAttributes.title}}
- @closeModal={{this.interceptCloseModal}}
+ @closeModal={{@closeModal}}
+ @shouldClose={{this.interceptCloseModal}}
class="insert-table-modal"
>
<:body>
@@ -372,7 +373,7 @@ export default class SpreadsheetEditor extends Component {
class="btn-insert-table"
/>
- <DModalCancel @close={{this.interceptCloseModal}} />
+ <DModalCancel />
</div>
<div class="secondary-actions">
diff --git a/frontend/discourse/tests/integration/components/d-modal-test.gjs b/frontend/discourse/tests/integration/components/d-modal-test.gjs
index 0f5edddb2ee..7d60df96130 100644
--- a/frontend/discourse/tests/integration/components/d-modal-test.gjs
+++ b/frontend/discourse/tests/integration/components/d-modal-test.gjs
@@ -106,6 +106,45 @@ module("Integration | Component | d-modal", function (hooks) {
assert.dom(".d-modal .alert").hasText("Some message");
});
+ test("shouldClose", async function (assert) {
+ class TestState {
+ shouldCloseModal = false;
+ closeModalCalled = false;
+
+ @action
+ closeModal() {
+ this.closeModalCalled = true;
+ }
+
+ @action
+ shouldClose() {
+ return this.shouldCloseModal;
+ }
+ }
+
+ const testState = new TestState();
+
+ await render(
+ <template>
+ <DModal
+ @inline={{true}}
+ @closeModal={{testState.closeModal}}
+ @shouldClose={{testState.shouldClose}}
+ />
+ </template>
+ );
+
+ await click(".d-modal .modal-close");
+
+ assert.false(testState.closeModalCalled);
+
+ testState.shouldCloseModal = true;
+
+ await click(".d-modal .modal-close");
+
+ assert.true(testState.closeModalCalled);
+ });
+
test("flash type", async function (assert) {
await render(
<template>