FIX: allow editing in composer before a category is selected (#10724)

FIX: allow editing in composer before a category is selected (#10724)

  • 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
diff --git a/app/assets/javascripts/discourse/app/models/composer.js b/app/assets/javascripts/discourse/app/models/composer.js
index 7a7054b..44ff23a 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 594cd9c..26ff2f1 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 48e2e18..bdb8504 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,

GitHub sha: 09a97363

This commit appears in #10724 which was approved by eviltrout. It was merged by jbrw.