Skip to content

Commit

Permalink
FIX: Do not offer to save draft if invalid (#13863)
Browse files Browse the repository at this point in the history
An invalid draft is the draft of a topic with a short title or body.
The client does not save these, but it will ask the client if they want
to save it. Even if the answer is 'yes', the draft is discarded. This
commit skips Save button for small drafts.
  • Loading branch information
nbianca committed Jul 30, 2021
1 parent 8498278 commit 531dbc5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
13 changes: 6 additions & 7 deletions app/assets/javascripts/discourse/app/controllers/composer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1189,9 +1189,6 @@ export default Controller.extend({
},
onSaveDraft: () => {
this._saveDraft();
if (this.model.draftKey === Composer.NEW_TOPIC_KEY) {
this.currentUser.set("has_topic_draft", true);
}
this.model.clearState();
this.close();
resolve();
Expand Down Expand Up @@ -1242,10 +1239,12 @@ export default Controller.extend({
);
}
} else {
this._saveDraftPromise = model.saveDraft().finally(() => {
this._lastDraftSaved = Date.now();
this._saveDraftPromise = null;
});
this._saveDraftPromise = model
.saveDraft(this.currentUser)
.finally(() => {
this._lastDraftSaved = Date.now();
this._saveDraftPromise = null;
});
}
}
},
Expand Down
36 changes: 29 additions & 7 deletions app/assets/javascripts/discourse/app/models/composer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1165,38 +1165,56 @@ const Composer = RestModel.extend({
return "";
},

saveDraft() {
@discourseComputed(
"draftSaving",
"disableDrafts",
"canEditTitle",
"title",
"reply",
"titleLengthValid",
"replyLength",
"minimumPostLength"
)
canSaveDraft() {
if (this.draftSaving) {
return Promise.resolve();
return false;
}

// Do not save when drafts are disabled
if (this.disableDrafts) {
return Promise.resolve();
return false;
}

if (this.canEditTitle) {
// Save title and/or post body
if (isEmpty(this.title) && isEmpty(this.reply)) {
return Promise.resolve();
return false;
}

// Do not save when both title and reply's length are too small
if (!this.titleLengthValid && this.replyLength < this.minimumPostLength) {
return Promise.resolve();
return false;
}
} else {
// Do not save when there is no reply
if (isEmpty(this.reply)) {
return Promise.resolve();
return false;
}

// Do not save when the reply's length is too small
if (this.replyLength < this.minimumPostLength) {
return Promise.resolve();
return false;
}
}

return true;
},

saveDraft(user) {
if (!this.canSaveDraft) {
return Promise.resolve();
}

this.setProperties({
draftSaving: true,
draftConflictUser: null,
Expand Down Expand Up @@ -1225,6 +1243,10 @@ const Composer = RestModel.extend({
draftConflictUser: result.conflict_user,
});
} else {
if (this.draftKey === NEW_TOPIC_KEY && user) {
user.set("has_topic_draft", true);
}

this.setProperties({
draftStatus: null,
draftConflictUser: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

<div class="modal-footer">
{{d-button icon="far-trash-alt" label="post.cancel_composer.discard" class="btn-danger discard-draft" action=(action "destroyDraft")}}
{{d-button label="post.cancel_composer.save_draft" class="save-draft" action=(action "saveDraftAndClose")}}
{{#if model.canSaveDraft}}
{{d-button label="post.cancel_composer.save_draft" class="save-draft" action=(action "saveDraftAndClose")}}
{{/if}}
{{d-button label="post.cancel_composer.keep_editing" class="keep-editing" action=(action "dismissModal")}}
</div>
11 changes: 11 additions & 0 deletions app/assets/javascripts/discourse/tests/acceptance/composer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -999,4 +999,15 @@ acceptance("Composer", function (needs) {
await fillIn(".d-editor-input", "@staff");
assert.ok(exists(".composer-popup"), "Shows the 'group_mentioned' notice");
});

test("Does not save invalid draft", async function (assert) {
this.siteSettings.min_first_post_length = 20;

await visit("/");
await click("#create-topic");
await fillIn("#reply-title", "Something");
await fillIn(".d-editor-input", "Something");
await click(".save-or-cancel .cancel");
assert.notOk(exists(".discard-draft-modal .save-draft"));
});
});

0 comments on commit 531dbc5

Please sign in to comment.