From 09a97363dac1d5cc7c8e6046216a3b25c1a53c79 Mon Sep 17 00:00:00 2001 From: jbrw Date: Wed, 23 Sep 2020 10:28:56 -0400 Subject: [PATCH] FIX: allow editing in composer before a category is selected (#10724) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * FIX: allow editing in composer before a category is selected If the site setting `allow_uncategorized_topics` is disabled, but no site categories have a topic template, the textarea of the composer should be enabled. This allows for a post body to be entered, however the post/topic should not be successfully created until a category is selected. If `allow_uncategorized_topics` is disabled *and* one or more categories have a topic template, the textarea of the composer should remain disabled until a category is chosen (so that the body of their post isn’t overwritten by the template). * Avoid recalculating hasTopicTemplates repeatedly --- .../discourse/app/models/composer.js | 7 +- .../acceptance/composer-uncategorized-test.js | 83 +++++++++++++++++-- test/javascripts/fixtures/site-fixtures.js | 3 +- 3 files changed, 84 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/discourse/app/models/composer.js b/app/assets/javascripts/discourse/app/models/composer.js index 7a7054bece0..44ff23abe0d 100644 --- a/app/assets/javascripts/discourse/app/models/composer.js +++ b/app/assets/javascripts/discourse/app/models/composer.js @@ -445,7 +445,8 @@ const Composer = RestModel.extend({ return ( canCategorize && !categoryId && - !this.siteSettings.allow_uncategorized_topics + !this.siteSettings.allow_uncategorized_topics && + !!this._hasTopicTemplates ); }, @@ -753,6 +754,10 @@ const Composer = RestModel.extend({ } } + this._hasTopicTemplates = this.site.categories.some( + (c) => c.topic_template + ); + if (opts.postId) { promise = promise.then(() => this.store.find("post", opts.postId).then((post) => { diff --git a/test/javascripts/acceptance/composer-uncategorized-test.js b/test/javascripts/acceptance/composer-uncategorized-test.js index 594cd9c88a3..26ff2f1148c 100644 --- a/test/javascripts/acceptance/composer-uncategorized-test.js +++ b/test/javascripts/acceptance/composer-uncategorized-test.js @@ -1,13 +1,16 @@ import selectKit from "helpers/select-kit-helper"; import { acceptance, updateCurrentUser } from "helpers/qunit-helpers"; -acceptance("Composer and uncategorized is not allowed", { - loggedIn: true, - settings: { - enable_whispers: true, - allow_uncategorized_topics: false, - }, -}); +acceptance( + "Composer disabled, uncategorized not allowed when any topic_template present", + { + loggedIn: true, + settings: { + enable_whispers: true, + allow_uncategorized_topics: false, + }, + } +); QUnit.test("Disable body until category is selected", async (assert) => { updateCurrentUser({ moderator: false, admin: false, trust_level: 1 }); @@ -47,3 +50,69 @@ QUnit.test("Disable body until category is selected", async (assert) => { "textarea is still enabled" ); }); + +acceptance( + "Composer enabled, uncategorized not allowed when topic_template not present", + { + loggedIn: true, + settings: { + allow_uncategorized_topics: false, + }, + site: { + categories: [ + { + id: 1, + name: "test won", + slug: "test-won", + topic_template: null, + }, + { + id: 2, + name: "test too", + slug: "test-too", + topic_template: "", + }, + { + id: 3, + name: "test free", + slug: "test-free", + topic_template: null, + }, + ], + }, + } +); + +QUnit.test( + "Enable composer/body if no topic templates present", + async (assert) => { + updateCurrentUser({ moderator: false, admin: false, trust_level: 1 }); + + await visit("/"); + await click("#create-topic"); + assert.ok(exists(".d-editor-input"), "the composer input is visible"); + assert.ok( + exists(".category-input .popup-tip.bad.hide"), + "category errors are hidden by default" + ); + assert.ok( + find(".d-editor-textarea-wrapper.disabled").length === 0, + "textarea is enabled" + ); + + await click("#reply-control button.create"); + assert.ok( + exists(".category-input .popup-tip.bad"), + "it shows the choose a category error" + ); + + const categoryChooser = selectKit(".category-chooser"); + await categoryChooser.expand(); + await categoryChooser.selectRowByValue(1); + + assert.ok( + !exists(".category-input .popup-tip.bad"), + "category error removed after selecting category" + ); + } +); diff --git a/test/javascripts/fixtures/site-fixtures.js b/test/javascripts/fixtures/site-fixtures.js index 48e2e18dc93..bdb8504b8f3 100644 --- a/test/javascripts/fixtures/site-fixtures.js +++ b/test/javascripts/fixtures/site-fixtures.js @@ -66,7 +66,8 @@ export default { notification_level: null, background_url: null, show_subcategory_list: false, - default_view: "latest" + default_view: "latest", + topic_template: "my topic template" }, { id: 10,