From 6273dfad4bc79576808c00b98f9c211e575021f6 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Mon, 27 Sep 2021 10:43:47 +0200 Subject: [PATCH] REFACTOR: minor changes to api-keys-new (#14435) - moves loading scopes to controller - avoids declaring array - simplify code --- .../addon/controllers/admin-api-keys-new.js | 101 ++++++++++-------- .../admin/addon/routes/admin-api-keys-new.js | 10 -- .../admin/addon/templates/api-keys-new.hbs | 2 +- 3 files changed, 58 insertions(+), 55 deletions(-) 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 c90316369a8..961c1a5c4b2 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")); + }, - changeUserMode(value) { - if (value === "all") { - this.model.set("username", null); - } - this.set("userMode", value); - }, + @action + changeUserMode(userMode) { + if (userMode === "all") { + this.model.set("username", null); + } + this.set("userMode", userMode); + }, - save() { - if (!this.useGlobalKey) { - const selectedScopes = Object.values(this.scopes) - .flat() - .filter((action) => { - return action.selected; - }); + @action + save() { + if (!this.useGlobalKey) { + const selectedScopes = Object.values(this.scopes) + .flat() + .filterBy("selected"); - this.model.set("scopes", selectedScopes); - } + this.model.set("scopes", selectedScopes); + } - this.model.save().catch(popupAjaxError); - }, + return this.model.save().catch(popupAjaxError); + }, - continue() { - this.transitionToRoute("adminApiKeys.show", this.model.id); - }, + @action + continue() { + this.transitionToRoute("adminApiKeys.show", this.model.id); + }, - showURLs(urls) { - return showModal("admin-api-key-urls", { - admin: true, - model: { - urls, - }, - }); - }, + @action + 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 af26a308485..cc375b0cc36 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 a8957262df3..2b9e40cf80a 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"}} + {{i18n "admin.api.all_api_keys"}} {{/link-to}}