FIX: Refresh members and tab count when assignments change

FIX: Refresh members and tab count when assignments change

diff --git a/app/controllers/discourse_assign/assign_controller.rb b/app/controllers/discourse_assign/assign_controller.rb
index ec591f4..c0d4a9f 100644
--- a/app/controllers/discourse_assign/assign_controller.rb
+++ b/app/controllers/discourse_assign/assign_controller.rb
@@ -147,7 +147,12 @@ module DiscourseAssign
         .limit(limit)
         .offset(offset)
 
-      render json: { members: serialize_data(members, GroupUserAssignedSerializer) }
+      assignment_count = Topic.joins("JOIN topic_custom_fields tcf ON topics.id = tcf.topic_id AND tcf.name = 'assigned_to_id' AND tcf.value IS NOT NULL")
+        .where("tcf.value IN (SELECT group_users.user_id::varchar(255) FROM group_users WHERE (group_id IN (SELECT id FROM groups WHERE name = ?)))", group.name)
+        .where("topics.deleted_at IS NULL")
+        .count
+
+      render json: { members: serialize_data(members, GroupUserAssignedSerializer), "assignment_count" => assignment_count }
     end
 
     private
diff --git a/assets/javascripts/discourse-assign/controllers/group-assigned.js.es6 b/assets/javascripts/discourse-assign/controllers/group-assigned.js.es6
index 23df1b8..a8d8b16 100644
--- a/assets/javascripts/discourse-assign/controllers/group-assigned.js.es6
+++ b/assets/javascripts/discourse-assign/controllers/group-assigned.js.es6
@@ -1,6 +1,7 @@
 import { inject as service } from "@ember/service";
 import Controller, { inject as controller } from "@ember/controller";
 import { ajax } from "discourse/lib/ajax";
+import { observes } from "discourse-common/utils/decorators";
 
 export default Controller.extend({
   router: service(),
@@ -8,6 +9,11 @@ export default Controller.extend({
   loading: false,
   offset: 0,
 
+  @observes("model.assignment_count")
+  assignmentCountChanged() {
+    this.set("group.assignment_count", this.model.assignment_count);
+  },
+
   findMembers(refresh) {
     if (refresh) {
       this.set("members", this.model.members);
diff --git a/assets/javascripts/discourse-assign/routes/group-assigned-show.js.es6 b/assets/javascripts/discourse-assign/routes/group-assigned-show.js.es6
index 7280d81..f9c236b 100644
--- a/assets/javascripts/discourse-assign/routes/group-assigned-show.js.es6
+++ b/assets/javascripts/discourse-assign/routes/group-assigned-show.js.es6
@@ -39,11 +39,5 @@ export default DiscourseRoute.extend({
 
   renderTemplate() {
     this.render("group-topics-list");
-  },
-
-  actions: {
-    changeAssigned() {
-      this.refresh();
-    }
   }
 });
diff --git a/assets/javascripts/discourse-assign/routes/group-assigned.js.es6 b/assets/javascripts/discourse-assign/routes/group-assigned.js.es6
index b927027..6e38895 100644
--- a/assets/javascripts/discourse-assign/routes/group-assigned.js.es6
+++ b/assets/javascripts/discourse-assign/routes/group-assigned.js.es6
@@ -22,5 +22,11 @@ export default DiscourseRoute.extend({
     } else {
       this.transitionTo("group.assigned.show", "everyone");
     }
+  },
+
+  actions: {
+    changeAssigned() {
+      this.refresh();
+    }
   }
 });

GitHub sha: a844da76

1 Like

This commit appears in #92 which was merged by davidtaylorhq.

We’re trying to get rid of observers as they add an indirection and makes the code really hard to wrap our head around.

Any chances you could re-work this without using an observer?

1 Like