DEV: don't swallow a promise from group.findMembers method and switch to using async/await (#13888)

DEV: don’t swallow a promise from group.findMembers method and switch to using async/await (#13888)

diff --git a/app/assets/javascripts/discourse/app/components/group-card-contents.js b/app/assets/javascripts/discourse/app/components/group-card-contents.js
index b53ae55..dc16eef 100644
--- a/app/assets/javascripts/discourse/app/components/group-card-contents.js
+++ b/app/assets/javascripts/discourse/app/components/group-card-contents.js
@@ -51,7 +51,7 @@ export default Component.extend(CardContentsBase, CleansUp, {
         }
         return group.can_see_members &&
           group.members.length < maxMembersToDisplay
-          ? group.findMembers({ limit: maxMembersToDisplay }, true)
+          ? group.reloadMembers({ limit: maxMembersToDisplay }, true)
           : Promise.resolve();
       })
       .catch(() => this._close())
diff --git a/app/assets/javascripts/discourse/app/controllers/group-index.js b/app/assets/javascripts/discourse/app/controllers/group-index.js
index a15d1f8..570a555 100644
--- a/app/assets/javascripts/discourse/app/controllers/group-index.js
+++ b/app/assets/javascripts/discourse/app/controllers/group-index.js
@@ -35,10 +35,10 @@ export default Controller.extend({
 
   @observes("order", "asc", "filter")
   _filtersChanged() {
-    this.findMembers(true);
+    this.reloadMembers(true);
   },
 
-  findMembers(refresh) {
+  reloadMembers(refresh) {
     if (this.loading || !this.model) {
       return;
     }
@@ -49,7 +49,7 @@ export default Controller.extend({
     }
 
     this.set("loading", true);
-    this.model.findMembers(this.memberParams, refresh).finally(() => {
+    this.model.reloadMembers(this.memberParams, refresh).finally(() => {
       this.setProperties({
         "application.showFooter":
           this.model.members.length >= this.model.user_count,
@@ -96,7 +96,7 @@ export default Controller.extend({
 
   @action
   loadMore() {
-    this.findMembers();
+    this.reloadMembers();
   },
 
   @action
@@ -139,7 +139,7 @@ export default Controller.extend({
           type: "DELETE",
           data: { user_ids: selection.map((u) => u.id).join(",") },
         }).then(() => {
-          this.model.findMembers(this.memberParams, true);
+          this.model.reloadMembers(this.memberParams, true);
           this.set("isBulk", false);
         });
 
diff --git a/app/assets/javascripts/discourse/app/models/group.js b/app/assets/javascripts/discourse/app/models/group.js
index 75d91ea..2f597b4 100644
--- a/app/assets/javascripts/discourse/app/models/group.js
+++ b/app/assets/javascripts/discourse/app/models/group.js
@@ -34,9 +34,9 @@ const Group = RestModel.extend({
     return automatic ? "automatic" : "custom";
   },
 
-  findMembers(params, refresh) {
+  async reloadMembers(params, refresh) {
     if (isEmpty(this.name) || !this.can_see_members) {
-      return Promise.reject();
+      return;
     }
 
     if (refresh) {
@@ -48,25 +48,24 @@ const Group = RestModel.extend({
       params
     );
 
-    return Group.loadMembers(this.name, params).then((result) => {
-      const ownerIds = new Set();
-      result.owners.forEach((owner) => ownerIds.add(owner.id));
-
-      const members = refresh ? [] : this.members;
-      members.pushObjects(
-        result.members.map((member) => {
-          member.owner = ownerIds.has(member.id);
-          member.primary = member.primary_group_name === this.name;
-          return User.create(member);
-        })
-      );
+    const response = await Group.loadMembers(this.name, params);
+    const ownerIds = new Set();
+    response.owners.forEach((owner) => ownerIds.add(owner.id));
+
+    const members = refresh ? [] : this.members;
+    members.pushObjects(
+      response.members.map((member) => {
+        member.owner = ownerIds.has(member.id);
+        member.primary = member.primary_group_name === this.name;
+        return User.create(member);
+      })
+    );
 
-      this.setProperties({
-        members,
-        user_count: result.meta.total,
-        limit: result.meta.limit,
-        offset: result.meta.offset,
-      });
+    this.setProperties({
+      members,
+      user_count: response.meta.total,
+      limit: response.meta.limit,
+      offset: response.meta.offset,
     });
   },
 
@@ -100,62 +99,63 @@ const Group = RestModel.extend({
     });
   },
 
-  removeOwner(member) {
-    return ajax(`/admin/groups/${this.id}/owners.json`, {
+  async removeOwner(member) {
+    await ajax(`/admin/groups/${this.id}/owners.json`, {
       type: "DELETE",
       data: { user_id: member.id },
-    }).then(() => this.findMembers({}, true));
+    });
+    await this.reloadMembers({}, true);
   },
 
-  removeMember(member, params) {
-    return ajax(`/groups/${this.id}/members.json`, {
+  async removeMember(member, params) {
+    await ajax(`/groups/${this.id}/members.json`, {
       type: "DELETE",
       data: { user_id: member.id },
-    }).then(() => this.findMembers(params, true));
+    });
+    await this.reloadMembers(params, true);
   },
 
-  leave() {
-    return ajax(`/groups/${this.id}/leave.json`, {
+  async leave() {
+    await ajax(`/groups/${this.id}/leave.json`, {
       type: "DELETE",
-    }).then(() => this.findMembers({}, true));
+    });
+    await this.reloadMembers({}, true);
   },
 
-  addMembers(usernames, filter, notifyUsers, emails = []) {
-    return ajax(`/groups/${this.id}/members.json`, {
+  async addMembers(usernames, filter, notifyUsers, emails = []) {
+    const response = await ajax(`/groups/${this.id}/members.json`, {
       type: "PUT",
       data: { usernames, emails, notify_users: notifyUsers },
-    }).then((response) => {
-      if (filter) {
-        this._filterMembers(response);
-      } else {
-        this.findMembers();
-      }
     });
+    if (filter) {
+      await this._filterMembers(response.usernames);
+    } else {
+      await this.reloadMembers();
+    }
   },
 
-  join() {
-    return ajax(`/groups/${this.id}/join.json`, {
+  async join() {
+    await ajax(`/groups/${this.id}/join.json`, {
       type: "PUT",
-    }).then(() => {
-      this.findMembers({}, true);
     });
+    await this.reloadMembers({}, true);
   },
 
-  addOwners(usernames, filter, notifyUsers) {
-    return ajax(`/admin/groups/${this.id}/owners.json`, {
+  async addOwners(usernames, filter, notifyUsers) {
+    const response = await ajax(`/admin/groups/${this.id}/owners.json`, {
       type: "PUT",
       data: { group: { usernames, notify_users: notifyUsers } },
-    }).then((response) => {
-      if (filter) {
-        this._filterMembers(response);
-      } else {
-        this.findMembers({}, true);
-      }
     });
+
+    if (filter) {
+      await this._filterMembers(response.usernames);
+    } else {
+      await this.reloadMembers({}, true);
+    }
   },
 
-  _filterMembers(response) {
-    return this.findMembers({ filter: response.usernames.join(",") });
+  _filterMembers(usernames) {
+    return this.reloadMembers({ filter: usernames.join(",") });
   },
 
   @discourseComputed("display_name", "name")
@@ -291,19 +291,19 @@ const Group = RestModel.extend({
     return attrs;
   },
 
-  create() {
-    return ajax("/admin/groups", {
+  async create() {
+    const response = await ajax("/admin/groups", {
       type: "POST",
       data: { group: this.asJSON() },
-    }).then((resp) => {
-      this.setProperties({
-        id: resp.basic_group.id,
-        usernames: null,
-        ownerUsernames: null,
-      });
+    });
 
-      this.findMembers();
+    this.setProperties({
+      id: response.basic_group.id,
+      usernames: null,
+      ownerUsernames: null,
     });
+
+    await this.reloadMembers();
   },
 
   save(opts = {}) {
diff --git a/app/assets/javascripts/discourse/app/routes/group-index.js b/app/assets/javascripts/discourse/app/routes/group-index.js
index 77a4ac1..1a9ff92 100644
--- a/app/assets/javascripts/discourse/app/routes/group-index.js
+++ b/app/assets/javascripts/discourse/app/routes/group-index.js
@@ -20,7 +20,7 @@ export default DiscourseRoute.extend({
       showing: "members",
     });
 

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

GitHub sha: f0d048b42a3f47ab30947c6704a5ba1f4b55f3c3

This commit appears in #13888 which was approved by eviltrout. It was merged by AndrewPrigorshnev.