FIX: ensures minimum tags logic is correct and shared (#14723)

FIX: ensures minimum tags logic is correct and shared (#14723)

Also fixes a bug where select-kit was not updating noneItem in multi-selects.

diff --git a/app/assets/javascripts/discourse/app/controllers/topic.js b/app/assets/javascripts/discourse/app/controllers/topic.js
index c5870ca..9a2eb38 100644
--- a/app/assets/javascripts/discourse/app/controllers/topic.js
+++ b/app/assets/javascripts/discourse/app/controllers/topic.js
@@ -1,3 +1,4 @@
+import Category from "discourse/models/category";
 import Controller, { inject as controller } from "@ember/controller";
 import DiscourseURL, { userPath } from "discourse/lib/url";
 import { alias, and, not, or } from "@ember/object/computed";
@@ -212,11 +213,9 @@ export default Controller.extend(bufferedProperty("model"), {
     );
   },
 
-  @discourseComputed("model.category")
-  minimumRequiredTags(category) {
-    return category && category.minimum_required_tags > 0
-      ? category.minimum_required_tags
-      : null;
+  @discourseComputed("buffered.category_id")
+  minimumRequiredTags(categoryId) {
+    return Category.findById(categoryId)?.minimumRequiredTags || 0;
   },
 
   _removeDeleteOnOwnerReplyBookmarks() {
diff --git a/app/assets/javascripts/discourse/app/models/category.js b/app/assets/javascripts/discourse/app/models/category.js
index 17a377e..1636d8d 100644
--- a/app/assets/javascripts/discourse/app/models/category.js
+++ b/app/assets/javascripts/discourse/app/models/category.js
@@ -42,6 +42,19 @@ const Category = RestModel.extend({
     }
   },
 
+  @discourseComputed(
+    "required_tag_groups",
+    "min_tags_from_required_group",
+    "minimum_required_tags"
+  )
+  minimumRequiredTags() {
+    if (this.required_tag_groups) {
+      return this.min_tags_from_required_group;
+    } else {
+      return this.minimum_required_tags > 0 ? this.minimum_required_tags : null;
+    }
+  },
+
   @discourseComputed
   availablePermissions() {
     return [
diff --git a/app/assets/javascripts/discourse/app/models/composer.js b/app/assets/javascripts/discourse/app/models/composer.js
index 4db89cc..8c3af70 100644
--- a/app/assets/javascripts/discourse/app/models/composer.js
+++ b/app/assets/javascripts/discourse/app/models/composer.js
@@ -157,26 +157,9 @@ const Composer = RestModel.extend({
     return categoryId ? this.site.categories.findBy("id", categoryId) : null;
   },
 
-  @discourseComputed("category")
-  minimumRequiredTags(category) {
-    if (category) {
-      if (category.required_tag_groups) {
-        return category.min_tags_from_required_group;
-      } else {
-        return category.minimum_required_tags > 0
-          ? category.minimum_required_tags
-          : null;
-      }
-    }
-
-    return null;
-  },
-
-  @discourseComputed("category")
-  requiredTagGroups(category) {
-    return category && category.required_tag_groups
-      ? category.required_tag_groups
-      : null;
+  @discourseComputed("category.minimumRequiredTags")
+  minimumRequiredTags(minimumRequiredTags) {
+    return minimumRequiredTags || 0;
   },
 
   creatingTopic: equal("action", CREATE_TOPIC),
diff --git a/app/assets/javascripts/discourse/app/templates/composer.hbs b/app/assets/javascripts/discourse/app/templates/composer.hbs
index 532ea5e..4d6a4c3 100644
--- a/app/assets/javascripts/discourse/app/templates/composer.hbs
+++ b/app/assets/javascripts/discourse/app/templates/composer.hbs
@@ -99,7 +99,6 @@
                     options=(hash
                       categoryId=model.categoryId
                       minimum=model.minimumRequiredTags
-                      requiredTagGroups=model.requiredTagGroups
                     )
                   }}
                   {{popup-input-tip validation=tagValidation}}
diff --git a/app/assets/javascripts/discourse/tests/unit/models/category-test.js b/app/assets/javascripts/discourse/tests/unit/models/category-test.js
index c72206e..18c2f7c 100644
--- a/app/assets/javascripts/discourse/tests/unit/models/category-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/models/category-test.js
@@ -221,6 +221,50 @@ module("Unit | Model | category", function () {
     assert.deepEqual(Category.findBySlugPathWithID("foo/baz/3"), baz);
   });
 
+  test("minimumRequiredTags", function (assert) {
+    const store = createStore();
+
+    let foo = store.createRecord("category", {
+      id: 1,
+      slug: "foo",
+      required_tag_groups: ["bar"],
+      min_tags_from_required_group: 2,
+    });
+
+    assert.equal(foo.minimumRequiredTags, 2);
+
+    foo = store.createRecord("category", {
+      id: 2,
+      slug: "foo",
+    });
+
+    assert.equal(foo.minimumRequiredTags, null);
+
+    foo = store.createRecord("category", {
+      id: 3,
+      slug: "foo",
+      minimum_required_tags: 0,
+    });
+
+    assert.equal(foo.minimumRequiredTags, null);
+
+    foo = store.createRecord("category", {
+      id: 4,
+      slug: "foo",
+      minimum_required_tags: 2,
+    });
+
+    assert.equal(foo.minimumRequiredTags, 2);
+
+    foo = store.createRecord("category", {
+      id: 5,
+      slug: "foo",
+      min_tags_from_required_group: 2,
+    });
+
+    assert.equal(foo.minimumRequiredTags, null);
+  });
+
   test("search with category name", function (assert) {
     const store = createStore(),
       category1 = store.createRecord("category", {
diff --git a/app/assets/javascripts/select-kit/addon/components/category-chooser.js b/app/assets/javascripts/select-kit/addon/components/category-chooser.js
index a81cf0c..2bca205 100644
--- a/app/assets/javascripts/select-kit/addon/components/category-chooser.js
+++ b/app/assets/javascripts/select-kit/addon/components/category-chooser.js
@@ -20,6 +20,7 @@ export default ComboBoxComponent.extend({
     permissionType: PermissionType.FULL,
     excludeCategoryId: null,
     scopedCategoryId: null,
+    prioritizedCategoryId: null,
   },
 
   modifyComponentForRow() {
diff --git a/app/assets/javascripts/select-kit/addon/components/dropdown-select-box.js b/app/assets/javascripts/select-kit/addon/components/dropdown-select-box.js
index 594ba93..1d57178 100644
--- a/app/assets/javascripts/select-kit/addon/components/dropdown-select-box.js
+++ b/app/assets/javascripts/select-kit/addon/components/dropdown-select-box.js
@@ -12,6 +12,7 @@ export default SingleSelectComponent.extend({
     caretUpIcon: "caret-up",
     caretDownIcon: "caret-down",
     showCaret: false,
+    customStyle: null,
   },
 
   modifyComponentForRow() {
diff --git a/app/assets/javascripts/select-kit/addon/components/mini-tag-chooser.js b/app/assets/javascripts/select-kit/addon/components/mini-tag-chooser.js
index 59bc2a9..0025efd 100644
--- a/app/assets/javascripts/select-kit/addon/components/mini-tag-chooser.js
+++ b/app/assets/javascripts/select-kit/addon/components/mini-tag-chooser.js
@@ -23,10 +23,8 @@ export default MultiSelectComponent.extend(TagsMixin, {
     termMatchesForbidden: false,
     categoryId: null,
     everyTag: false,
-    none: "tagging.choose_for_topic",
     closeOnChange: false,
-    maximum: "maximumSelectedTags",
-    minimum: "minimumSelectedTags",
+    maximum: "maxTagsPerTopic",
     autoInsertNoneItem: false,
   },
 
@@ -38,31 +36,20 @@ export default MultiSelectComponent.extend(TagsMixin, {
     return "tag-row";
   },
 
-  allowAnyTag: or("allowCreate", "site.can_create_tag"),
-
-  maximumSelectedTags: computed(function () {
-    return parseInt(
-      this.options.limit ||
-        this.selectKit.options.maximum ||
-        this.maxTagsPerTopic,
-      10
-    );
-  }),
-
-  minimumSelectedTags: computed(function () {
-    if (
-      this.selectKit.options.minimum ||
-      this.selectKit.options.requiredTagGroups
-    ) {
-      const minimum = parseInt(this.selectKit.options.minimum, 10);
-      if (minimum > 0) {
-        return this.defaultItem(
-          null,
-          I18n.t("select_kit.min_content_not_reached", { count: minimum })
-        );
-      }
+  modifyNoSelection() {
+    if (this.selectKit.options.minimum > 0) {
+      return this.defaultItem(
+        null,
+        I18n.t("tagging.choose_for_topic_required", {

[... diff too long, it was truncated ...]

GitHub sha: 904d509cce1d699cbea939f372d222d61aeec03d

This commit appears in #14723 which was approved by eviltrout. It was merged by jjaffeux.