Skip to content

Commit 804bdcf

Browse files
authored
FIX: resolve discard draft keep editing (#32667)
Fixes a bug where clicking keep editing in the discard modal dialog would result in the composer auto save being disabled. Because of this updates to drafts after this point can easily be lost. The issue was that the `cancelComposer` was not resolving the promise, and therefore not setting `skipAutoSave` to false in the final step. This change also includes another fix for an issue that was revealed where the discard draft dialog would continually open again after we close it. This would happen when editing a draft while viewing a different topic. It wasn't noticed before as the `keep editing` button would result in the code hanging at the `await this.cancelComposer(opts)` stage and not opening the composer again for the new topic reply.
1 parent 84762e6 commit 804bdcf

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

app/assets/javascripts/discourse/app/components/modal/discard-draft.gjs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ export default class DiscardDraftModal extends Component {
1717
this.args.closeModal();
1818
}
1919

20+
@action
21+
async keepEditing() {
22+
await this.args.model.onKeepEditing();
23+
this.args.closeModal();
24+
}
25+
2026
<template>
2127
<DModal
2228
@closeModal={{@closeModal}}
@@ -45,7 +51,7 @@ export default class DiscardDraftModal extends Component {
4551
{{/if}}
4652
<DButton
4753
@label="post.cancel_composer.keep_editing"
48-
@action={{@closeModal}}
54+
@action={{this.keepEditing}}
4955
class="keep-editing"
5056
/>
5157
</:footer>

app/assets/javascripts/discourse/app/services/composer.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,8 +1442,10 @@ export default class ComposerService extends Service {
14421442
}
14431443
}
14441444

1445-
await this.cancelComposer(opts);
1446-
await this.open(opts);
1445+
const retry = await this.cancelComposer(opts);
1446+
if (retry) {
1447+
await this.open(opts);
1448+
}
14471449
return;
14481450
}
14491451

@@ -1627,16 +1629,17 @@ export default class ComposerService extends Service {
16271629
})
16281630
.finally(() => {
16291631
this.appEvents.trigger("composer:cancelled");
1630-
resolve();
1632+
resolve(true);
16311633
});
16321634
},
16331635
onSaveDraft: () => {
16341636
this._saveDraft();
16351637
this.model.clearState();
16361638
this.close();
16371639
this.appEvents.trigger("composer:cancelled");
1638-
return resolve();
1640+
return resolve(true);
16391641
},
1642+
onKeepEditing: () => resolve(false),
16401643
},
16411644
});
16421645
} else {

app/assets/javascripts/discourse/tests/acceptance/composer-test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,51 @@ import { i18n } from "discourse-i18n";
469469
.hasNoValue("discards draft and reset composer textarea");
470470
});
471471

472+
test("Autosaves drafts after clicking keep editing in discard modal", async function (assert) {
473+
pretender.post("/drafts.json", function () {
474+
assert.step("saveDraft");
475+
return response(200, {});
476+
});
477+
478+
await visit("/t/internationalization-localization/280");
479+
480+
await click("#topic-footer-buttons .btn.create");
481+
482+
await fillIn(".d-editor-input", "this is draft content of the reply");
483+
484+
assert.verifySteps(["saveDraft"], "first draft is auto saved");
485+
486+
await click("#reply-control button.cancel");
487+
488+
assert
489+
.dom(".discard-draft-modal.modal")
490+
.exists("pops up the discard drafts modal");
491+
492+
await click(".d-modal__footer button.keep-editing");
493+
assert.dom(".discard-draft-modal.modal").doesNotExist("hides modal");
494+
495+
assert
496+
.dom(".d-editor-input")
497+
.hasValue(
498+
"this is draft content of the reply",
499+
"composer has the content of the first draft"
500+
);
501+
502+
await fillIn(
503+
".d-editor-input",
504+
"this is the updated content of the reply",
505+
"update content in the composer"
506+
);
507+
508+
assert.verifySteps(["saveDraft"], "second draft is saved");
509+
510+
await click("#reply-control button.create");
511+
512+
assert
513+
.dom(".topic-post:last-of-type .cooked p")
514+
.hasText("this is the updated content of the reply");
515+
});
516+
472517
test("Create an enqueued Reply", async function (assert) {
473518
pretender.post("/posts", function () {
474519
return response(200, {

0 commit comments

Comments
 (0)