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

- moves loading scopes to controller
- avoids declaring array
- simplify code
This commit is contained in:
Joffrey JAFFEUX
2021-09-27 10:43:47 +02:00
committed by GitHub
parent 1abe807528
commit 6273dfad4b
3 changed files with 58 additions and 55 deletions

View File

@ -3,70 +3,83 @@ import I18n from "I18n";
import discourseComputed from "discourse-common/utils/decorators"; import discourseComputed from "discourse-common/utils/decorators";
import { isBlank } from "@ember/utils"; import { isBlank } from "@ember/utils";
import { popupAjaxError } from "discourse/lib/ajax-error"; 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 showModal from "discourse/lib/show-modal";
import { ajax } from "discourse/lib/ajax";
export default Controller.extend({ export default Controller.extend({
userModes: [ userModes: null,
{ id: "all", name: I18n.t("admin.api.all_users") },
{ id: "single", name: I18n.t("admin.api.single_user") },
],
useGlobalKey: false, useGlobalKey: false,
scopes: null, scopes: null,
@discourseComputed("userMode") init() {
showUserSelector(mode) { this._super(...arguments);
return mode === "single";
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") showUserSelector: equal("userMode", "single"),
saveDisabled(description, username, userMode) {
if (isBlank(description)) { @discourseComputed("model.{description,username}", "showUserSelector")
saveDisabled(model, showUserSelector) {
if (isBlank(model.description)) {
return true; return true;
} }
if (userMode === "single" && isBlank(username)) { if (showUserSelector && isBlank(model.username)) {
return true; return true;
} }
return false; return false;
}, },
actions: { @action
updateUsername(selected) { updateUsername(selected) {
this.set("model.username", get(selected, "firstObject")); this.set("model.username", get(selected, "firstObject"));
}, },
changeUserMode(value) { @action
if (value === "all") { changeUserMode(userMode) {
if (userMode === "all") {
this.model.set("username", null); this.model.set("username", null);
} }
this.set("userMode", value); this.set("userMode", userMode);
}, },
@action
save() { save() {
if (!this.useGlobalKey) { if (!this.useGlobalKey) {
const selectedScopes = Object.values(this.scopes) const selectedScopes = Object.values(this.scopes)
.flat() .flat()
.filter((action) => { .filterBy("selected");
return action.selected;
});
this.model.set("scopes", selectedScopes); this.model.set("scopes", selectedScopes);
} }
this.model.save().catch(popupAjaxError); return this.model.save().catch(popupAjaxError);
}, },
@action
continue() { continue() {
this.transitionToRoute("adminApiKeys.show", this.model.id); this.transitionToRoute("adminApiKeys.show", this.model.id);
}, },
@action
showURLs(urls) { showURLs(urls) {
return showModal("admin-api-key-urls", { return showModal("admin-api-key-urls", {
admin: true, admin: true,
model: { model: { urls },
urls,
},
}); });
}, },
_loadScopes() {
return ajax("/admin/api/keys/scopes.json")
.then((data) => {
this.set("scopes", data.scopes);
})
.catch(popupAjaxError);
}, },
}); });

View File

@ -1,17 +1,7 @@
import Route from "@ember/routing/route"; import Route from "@ember/routing/route";
import { ajax } from "discourse/lib/ajax";
export default Route.extend({ export default Route.extend({
model() { model() {
return this.store.createRecord("api-key"); return this.store.createRecord("api-key");
}, },
setupController(controller, model) {
ajax("/admin/api/keys/scopes.json").then((data) => {
controller.setProperties({
scopes: data.scopes,
model,
});
});
},
}); });

View File

@ -1,6 +1,6 @@
{{#link-to "adminApiKeys.index" class="go-back"}} {{#link-to "adminApiKeys.index" class="go-back"}}
{{d-icon "arrow-left"}} {{d-icon "arrow-left"}}
{{i18n "admin.api.all_api_keys"}} <span>{{i18n "admin.api.all_api_keys"}}</span>
{{/link-to}} {{/link-to}}
<div class="api-key api-key-new"> <div class="api-key api-key-new">