Skip to content

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

FIX: Correctly set categoryId on Composer model (#23934)

Jarek Radosze31859a33ac2
diff --git a/app/assets/javascripts/discourse/app/models/composer.js b/app/assets/javascripts/discourse/app/models/composer.js
index 0b27a763f5e..6c542d2e191 100644
--- a/app/assets/javascripts/discourse/app/models/composer.js
+++ b/app/assets/javascripts/discourse/app/models/composer.js
@@ -824,7 +824,10 @@ const Composer = RestModel.extend({
});
// We set the category id separately for topic templates on opening of composer
- this.set("categoryId", opts.categoryId || this.get("topic.category.id"));
+ this.set(
+ "categoryId",
+ opts.topicCategoryId || opts.categoryId || this.get("topic.category.id")
+ );
if (!this.categoryId && this.creatingTopic) {
const categories = this.site.categories;
diff --git a/app/assets/javascripts/discourse/app/services/composer.js b/app/assets/javascripts/discourse/app/services/composer.js
index 114d95ea5bc..03bb68f6710 100644
--- a/app/assets/javascripts/discourse/app/services/composer.js
+++ b/app/assets/javascripts/discourse/app/services/composer.js
@@ -1455,10 +1455,6 @@ export default class ComposerService extends Service {
this.model.set("title", opts.topicTitle);
}
- if (opts.topicCategoryId) {
- this.model.set("categoryId", opts.topicCategoryId);
- }
-
if (opts.topicTags && this.site.can_tag_topics) {
let tags = escapeExpression(opts.topicTags)
.split(",")
diff --git a/spec/system/composer/category_templates_spec.rb b/spec/system/composer/category_templates_spec.rb
index 1de2173e719..af045552520 100644
--- a/spec/system/composer/category_templates_spec.rb
+++ b/spec/system/composer/category_templates_spec.rb
@@ -160,6 +160,77 @@ describe "Composer Form Templates", type: :system do
sign_in user
end
+ describe "discard draft modal" do
+ it "does not show the modal if there is no draft on a topic without a template" do
+ category_page.visit(category_no_template)
+ category_page.new_topic_button.click
+ composer.close
+ expect(composer).to be_closed
+ end
+
+ it "shows the modal if there is a draft on a topic without a template" do
+ category_page.visit(category_no_template)
+ category_page.new_topic_button.click
+ composer.fill_content("abc")
+ composer.close
+ expect(composer).to be_opened
+ expect(composer).to have_discard_draft_modal
+ end
+
+ it "does not show the modal if there is no draft on a topic with a topic template" do
+ category_page.visit(category_topic_template)
+ category_page.new_topic_button.click
+ composer.close
+ expect(composer).to be_closed
+ end
+
+ it "shows the modal if there is a draft on a topic with a topic template" do
+ category_page.visit(category_topic_template)
+ category_page.new_topic_button.click
+ composer.append_content(" some more content")
+ composer.close
+ expect(composer).to be_opened
+ expect(composer).to have_discard_draft_modal
+ end
+
+ it "does not show the modal if on a topic with a form template" do
+ category_page.visit(category_with_template_1)
+ category_page.new_topic_button.click
+ composer.close
+ expect(composer).to be_closed
+ end
+
+ context "when the default template has a topic template" do
+ SiteSetting.default_composer_category =
+ (
+ if SiteSetting.general_category_id != -1
+ SiteSetting.general_category_id
+ else
+ SiteSetting.uncategorized_category_id
+ end
+ )
+ let(:default_category) { Category.find(SiteSetting.default_composer_category) }
+
+ before { default_category.update!(topic_template: "Testing") }
+
+ it "does not show the modal if there is no draft" do
+ category_page.visit(default_category)
+ category_page.new_topic_button.click
+ composer.close
+ expect(composer).to be_closed
+ end
+
+ it "shows the modal if there is a draft" do
+ category_page.visit(default_category)
+ category_page.new_topic_button.click
+ composer.append_content(" some more content")
+ composer.close
+ expect(composer).to be_opened
+ expect(composer).to have_discard_draft_modal
+ end
+ end
+ end
+
it "shows a textarea when no form template is assigned to the category" do
category_page.visit(category_no_template)
category_page.new_topic_button.click
diff --git a/spec/system/page_objects/components/composer.rb b/spec/system/page_objects/components/composer.rb
index 83b640011d1..ccf303b9ebc 100644
--- a/spec/system/page_objects/components/composer.rb
+++ b/spec/system/page_objects/components/composer.rb
@@ -10,6 +10,10 @@ module PageObjects
page.has_css?("#{COMPOSER_ID}.open")
end
+ def closed?
+ page.has_css?("#{COMPOSER_ID}.closed", visible: :all)
+ end
+
def open_composer_actions
find(".composer-action-title .btn").click
self
@@ -30,6 +34,12 @@ module PageObjects
self
end
+ def append_content(content)
+ current_content = composer_input.value
+ composer_input.set(current_content + content)
+ self
+ end
+
def fill_form_template_field(field, content)
form_template_field(field).fill_in(with: content)
self
@@ -90,6 +100,10 @@ module PageObjects
find("#{COMPOSER_ID} .d-editor-preview-wrapper")
end
+ def has_discard_draft_modal?
+ page.has_css?(".discard-draft-modal")
+ end
+
def has_emoji_autocomplete?
has_css?(AUTOCOMPLETE_MENU)
end