mirror of
https://github.com/discourse/discourse.git
synced 2025-05-25 00:32:52 +08:00
REFACTOR: minor changes to api-keys-new (#14435)
- moves loading scopes to controller - avoids declaring array - simplify code
This commit is contained in:
@ -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: {
|
||||
@action
|
||||
updateUsername(selected) {
|
||||
this.set("model.username", get(selected, "firstObject"));
|
||||
},
|
||||
|
||||
changeUserMode(value) {
|
||||
if (value === "all") {
|
||||
@action
|
||||
changeUserMode(userMode) {
|
||||
if (userMode === "all") {
|
||||
this.model.set("username", null);
|
||||
}
|
||||
this.set("userMode", value);
|
||||
this.set("userMode", userMode);
|
||||
},
|
||||
|
||||
@action
|
||||
save() {
|
||||
if (!this.useGlobalKey) {
|
||||
const selectedScopes = Object.values(this.scopes)
|
||||
.flat()
|
||||
.filter((action) => {
|
||||
return action.selected;
|
||||
});
|
||||
.filterBy("selected");
|
||||
|
||||
this.model.set("scopes", selectedScopes);
|
||||
}
|
||||
|
||||
this.model.save().catch(popupAjaxError);
|
||||
return this.model.save().catch(popupAjaxError);
|
||||
},
|
||||
|
||||
@action
|
||||
continue() {
|
||||
this.transitionToRoute("adminApiKeys.show", this.model.id);
|
||||
},
|
||||
|
||||
@action
|
||||
showURLs(urls) {
|
||||
return showModal("admin-api-key-urls", {
|
||||
admin: true,
|
||||
model: {
|
||||
urls,
|
||||
},
|
||||
model: { urls },
|
||||
});
|
||||
},
|
||||
|
||||
_loadScopes() {
|
||||
return ajax("/admin/api/keys/scopes.json")
|
||||
.then((data) => {
|
||||
this.set("scopes", data.scopes);
|
||||
})
|
||||
.catch(popupAjaxError);
|
||||
},
|
||||
});
|
||||
|
@ -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">
|
||||
|
Reference in New Issue
Block a user