REFACTOR: admin-user (#7475)

REFACTOR: admin-user (#7475)

diff --git a/app/assets/javascripts/admin/models/admin-user.js.es6 b/app/assets/javascripts/admin/models/admin-user.js.es6
index 64c11cb..1f0738e 100644
--- a/app/assets/javascripts/admin/models/admin-user.js.es6
+++ b/app/assets/javascripts/admin/models/admin-user.js.es6
@@ -52,7 +52,7 @@ const AdminUser = Discourse.User.extend({
   canResetBounceScore: Ember.computed.gt("bounce_score", 0),
 
   resetBounceScore() {
-    return ajax(`/admin/users/${this.get("id")}/reset_bounce_score`, {
+    return ajax(`/admin/users/${this.id}/reset_bounce_score`, {
       type: "POST"
     }).then(() =>
       this.setProperties({
@@ -63,25 +63,24 @@ const AdminUser = Discourse.User.extend({
   },
 
   generateApiKey() {
-    const self = this;
-    return ajax("/admin/users/" + this.get("id") + "/generate_api_key", {
+    return ajax(`/admin/users/${this.id}/generate_api_key`, {
       type: "POST"
-    }).then(function(result) {
+    }).then(result => {
       const apiKey = ApiKey.create(result.api_key);
-      self.set("api_key", apiKey);
+      this.set("api_key", apiKey);
       return apiKey;
     });
   },
 
   groupAdded(added) {
-    return ajax("/admin/users/" + this.get("id") + "/groups", {
+    return ajax(`/admin/users/${this.id}/groups`, {
       type: "POST",
       data: { group_id: added.id }
     }).then(() => this.get("groups").pushObject(added));
   },
 
   groupRemoved(groupId) {
-    return ajax("/admin/users/" + this.get("id") + "/groups/" + groupId, {
+    return ajax(`/admin/users/${this.id}/groups/${groupId}`, {
       type: "DELETE"
     }).then(() => {
       this.set("groups.[]", this.get("groups").rejectBy("id", groupId));
@@ -92,80 +91,84 @@ const AdminUser = Discourse.User.extend({
   },
 
   revokeApiKey() {
-    return ajax("/admin/users/" + this.get("id") + "/revoke_api_key", {
+    return ajax(`/admin/users/${this.id}/revoke_api_key`, {
       type: "DELETE"
     }).then(() => this.set("api_key", null));
   },
 
   deleteAllPosts() {
     let deletedPosts = 0;
-    const user = this,
-      message = I18n.messageFormat("admin.user.delete_all_posts_confirm_MF", {
+    const user = this;
+    const message = I18n.messageFormat(
+      "admin.user.delete_all_posts_confirm_MF",
+      {
         POSTS: user.get("post_count"),
         TOPICS: user.get("topic_count")
-      }),
-      buttons = [
-        {
-          label: I18n.t("composer.cancel"),
-          class: "d-modal-cancel",
-          link: true
-        },
-        {
-          label:
-            `${iconHTML("exclamation-triangle")} ` +
-            I18n.t("admin.user.delete_all_posts"),
-          class: "btn btn-danger",
-          callback: () => {
-            openProgressModal();
+      }
+    );
+    const buttons = [
+      {
+        label: I18n.t("composer.cancel"),
+        class: "d-modal-cancel",
+        link: true
+      },
+      {
+        label:
+          `${iconHTML("exclamation-triangle")} ` +
+          I18n.t("admin.user.delete_all_posts"),
+        class: "btn btn-danger",
+        callback: () => {
+          openProgressModal();
+          performDelete();
+        }
+      }
+    ];
+    const openProgressModal = () => {
+      bootbox.dialog(
+        `<p>${I18n.t(
+          "admin.user.delete_posts_progress"
+        )}</p><div class='progress-bar'><span></span></div>`,
+        [],
+        { classes: "delete-posts-progress" }
+      );
+    };
+    const performDelete = () => {
+      let deletedPercentage = 0;
+      return ajax(`/admin/users/${user.get("id")}/delete_posts_batch`, {
+        type: "PUT"
+      })
+        .then(({ posts_deleted }) => {
+          if (posts_deleted === 0) {
+            user.set("post_count", 0);
+            bootbox.hideAll();
+          } else {
+            deletedPosts += posts_deleted;
+            deletedPercentage = Math.floor(
+              (deletedPosts * 100) / user.get("post_count")
+            );
+            $(".delete-posts-progress .progress-bar > span").css({
+              width: `${deletedPercentage}%`
+            });
             performDelete();
           }
-        }
-      ],
-      openProgressModal = () => {
-        bootbox.dialog(
-          `<p>${I18n.t(
-            "admin.user.delete_posts_progress"
-          )}</p><div class='progress-bar'><span></span></div>`,
-          [],
-          { classes: "delete-posts-progress" }
-        );
-      },
-      performDelete = () => {
-        let deletedPercentage = 0;
-        return ajax(`/admin/users/${user.get("id")}/delete_posts_batch`, {
-          type: "PUT"
         })
-          .then(({ posts_deleted }) => {
-            if (posts_deleted === 0) {
-              user.set("post_count", 0);
-              bootbox.hideAll();
-            } else {
-              deletedPosts += posts_deleted;
-              deletedPercentage = Math.floor(
-                (deletedPosts * 100) / user.get("post_count")
-              );
-              $(".delete-posts-progress .progress-bar > span").css({
-                width: `${deletedPercentage}%`
-              });
-              performDelete();
-            }
-          })
-          .catch(e => {
-            bootbox.hideAll();
-            let error;
-            AdminUser.find(user.get("id")).then(u => user.setProperties(u));
-            if (e.responseJSON && e.responseJSON.errors) {
-              error = e.responseJSON.errors[0];
-            }
-            error = error || I18n.t("admin.user.delete_posts_failed");
-            bootbox.alert(error);
-          });
-      };
+        .catch(e => {
+          bootbox.hideAll();
+          let error;
+          AdminUser.find(user.get("id")).then(u => user.setProperties(u));
+          if (e.responseJSON && e.responseJSON.errors) {
+            error = e.responseJSON.errors[0];
+          }
+          error = error || I18n.t("admin.user.delete_posts_failed");
+          bootbox.alert(error);
+        });
+    };
+
     bootbox.dialog(message, buttons, { classes: "delete-all-posts" });
   },
 
   revokeAdmin() {
-    return ajax(`/admin/users/${this.get("id")}/revoke_admin`, {
+    return ajax(`/admin/users/${this.id}/revoke_admin`, {
       type: "PUT"
     }).then(() => {
       this.setProperties({
@@ -177,7 +180,7 @@ const AdminUser = Discourse.User.extend({
   },
 
   grantAdmin() {
-    return ajax(`/admin/users/${this.get("id")}/grant_admin`, {
+    return ajax(`/admin/users/${this.id}/grant_admin`, {
       type: "PUT"
     })
       .then(() => {
@@ -187,12 +190,11 @@ const AdminUser = Discourse.User.extend({
   },
 
   revokeModeration() {
-    const self = this;
-    return ajax("/admin/users/" + this.get("id") + "/revoke_moderation", {
+    return ajax(`/admin/users/${this.id}/revoke_moderation`, {
       type: "PUT"
     })
-      .then(function() {
-        self.setProperties({
+      .then(() => {
+        this.setProperties({
           moderator: false,
           can_grant_moderation: true,
           can_revoke_moderation: false
@@ -202,12 +204,11 @@ const AdminUser = Discourse.User.extend({
   },
 
   grantModeration() {
-    const self = this;
-    return ajax("/admin/users/" + this.get("id") + "/grant_moderation", {
+    return ajax(`/admin/users/${this.id}/grant_moderation`, {
       type: "PUT"
     })
-      .then(function() {
-        self.setProperties({
+      .then(() => {
+        this.setProperties({
           moderator: true,
           can_grant_moderation: false,
           can_revoke_moderation: true
@@ -217,7 +218,7 @@ const AdminUser = Discourse.User.extend({
   },
 
   disableSecondFactor() {
-    return ajax(`/admin/users/${this.get("id")}/disable_second_factor`, {
+    return ajax(`/admin/users/${this.id}/disable_second_factor`, {
       type: "PUT"
     })
       .then(() => {
@@ -227,11 +228,10 @@ const AdminUser = Discourse.User.extend({
   },
 
   approve() {
-    const self = this;
-    return ajax("/admin/users/" + this.get("id") + "/approve", {
+    return ajax(`/admin/users/${this.id}/approve`, {

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

GitHub sha: 4bbb34f7

1 Like