From c8a02161dd46e9554c83b6f6b7036f626548aaa6 Mon Sep 17 00:00:00 2001 From: Mark VanLandingham Date: Tue, 4 Feb 2020 09:59:56 -0600 Subject: [PATCH] FIX: Warn users of overwriting new topic draft (#8841) --- .../components/composer-actions.js.es6 | 16 +++ config/locales/client.en.yml | 1 + .../acceptance/composer-actions-test.js.es6 | 126 ++++++++++++------ .../acceptance/search-full-test.js.es6 | 10 +- .../acceptance/user-drafts-stream-test.js.es6 | 7 +- test/javascripts/fixtures/draft.js.es6 | 3 +- 6 files changed, 117 insertions(+), 46 deletions(-) diff --git a/app/assets/javascripts/select-kit/components/composer-actions.js.es6 b/app/assets/javascripts/select-kit/components/composer-actions.js.es6 index 7b8b461d35f..125b8701cf9 100644 --- a/app/assets/javascripts/select-kit/components/composer-actions.js.es6 +++ b/app/assets/javascripts/select-kit/components/composer-actions.js.es6 @@ -5,6 +5,7 @@ import { CREATE_SHARED_DRAFT, REPLY } from "discourse/models/composer"; +import Draft from "discourse/models/draft"; import { computed } from "@ember/object"; import { camelize } from "@ember/string"; @@ -223,6 +224,21 @@ export default DropdownSelectBoxComponent.extend({ }, replyAsNewTopicSelected(options) { + Draft.get("new_topic").then(response => { + if (response.draft) { + bootbox.confirm( + I18n.t("composer.composer_actions.reply_as_new_topic.confirm"), + result => { + if (result) this._replyAsNewTopicSelect(options); + } + ); + } else { + this._replyAsNewTopicSelect(options); + } + }); + }, + + _replyAsNewTopicSelect(options) { options.action = CREATE_TOPIC; options.categoryId = this.get("composerModel.topic.category.id"); options.disableScopedCategory = true; diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 0a1a7264be0..ff18330584f 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1756,6 +1756,7 @@ en: reply_as_new_topic: label: Reply as linked topic desc: Create a new topic linked to this topic + confirm: You have a new topic draft saved, which will be overwritten if you create a linked topic. reply_as_private_message: label: New message desc: Create a new personal message diff --git a/test/javascripts/acceptance/composer-actions-test.js.es6 b/test/javascripts/acceptance/composer-actions-test.js.es6 index fe4dceb9345..c95497eb084 100644 --- a/test/javascripts/acceptance/composer-actions-test.js.es6 +++ b/test/javascripts/acceptance/composer-actions-test.js.es6 @@ -2,6 +2,8 @@ import selectKit from "helpers/select-kit-helper"; import { acceptance, updateCurrentUser } from "helpers/qunit-helpers"; import { _clearSnapshots } from "select-kit/components/composer-actions"; import { toggleCheckDraftPopup } from "discourse/controllers/composer"; +import Draft from "discourse/models/draft"; +import { Promise } from "rsvp"; acceptance("Composer Actions", { loggedIn: true, @@ -99,6 +101,9 @@ QUnit.test("replying to post - toggle_whisper", async assert => { }); QUnit.test("replying to post - reply_as_new_topic", async assert => { + sandbox + .stub(Draft, "get") + .returns(Promise.resolve({ draft: "", draft_sequence: 0 })); const composerActions = selectKit(".composer-actions"); const categoryChooser = selectKit(".title-wrapper .category-chooser"); const categoryChooserReplyArea = selectKit(".reply-area .category-chooser"); @@ -129,46 +134,16 @@ QUnit.test("replying to post - reply_as_new_topic", async assert => { .val() .includes(quote) ); + sandbox.restore(); }); -QUnit.test("shared draft", async assert => { - try { - toggleCheckDraftPopup(true); - - const composerActions = selectKit(".composer-actions"); - const tags = selectKit(".mini-tag-chooser"); - - await visit("/"); - await click("#create-topic"); - - await fillIn( - "#reply-title", - "This is the new text for the title using 'quotes'" - ); - - await fillIn(".d-editor-input", "This is the new text for the post"); - await tags.expand(); - await tags.selectRowByValue("monkey"); - await composerActions.expand(); - await composerActions.selectRowByValue("shared_draft"); - - assert.equal(tags.header().value(), "monkey", "tags are not reset"); - - assert.equal( - find("#reply-title").val(), - "This is the new text for the title using 'quotes'" - ); - - assert.equal( - find("#reply-control .btn-primary.create .d-button-label").text(), - I18n.t("composer.create_shared_draft") - ); - - assert.ok(find("#reply-control.composing-shared-draft").length === 1); - await click(".modal-footer .btn.btn-default"); - } finally { - toggleCheckDraftPopup(false); - } +QUnit.test("reply_as_new_topic without a new_topic draft", async assert => { + await visit("/t/internationalization-localization/280"); + await click(".create.reply"); + const composerActions = selectKit(".composer-actions"); + await composerActions.expand(); + await composerActions.selectRowByValue("reply_as_new_topic"); + assert.equal(exists(find(".bootbox")), false); }); QUnit.test("hide component if no content", async assert => { @@ -363,3 +338,78 @@ QUnit.test( ); } ); + +acceptance("Composer Actions With New Topic Draft", { + loggedIn: true, + settings: { + enable_whispers: true + }, + site: { + can_tag_topics: true + }, + beforeEach() { + _clearSnapshots(); + }, + pretend(server, helper) { + server.get("draft.json", () => { + return helper.response({ + draft: + '{"reply":"dum de dum da ba.","action":"createTopic","title":"dum da ba dum dum","categoryId":null,"archetypeId":"regular","metaData":null,"composerTime":540879,"typingTime":3400}', + draft_sequence: 0 + }); + }); + } +}); + +QUnit.test("shared draft", async assert => { + try { + toggleCheckDraftPopup(true); + + const composerActions = selectKit(".composer-actions"); + const tags = selectKit(".mini-tag-chooser"); + + await visit("/"); + await click("#create-topic"); + + await fillIn( + "#reply-title", + "This is the new text for the title using 'quotes'" + ); + + await fillIn(".d-editor-input", "This is the new text for the post"); + await tags.expand(); + await tags.selectRowByValue("monkey"); + await composerActions.expand(); + await composerActions.selectRowByValue("shared_draft"); + + assert.equal(tags.header().value(), "monkey", "tags are not reset"); + + assert.equal( + find("#reply-title").val(), + "This is the new text for the title using 'quotes'" + ); + + assert.equal( + find("#reply-control .btn-primary.create .d-button-label").text(), + I18n.t("composer.create_shared_draft") + ); + + assert.ok(find("#reply-control.composing-shared-draft").length === 1); + await click(".modal-footer .btn.btn-default"); + } finally { + toggleCheckDraftPopup(false); + } +}); + +QUnit.test("reply_as_new_topic with new_topic draft", async assert => { + await visit("/t/internationalization-localization/280"); + await click(".create.reply"); + const composerActions = selectKit(".composer-actions"); + await composerActions.expand(); + await composerActions.selectRowByValue("reply_as_new_topic"); + assert.equal( + find(".bootbox .modal-body").text(), + I18n.t("composer.composer_actions.reply_as_new_topic.confirm") + ); + await click(".modal-footer .btn.btn-default"); +}); diff --git a/test/javascripts/acceptance/search-full-test.js.es6 b/test/javascripts/acceptance/search-full-test.js.es6 index c9185341b33..e961e8015a2 100644 --- a/test/javascripts/acceptance/search-full-test.js.es6 +++ b/test/javascripts/acceptance/search-full-test.js.es6 @@ -366,14 +366,14 @@ QUnit.test("update post time through advanced search ui", async assert => { "it should update the search term correctly" ); - const postTimeSelector = selectKit( - ".search-advanced-options .select-kit#postTime" - ); - await visit("/search"); await fillIn(".search-query", "none"); - await fillIn("#search-post-date .date-picker", "2016-10-05"); + await fillIn("#search-post-date .date-picker", "October 5, 2016"); + + const postTimeSelector = selectKit( + ".search-advanced-options .select-kit#postTime" + ); await postTimeSelector.expand(); await postTimeSelector.selectRowByValue("after"); diff --git a/test/javascripts/acceptance/user-drafts-stream-test.js.es6 b/test/javascripts/acceptance/user-drafts-stream-test.js.es6 index 4804095f364..3a8ae358db3 100644 --- a/test/javascripts/acceptance/user-drafts-stream-test.js.es6 +++ b/test/javascripts/acceptance/user-drafts-stream-test.js.es6 @@ -18,5 +18,10 @@ QUnit.test("Stream - resume draft", async assert => { assert.ok(find(".user-stream-item").length > 0, "has drafts"); await click(".user-stream-item .resume-draft"); - assert.equal(find(".d-editor-input").val(), "dum de dum da ba."); + assert.equal( + find(".d-editor-input") + .val() + .trim(), + "A fun new topic for testing drafts." + ); }); diff --git a/test/javascripts/fixtures/draft.js.es6 b/test/javascripts/fixtures/draft.js.es6 index 7819b4adc65..bdbdfed17c4 100644 --- a/test/javascripts/fixtures/draft.js.es6 +++ b/test/javascripts/fixtures/draft.js.es6 @@ -1,7 +1,6 @@ export default { "/draft.json": { - draft: - '{"reply":"dum de dum da ba.","action":"createTopic","title":"dum da ba dum dum","categoryId":null,"archetypeId":"regular","metaData":null,"composerTime":540879,"typingTime":3400}', + draft: null, draft_sequence: 0 } };