FEATURE: add dismiss unread topics button when filtered by tag. (#10547)

FEATURE: add dismiss unread topics button when filtered by tag. (#10547)

diff --git a/app/assets/javascripts/discourse/app/controllers/tags-show.js b/app/assets/javascripts/discourse/app/controllers/tags-show.js
index 42d6bce..a2eb9af 100644
--- a/app/assets/javascripts/discourse/app/controllers/tags-show.js
+++ b/app/assets/javascripts/discourse/app/controllers/tags-show.js
@@ -7,6 +7,7 @@ import NavItem from "discourse/models/nav-item";
 import FilterModeMixin from "discourse/mixins/filter-mode";
 import { queryParams } from "discourse/controllers/discovery-sortable";
 import bootbox from "bootbox";
+import showModal from "discourse/lib/show-modal";
 
 export default Controller.extend(BulkTopicSelection, FilterModeMixin, {
   application: controller(),
@@ -88,7 +89,7 @@ export default Controller.extend(BulkTopicSelection, FilterModeMixin, {
 
   @discourseComputed("navMode", "list.topics.length", "loading")
   footerMessage(navMode, listTopicsLength, loading) {
-    if (loading || listTopicsLength !== 0) {
+    if (loading) {
       return;
     }
 
@@ -97,13 +98,29 @@ export default Controller.extend(BulkTopicSelection, FilterModeMixin, {
         tag: this.get("tag.id")
       });
     } else {
-      return I18n.t(`tagging.topics.bottom.${navMode}`, {
+      return I18n.t(`topics.bottom.tag`, {
         tag: this.get("tag.id")
       });
     }
   },
 
+  isFilterPage: function(filter, filterType) {
+    if (!filter) {
+      return false;
+    }
+    return filter.match(new RegExp(filterType + "$", "gi")) ? true : false;
+  },
+
+  @discourseComputed("list.filter", "list.topics.length")
+  showDismissRead(filter, topicsLength) {
+    return this.isFilterPage(filter, "unread") && topicsLength > 0;
+  },
+
   actions: {
+    dismissReadPosts() {
+      showModal("dismiss-read", { title: "topics.bulk.dismiss_read" });
+    },
+
     changeSort(order) {
       if (order === this.order) {
         this.toggleProperty("ascending");
@@ -122,7 +139,9 @@ export default Controller.extend(BulkTopicSelection, FilterModeMixin, {
 
     refresh() {
       return this.store
-        .findFiltered("topicList", { filter: "tags/" + this.get("tag.id") })
+        .findFiltered("topicList", {
+          filter: this.get("list.filter")
+        })
         .then(list => {
           this.set("list", list);
           this.resetSelected();
diff --git a/app/assets/javascripts/discourse/app/mixins/bulk-topic-selection.js b/app/assets/javascripts/discourse/app/mixins/bulk-topic-selection.js
index cc2dfc1..e1c6799 100644
--- a/app/assets/javascripts/discourse/app/mixins/bulk-topic-selection.js
+++ b/app/assets/javascripts/discourse/app/mixins/bulk-topic-selection.js
@@ -21,7 +21,7 @@ export default Mixin.create({
       this.selected.clear();
     },
 
-    dismissRead(operationType, categoryOptions) {
+    dismissRead(operationType, options) {
       let operation;
       if (operationType === "posts") {
         operation = { type: "dismiss_posts" };
@@ -36,12 +36,7 @@ export default Mixin.create({
       if (this.selected.length > 0) {
         promise = Topic.bulkOperation(this.selected, operation);
       } else {
-        promise = Topic.bulkOperationByFilter(
-          "unread",
-          operation,
-          this.get("category.id"),
-          categoryOptions
-        );
+        promise = Topic.bulkOperationByFilter("unread", operation, options);
       }
 
       promise.then(result => {
diff --git a/app/assets/javascripts/discourse/app/models/topic.js b/app/assets/javascripts/discourse/app/models/topic.js
index 145fd2a..5cbaf14 100644
--- a/app/assets/javascripts/discourse/app/models/topic.js
+++ b/app/assets/javascripts/discourse/app/models/topic.js
@@ -800,14 +800,21 @@ Topic.reopenClass({
     });
   },
 
-  bulkOperationByFilter(filter, operation, categoryId, options) {
+  bulkOperationByFilter(filter, operation, options) {
     let data = { filter, operation };
 
-    if (options && options.includeSubcategories) {
-      data.include_subcategories = true;
+    if (options) {
+      if (options.categoryId) {
+        data.category_id = options.categoryId;
+      }
+      if (options.includeSubcategories) {
+        data.include_subcategories = true;
+      }
+      if (options.tagName) {
+        data.tag_name = options.tagName;
+      }
     }
 
-    if (categoryId) data.category_id = categoryId;
     return ajax("/topics/bulk", {
       type: "PUT",
       data
diff --git a/app/assets/javascripts/discourse/app/routes/discovery.js b/app/assets/javascripts/discourse/app/routes/discovery.js
index 43f030d..32bf3b6 100644
--- a/app/assets/javascripts/discourse/app/routes/discovery.js
+++ b/app/assets/javascripts/discourse/app/routes/discovery.js
@@ -75,6 +75,7 @@ export default DiscourseRoute.extend(OpenComposer, {
     dismissRead(operationType) {
       const controller = this.controllerFor("discovery/topics");
       controller.send("dismissRead", operationType, {
+        categoryId: controller.get("category.id"),
         includeSubcategories: !controller.noSubcategories
       });
     }
diff --git a/app/assets/javascripts/discourse/app/routes/tags-show.js b/app/assets/javascripts/discourse/app/routes/tags-show.js
index 1b26424..fdbd0ba 100644
--- a/app/assets/javascripts/discourse/app/routes/tags-show.js
+++ b/app/assets/javascripts/discourse/app/routes/tags-show.js
@@ -6,7 +6,10 @@ import {
   filterQueryParams,
   findTopicList
 } from "discourse/routes/build-topic-route";
-import { queryParams } from "discourse/controllers/discovery-sortable";
+import {
+  resetParams,
+  queryParams
+} from "discourse/controllers/discovery-sortable";
 import PermissionType from "discourse/models/permission-type";
 import Category from "discourse/models/category";
 import FilterModeMixin from "discourse/mixins/filter-mode";
@@ -207,6 +210,30 @@ export default DiscourseRoute.extend(FilterModeMixin, {
       }
     },
 
+    dismissReadTopics(dismissTopics) {
+      const operationType = dismissTopics ? "topics" : "posts";
+      this.send("dismissRead", operationType);
+    },
+
+    dismissRead(operationType) {
+      const controller = this.controllerFor("tags-show");
+      let options = {
+        tagName: controller.get("tag.id")
+      };
+      const categoryId = controller.get("category.id");
+
+      if (categoryId) {
+        options = $.extend({}, options, {
+          categoryId: categoryId,
+          includeSubcategories: !controller.noSubcategories
+        });
+      }
+
+      controller.send("dismissRead", operationType, options);
+    },
+
+    resetParams,
+
     didTransition() {
       this.controllerFor("tags.show")._showFooter();
       return true;
diff --git a/app/assets/javascripts/discourse/app/templates/tags/show.hbs b/app/assets/javascripts/discourse/app/templates/tags/show.hbs
index e30edee..dded4a1 100644
--- a/app/assets/javascripts/discourse/app/templates/tags/show.hbs
+++ b/app/assets/javascripts/discourse/app/templates/tags/show.hbs
@@ -82,13 +82,21 @@
                   changeSort=(action "changeSort")
                 }}
               {{/discovery-topics-list}}
-            {{else}}
-              <footer class="topic-list-bottom">
-                <h3>
-                  {{footerMessage}}{{#link-to "discovery.categories"}} {{i18n "topic.browse_all_categories"}}{{/link-to}} {{i18n "or"}} {{#link-to "discovery.latest"}}{{i18n "topic.view_latest_topics"}}{{/link-to}}.
-                </h3>
-              </footer>
             {{/if}}
+            <footer class="topic-list-bottom">
+              {{#if showDismissRead}}
+                {{d-button
+                  class="btn-default dismiss-read"
+                  id="dismiss-topics"
+                  action=(action "dismissReadPosts")
+                  title="topics.bulk.dismiss_tooltip"
+                  label="topics.bulk.dismiss_button"}}
+              {{/if}}
+
+              {{#footer-message education=footerEducation message=footerMessage}}

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

GitHub sha: a8502ae1

This commit appears in #10547 which was approved by eviltrout. It was merged by vinothkannans.

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/button-to-dismiss-unread-topics-when-list-is-filtered-by-tag/159992/4