FEATURE: add category banner for why a user cannot post (#9576)

FEATURE: add category banner for why a user cannot post (#9576)

  • FEATURE: add category banner for why a user cannot post

Adds a category banner for why a user is unable to post in a category.

Also adds an extra alert for the user when a user is unable to create a topic in a category and they still try and click on the disabled-looking new topic button.

diff --git a/app/assets/javascripts/discourse/app/components/category-read-only-banner.js b/app/assets/javascripts/discourse/app/components/category-read-only-banner.js
new file mode 100644
index 0000000..f1cd4a0
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/components/category-read-only-banner.js
@@ -0,0 +1,11 @@
+import Component from "@ember/component";
+import discourseComputed from "discourse-common/utils/decorators";
+import { and } from "@ember/object/computed";
+
+export default Component.extend({
+  @discourseComputed
+  user() {
+    return this.currentUser;
+  },
+  shouldShow: and("category.read_only_banner", "readOnly", "user")
+});
diff --git a/app/assets/javascripts/discourse/app/components/create-topic-button.js b/app/assets/javascripts/discourse/app/components/create-topic-button.js
index b2ab306..9642446 100644
--- a/app/assets/javascripts/discourse/app/components/create-topic-button.js
+++ b/app/assets/javascripts/discourse/app/components/create-topic-button.js
@@ -1,5 +1,6 @@
 import Component from "@ember/component";
 export default Component.extend({
   tagName: "",
-  label: "topic.create"
+  label: "topic.create",
+  btnClass: "btn-default"
 });
diff --git a/app/assets/javascripts/discourse/app/components/d-navigation.js b/app/assets/javascripts/discourse/app/components/d-navigation.js
index df6dc3c..bb862fd 100644
--- a/app/assets/javascripts/discourse/app/components/d-navigation.js
+++ b/app/assets/javascripts/discourse/app/components/d-navigation.js
@@ -15,6 +15,38 @@ export default Component.extend(FilterModeMixin, {
     return category && this.currentUser;
   },
 
+  @discourseComputed("category", "createTopicDisabled")
+  categoryReadOnlyBanner(category, createTopicDisabled) {
+    if (category && this.currentUser && createTopicDisabled) {
+      return category.read_only_banner;
+    }
+  },
+
+  @discourseComputed(
+    "createTopicDisabled",
+    "hasDraft",
+    "categoryReadOnlyBanner"
+  )
+  createTopicButtonDisabled(
+    createTopicDisabled,
+    hasDraft,
+    categoryReadOnlyBanner
+  ) {
+    if (categoryReadOnlyBanner && !hasDraft) {
+      return false;
+    }
+    return createTopicDisabled;
+  },
+
+  @discourseComputed("categoryReadOnlyBanner", "hasDraft")
+  createTopicClass(categoryReadOnlyBanner, hasDraft) {
+    if (categoryReadOnlyBanner && !hasDraft) {
+      return "btn-default disabled";
+    } else {
+      return "btn-default";
+    }
+  },
+
   @discourseComputed()
   categories() {
     return this.site.get("categoriesList");
@@ -65,6 +97,14 @@ export default Component.extend(FilterModeMixin, {
           this.reorderCategories();
           break;
       }
+    },
+
+    clickCreateTopicButton() {
+      if (this.categoryReadOnlyBanner && !this.hasDraft) {
+        bootbox.alert(this.categoryReadOnlyBanner);
+      } else {
+        this.createTopic();
+      }
     }
   }
 });
diff --git a/app/assets/javascripts/discourse/app/models/category.js b/app/assets/javascripts/discourse/app/models/category.js
index d765f84..7d2b590 100644
--- a/app/assets/javascripts/discourse/app/models/category.js
+++ b/app/assets/javascripts/discourse/app/models/category.js
@@ -187,7 +187,8 @@ const Category = RestModel.extend({
           "navigate_to_first_post_after_read"
         ),
         search_priority: this.search_priority,
-        reviewable_by_group_name: this.reviewable_by_group_name
+        reviewable_by_group_name: this.reviewable_by_group_name,
+        read_only_banner: this.read_only_banner
       },
       type: id ? "PUT" : "POST"
     });
