REFACTOR: minor changes to api-keys-new (#14435)

REFACTOR: minor changes to api-keys-new (#14435)

  • moves loading scopes to controller
  • avoids declaring array
  • simplify code
diff --git a/app/assets/javascripts/admin/addon/controllers/admin-api-keys-new.js b/app/assets/javascripts/admin/addon/controllers/admin-api-keys-new.js
index c903163..961c1a5 100644
--- a/app/assets/javascripts/admin/addon/controllers/admin-api-keys-new.js
+++ b/app/assets/javascripts/admin/addon/controllers/admin-api-keys-new.js
@@ -3,70 +3,83 @@ import I18n from "I18n";
 import discourseComputed from "discourse-common/utils/decorators";
 import { isBlank } from "@ember/utils";
 import { popupAjaxError } from "discourse/lib/ajax-error";
-import { get } from "@ember/object";
+import { action, get } from "@ember/object";
+import { equal } from "@ember/object/computed";
 import showModal from "discourse/lib/show-modal";
+import { ajax } from "discourse/lib/ajax";
 
 export default Controller.extend({
-  userModes: [
-    { id: "all", name: I18n.t("admin.api.all_users") },
-    { id: "single", name: I18n.t("admin.api.single_user") },
-  ],
+  userModes: null,
   useGlobalKey: false,
   scopes: null,
 
-  @discourseComputed("userMode")
-  showUserSelector(mode) {
-    return mode === "single";
+  init() {
+    this._super(...arguments);
+
+    this.set("userModes", [
+      { id: "all", name: I18n.t("admin.api.all_users") },
+      { id: "single", name: I18n.t("admin.api.single_user") },
+    ]);
+    this._loadScopes();
   },
 
-  @discourseComputed("model.description", "model.username", "userMode")
-  saveDisabled(description, username, userMode) {
-    if (isBlank(description)) {
+  showUserSelector: equal("userMode", "single"),
+
+  @discourseComputed("model.{description,username}", "showUserSelector")
+  saveDisabled(model, showUserSelector) {
+    if (isBlank(model.description)) {
       return true;
     }
-    if (userMode === "single" && isBlank(username)) {
+    if (showUserSelector && isBlank(model.username)) {
       return true;
     }
     return false;
   },
 
-  actions: {
-    updateUsername(selected) {
-      this.set("model.username", get(selected, "firstObject"));
-    },
+  @action
+  updateUsername(selected) {
+    this.set("model.username", get(selected, "firstObject"));
+  },
+
+  @action
+  changeUserMode(userMode) {
+    if (userMode === "all") {
+      this.model.set("username", null);
+    }
+    this.set("userMode", userMode);
+  },
 
-    changeUserMode(value) {
-      if (value === "all") {
-        this.model.set("username", null);
-      }
-      this.set("userMode", value);
-    },
+  @action
+  save() {
+    if (!this.useGlobalKey) {
+      const selectedScopes = Object.values(this.scopes)
+        .flat()
+        .filterBy("selected");
 
-    save() {
-      if (!this.useGlobalKey) {
-        const selectedScopes = Object.values(this.scopes)
-          .flat()
-          .filter((action) => {
-            return action.selected;
-          });
+      this.model.set("scopes", selectedScopes);
+    }
 
-        this.model.set("scopes", selectedScopes);
-      }
+    return this.model.save().catch(popupAjaxError);
+  },
 
-      this.model.save().catch(popupAjaxError);
-    },
+  @action
+  continue() {
+    this.transitionToRoute("adminApiKeys.show", this.model.id);
+  },
 
-    continue() {
-      this.transitionToRoute("adminApiKeys.show", this.model.id);
-    },
+  @action
+  showURLs(urls) {
+    return showModal("admin-api-key-urls", {
+      admin: true,
+      model: { urls },
+    });
+  },
 
-    showURLs(urls) {
-      return showModal("admin-api-key-urls", {
-        admin: true,
-        model: {
-          urls,
-        },
-      });
-    },
+  _loadScopes() {
+    return ajax("/admin/api/keys/scopes.json")
+      .then((data) => {
+        this.set("scopes", data.scopes);
+      })
+      .catch(popupAjaxError);
   },
 });
diff --git a/app/assets/javascripts/admin/addon/routes/admin-api-keys-new.js b/app/assets/javascripts/admin/addon/routes/admin-api-keys-new.js
index af26a30..cc375b0 100644
--- a/app/assets/javascripts/admin/addon/routes/admin-api-keys-new.js
+++ b/app/assets/javascripts/admin/addon/routes/admin-api-keys-new.js
@@ -1,17 +1,7 @@
 import Route from "@ember/routing/route";
-import { ajax } from "discourse/lib/ajax";
 
 export default Route.extend({
   model() {
     return this.store.createRecord("api-key");
   },
-
-  setupController(controller, model) {
-    ajax("/admin/api/keys/scopes.json").then((data) => {
-      controller.setProperties({
-        scopes: data.scopes,
-        model,
-      });
-    });
-  },
 });
diff --git a/app/assets/javascripts/admin/addon/templates/api-keys-new.hbs b/app/assets/javascripts/admin/addon/templates/api-keys-new.hbs
index a895726..2b9e40c 100644
--- a/app/assets/javascripts/admin/addon/templates/api-keys-new.hbs
+++ b/app/assets/javascripts/admin/addon/templates/api-keys-new.hbs
@@ -1,6 +1,6 @@
 {{#link-to "adminApiKeys.index" class="go-back"}}
   {{d-icon "arrow-left"}}
-  {{i18n "admin.api.all_api_keys"}}
+  <span>{{i18n "admin.api.all_api_keys"}}</span>
 {{/link-to}}
 
 <div class="api-key api-key-new">

GitHub sha: 6273dfad4bc79576808c00b98f9c211e575021f6