Skip to content

Commit

Permalink
REFACTOR: minor changes to api-keys-new (#14435)
Browse files Browse the repository at this point in the history
- moves loading scopes to controller
- avoids declaring array
- simplify code
  • Loading branch information
jjaffeux committed Sep 27, 2021
1 parent 1abe807 commit 6273dfa
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 55 deletions.
101 changes: 57 additions & 44 deletions app/assets/javascripts/admin/addon/controllers/admin-api-keys-new.js
Expand Up @@ -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);
},
});
10 changes: 0 additions & 10 deletions 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,
});
});
},
});
@@ -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">
Expand Down

0 comments on commit 6273dfa

Please sign in to comment.