diff --git a/app/assets/javascripts/discourse/app/templates/components/category-read-only-banner.hbs b/app/assets/javascripts/discourse/app/templates/components/category-read-only-banner.hbs
new file mode 100644
index 0000000..4c00dc8
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/templates/components/category-read-only-banner.hbs
@@ -0,0 +1,7 @@
+{{#if shouldShow}}
+  <div class="row">
+    <div class="alert alert-info category-read-only-banner">
+      {{category.read_only_banner}}
+    </div>
+  </div>
+{{/if}}
diff --git a/app/assets/javascripts/discourse/app/templates/components/create-topic-button.hbs b/app/assets/javascripts/discourse/app/templates/components/create-topic-button.hbs
index 3a0e3eb..76524d1 100644
--- a/app/assets/javascripts/discourse/app/templates/components/create-topic-button.hbs
+++ b/app/assets/javascripts/discourse/app/templates/components/create-topic-button.hbs
@@ -1,6 +1,6 @@
 {{#if canCreateTopic}}
   {{d-button
-    class="btn-default"
+    class=btnClass
     id="create-topic"
     action=action
     icon="plus"
diff --git a/app/assets/javascripts/discourse/app/templates/components/d-navigation.hbs b/app/assets/javascripts/discourse/app/templates/components/d-navigation.hbs
index 6a0d8e3..e9e0b9a 100644
--- a/app/assets/javascripts/discourse/app/templates/components/d-navigation.hbs
+++ b/app/assets/javascripts/discourse/app/templates/components/d-navigation.hbs
@@ -25,9 +25,10 @@
 
 {{create-topic-button
   canCreateTopic=canCreateTopic
-  action=createTopic
-  disabled=createTopicDisabled
+  action=(action "clickCreateTopicButton")
+  disabled=createTopicButtonDisabled
   label=createTopicLabel
+  btnClass=createTopicClass
 }}
 
 {{#if showCategoryEdit}}
diff --git a/app/assets/javascripts/discourse/app/templates/components/edit-category-settings.hbs b/app/assets/javascripts/discourse/app/templates/components/edit-category-settings.hbs
index f7b4981..ab17a30 100644
--- a/app/assets/javascripts/discourse/app/templates/components/edit-category-settings.hbs
+++ b/app/assets/javascripts/discourse/app/templates/components/edit-category-settings.hbs
@@ -221,6 +221,20 @@
       }}
     </section>
   {{/if}}
+
+  <section class="field category-read-only-banner">
+    <label for="category-read-only-banner">
+      {{i18n "category.read_only_banner"}}
+    </label>
+    {{text-field
+        valueProperty="value"
+        id="read-only-message"
+        value=category.read_only_banner
+        options=(hash
+        placementStrategy="absolute"
+        )
+    }}
+  </section>
 </section>
 
 <section>
diff --git a/app/assets/javascripts/discourse/app/templates/discovery.hbs b/app/assets/javascripts/discourse/app/templates/discovery.hbs
index 1c8f879..ddec84d 100644
--- a/app/assets/javascripts/discourse/app/templates/discovery.hbs
+++ b/app/assets/javascripts/discourse/app/templates/discovery.hbs
@@ -3,6 +3,7 @@
 {{else}}
   <div class="container">
     {{discourse-banner user=currentUser banner=site.banner}}
+    {{category-read-only-banner category=category readOnly=navigationCategory.cannotCreateTopicOnCategory}}
   </div>
 
   <div class="list-controls">
diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb
index acb39e7..770275a 100644
--- a/app/controllers/categories_controller.rb
+++ b/app/controllers/categories_controller.rb
@@ -328,6 +328,7 @@ class CategoriesController < ApplicationController
         :allow_global_tags,
         :required_tag_group_name,
         :min_tags_from_required_group,
+        :read_only_banner,
         custom_fields: [params[:custom_fields].try(:keys)],
         permissions: [*p.try(:keys)],
         allowed_tags: [],
diff --git a/app/models/category.rb b/app/models/category.rb
index 2651602..bbf1e9b 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -978,6 +978,7 @@ end
 #  reviewable_by_group_id            :integer
 #  required_tag_group_id             :integer
 #  min_tags_from_required_group      :integer          default(1), not null
+#  read_only_banner                  :string
 #
 # Indexes
 #
diff --git a/app/serializers/site_category_serializer.rb b/app/serializers/site_category_serializer.rb
index 31549fd..89868e9 100644
--- a/app/serializers/site_category_serializer.rb
+++ b/app/serializers/site_category_serializer.rb
@@ -6,7 +6,8 @@ class SiteCategorySerializer < BasicCategorySerializer

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

GitHub sha: 2cb9e85d

This commit appears in #9576 which was approved by eviltrout. It was merged by featheredtoast.