REFACTOR: various refactoring applied to group pages (#9440)

This commit is contained in:
Joffrey JAFFEUX 2020-04-16 22:12:37 +02:00 committed by GitHub
parent b37b238382
commit 0a9f283e7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 192 additions and 185 deletions

View File

@ -3,6 +3,7 @@ import { isEmpty } from "@ember/utils";
import Controller from "@ember/controller"; import Controller from "@ember/controller";
import { extractError } from "discourse/lib/ajax-error"; import { extractError } from "discourse/lib/ajax-error";
import ModalFunctionality from "discourse/mixins/modal-functionality"; import ModalFunctionality from "discourse/mixins/modal-functionality";
import { action } from "@ember/object";
export default Controller.extend(ModalFunctionality, { export default Controller.extend(ModalFunctionality, {
loading: false, loading: false,
@ -13,36 +14,32 @@ export default Controller.extend(ModalFunctionality, {
return loading || !usernames || !(usernames.length > 0); return loading || !usernames || !(usernames.length > 0);
}, },
actions: { @action
addMembers() { addMembers() {
this.set("loading", true); this.set("loading", true);
const model = this.model; const usernames = this.model.usernames;
const usernames = model.get("usernames"); if (isEmpty(usernames)) {
if (isEmpty(usernames)) { return;
return;
}
let promise;
if (this.setAsOwner) {
promise = model.addOwners(usernames, true);
} else {
promise = model.addMembers(usernames, true);
}
promise
.then(() => {
this.transitionToRoute("group.members", this.get("model.name"), {
queryParams: { filter: usernames }
});
model.set("usernames", null);
this.send("closeModal");
})
.catch(error => {
this.flash(extractError(error), "error");
})
.finally(() => this.set("loading", false));
} }
let promise;
if (this.setAsOwner) {
promise = this.model.addOwners(usernames, true);
} else {
promise = this.model.addMembers(usernames, true);
}
promise
.then(() => {
this.transitionToRoute("group.members", this.get("model.name"), {
queryParams: { filter: usernames }
});
this.model.set("usernames", null);
this.send("closeModal");
})
.catch(error => this.flash(extractError(error), "error"))
.finally(() => this.set("loading", false));
} }
}); });

View File

@ -1,8 +1,9 @@
import Controller, { inject as controller } from "@ember/controller"; import Controller, { inject as controller } from "@ember/controller";
import { alias } from "@ember/object/computed"; import { gt, readOnly } from "@ember/object/computed";
import discourseComputed, { observes } from "discourse-common/utils/decorators"; import discourseComputed, { observes } from "discourse-common/utils/decorators";
import { popupAjaxError } from "discourse/lib/ajax-error"; import { popupAjaxError } from "discourse/lib/ajax-error";
import discourseDebounce from "discourse/lib/debounce"; import discourseDebounce from "discourse/lib/debounce";
import { action } from "@ember/object";
export default Controller.extend({ export default Controller.extend({
application: controller(), application: controller(),
@ -15,7 +16,7 @@ export default Controller.extend({
filterInput: null, filterInput: null,
loading: false, loading: false,
isOwner: alias("model.is_group_owner"), isOwner: readOnly("model.is_group_owner"),
showActions: false, showActions: false,
@observes("filterInput") @observes("filterInput")
@ -29,27 +30,22 @@ export default Controller.extend({
}, },
findMembers(refresh) { findMembers(refresh) {
if (this.loading) { if (this.loading || !this.model) {
return; return;
} }
const model = this.model; if (!refresh && this.model.members.length >= this.model.user_count) {
if (!model) {
return;
}
if (!refresh && model.members.length >= model.user_count) {
this.set("application.showFooter", true); this.set("application.showFooter", true);
return; return;
} }
this.set("loading", true); this.set("loading", true);
model.findMembers(this.memberParams, refresh).finally(() => { this.model.findMembers(this.memberParams, refresh).finally(() => {
this.set( this.setProperties({
"application.showFooter", "application.showFooter":
model.members.length >= model.user_count this.model.members.length >= this.model.user_count,
); loading: false
this.set("loading", false); });
}); });
}, },
@ -58,10 +54,7 @@ export default Controller.extend({
return { order, desc, filter }; return { order, desc, filter };
}, },
@discourseComputed("model.members.[]") hasMembers: gt("model.members.length", 0),
hasMembers(members) {
return members && members.length > 0;
},
@discourseComputed("model") @discourseComputed("model")
canManageGroup(model) { canManageGroup(model) {
@ -77,49 +70,53 @@ export default Controller.extend({
} }
}, },
actions: { @action
loadMore() { loadMore() {
this.findMembers(); this.findMembers();
}, },
toggleActions() { @action
this.toggleProperty("showActions"); toggleActions() {
}, this.toggleProperty("showActions");
},
actOnGroup(member, actionId) { @action
switch (actionId) { actOnGroup(member, actionId) {
case "removeMember": switch (actionId) {
this.send("removeMember", member); case "removeMember":
break; this.removeMember(member);
case "makeOwner": break;
this.send("makeOwner", member.username); case "makeOwner":
break; this.makeOwner(member.username);
case "removeOwner": break;
this.send("removeOwner", member); case "removeOwner":
break; this.removeOwner(member);
} break;
}, }
},
removeMember(user) { @action
this.model.removeMember(user, this.memberParams); removeMember(user) {
}, this.model.removeMember(user, this.memberParams);
},
makeOwner(username) { @action
this.model.addOwners(username); makeOwner(username) {
}, this.model.addOwners(username);
},
removeOwner(user) { @action
this.model.removeOwner(user); removeOwner(user) {
}, this.model.removeOwner(user);
},
addMembers() { @action
const usernames = this.usernames; addMembers() {
if (usernames && usernames.length > 0) { if (this.usernames && this.usernames.length > 0) {
this.model this.model
.addMembers(usernames) .addMembers(this.usernames)
.then(() => this.set("usernames", [])) .then(() => this.set("usernames", []))
.catch(popupAjaxError); .catch(popupAjaxError);
}
} }
} }
}); });

View File

@ -1,5 +1,5 @@
import Controller, { inject as controller } from "@ember/controller"; import Controller, { inject as controller } from "@ember/controller";
import EmberObject from "@ember/object"; import EmberObject, { action } from "@ember/object";
import discourseComputed, { observes } from "discourse-common/utils/decorators"; import discourseComputed, { observes } from "discourse-common/utils/decorators";
export default Controller.extend({ export default Controller.extend({
@ -19,8 +19,8 @@ export default Controller.extend({
"filters.target_user", "filters.target_user",
"filters.subject" "filters.subject"
) )
filterParams(action, acting_user, target_user, subject) { filterParams(filtersAction, acting_user, target_user, subject) {
return { action, acting_user, target_user, subject }; return { action: filtersAction, acting_user, target_user, subject };
}, },
@observes( @observes(
@ -54,28 +54,26 @@ export default Controller.extend({
}); });
}, },
actions: { @action
loadMore() { loadMore() {
if (this.get("model.all_loaded")) return; if (this.get("model.all_loaded")) return;
this.set("loading", true); this.set("loading", true);
this.get("group.model") this.get("group.model")
.findLogs(this.offset + 1, this.filterParams) .findLogs(this.offset + 1, this.filterParams)
.then(results => { .then(results => {
results.logs.forEach(result => results.logs.forEach(result =>
this.get("model.logs").addObject(result) this.get("model.logs").addObject(result)
); );
this.incrementProperty("offset"); this.incrementProperty("offset");
this.set("model.all_loaded", results.all_loaded); this.set("model.all_loaded", results.all_loaded);
}) })
.finally(() => { .finally(() => this.set("loading", false));
this.set("loading", false); },
});
},
clearFilter(key) { @action
this.set(`filters.${key}`, ""); clearFilter(key) {
} this.set(`filters.${key}`, "");
} }
}); });

View File

@ -1,15 +1,18 @@
import EmberObject from "@ember/object"; import EmberObject, { action } from "@ember/object";
import Controller, { inject as controller } from "@ember/controller"; import Controller, { inject as controller } from "@ember/controller";
import discourseComputed from "discourse-common/utils/decorators"; import discourseComputed from "discourse-common/utils/decorators";
import { inject as service } from "@ember/service"; import { inject as service } from "@ember/service";
import { readOnly } from "@ember/object/computed"; import { readOnly } from "@ember/object/computed";
import deprecated from "discourse-common/lib/deprecated";
const Tab = EmberObject.extend({ const Tab = EmberObject.extend({
init() { init() {
this._super(...arguments); this._super(...arguments);
let name = this.name;
this.set("route", this.route || `group.` + name); this.setProperties({
this.set("message", I18n.t(`groups.${this.i18nKey || name}`)); route: this.route || `group.${this.name}`,
message: I18n.t(`groups.${this.i18nKey || this.name}`)
});
} }
}); });
@ -19,7 +22,7 @@ export default Controller.extend({
showing: "members", showing: "members",
destroying: null, destroying: null,
router: service(), router: service(),
currentPath: readOnly("router._router.currentPath"), currentPath: readOnly("router.currentRouteName"),
@discourseComputed( @discourseComputed(
"showMessages", "showMessages",
@ -39,11 +42,10 @@ export default Controller.extend({
name: "members", name: "members",
route: "group.index", route: "group.index",
icon: "users", icon: "users",
i18nKey: "members.title" i18nKey: "members.title",
count: userCount
}); });
membersTab.set("count", userCount);
const defaultTabs = [membersTab, Tab.create({ name: "activity" })]; const defaultTabs = [membersTab, Tab.create({ name: "activity" })];
if (canManageGroup && allowMembershipRequests) { if (canManageGroup && allowMembershipRequests) {
@ -127,37 +129,45 @@ export default Controller.extend({
); );
}, },
actions: { @action
messageGroup() { messageGroup() {
this.send("createNewMessageViaParams", this.get("model.name")); this.send("createNewMessageViaParams", this.get("model.name"));
}, },
destroy() { @action
const group = this.model; destroyGroup() {
this.set("destroying", true); this.set("destroying", true);
bootbox.confirm( bootbox.confirm(
I18n.t("admin.groups.delete_confirm"), I18n.t("admin.groups.delete_confirm"),
I18n.t("no_value"), I18n.t("no_value"),
I18n.t("yes_value"), I18n.t("yes_value"),
confirmed => { confirmed => {
if (confirmed) { if (confirmed) {
group this.model
.destroy() .destroy()
.then(() => { .then(() => this.transitionToRoute("groups.index"))
this.transitionToRoute("groups.index"); .catch(error => {
}) // eslint-disable-next-line no-console
.catch(error => { console.error(error);
// eslint-disable-next-line no-console bootbox.alert(I18n.t("admin.groups.delete_failed"));
console.error(error); })
bootbox.alert(I18n.t("admin.groups.delete_failed")); .finally(() => this.set("destroying", false));
}) } else {
.finally(() => this.set("destroying", false)); this.set("destroying", false);
} else {
this.set("destroying", false);
}
} }
); }
);
},
actions: {
destroy() {
deprecated("Use `destroyGroup` action instead of `destroy`.", {
since: "2.5.0",
dropFrom: "2.6.0"
});
this.destroyGroup();
} }
} }
}); });

View File

@ -1,21 +1,20 @@
import { action } from "@ember/object";
import Controller from "@ember/controller"; import Controller from "@ember/controller";
import { popupAjaxError } from "discourse/lib/ajax-error"; import { popupAjaxError } from "discourse/lib/ajax-error";
export default Controller.extend({ export default Controller.extend({
saving: null, saving: null,
actions: { @action
save() { save() {
this.set("saving", true); this.set("saving", true);
const group = this.model;
group this.model
.create() .create()
.then(() => { .then(() => {
this.transitionToRoute("group.members", group.name); this.transitionToRoute("group.members", this.model.name);
}) })
.catch(popupAjaxError) .catch(popupAjaxError)
.finally(() => this.set("saving", false)); .finally(() => this.set("saving", false));
}
} }
}); });

View File

@ -1,5 +1,6 @@
import DiscourseRoute from "discourse/routes/discourse"; import DiscourseRoute from "discourse/routes/discourse";
import showModal from "discourse/lib/show-modal"; import showModal from "discourse/lib/show-modal";
import { action } from "@ember/object";
export default DiscourseRoute.extend({ export default DiscourseRoute.extend({
titleToken() { titleToken() {
@ -12,28 +13,28 @@ export default DiscourseRoute.extend({
}, },
setupController(controller, model) { setupController(controller, model) {
this.controllerFor("group").set("showing", "members");
controller.setProperties({ controller.setProperties({
model, model,
filterInput: this._params.filter filterInput: this._params.filter,
showing: "members"
}); });
controller.findMembers(true); controller.findMembers(true);
}, },
actions: { @action
showAddMembersModal() { showAddMembersModal() {
showModal("group-add-members", { model: this.modelFor("group") }); showModal("group-add-members", { model: this.modelFor("group") });
}, },
showBulkAddModal() { @action
showModal("group-bulk-add", { model: this.modelFor("group") }); showBulkAddModal() {
}, showModal("group-bulk-add", { model: this.modelFor("group") });
},
didTransition() { @action
this.controllerFor("group-index").set("filterInput", this._params.filter); didTransition() {
return true; this.controllerFor("group-index").set("filterInput", this._params.filter);
} return true;
} }
}); });

View File

@ -1,7 +1,7 @@
import DiscourseRoute from "discourse/routes/discourse"; import DiscourseRoute from "discourse/routes/discourse";
export default DiscourseRoute.extend({ export default DiscourseRoute.extend({
beforeModel: function() { beforeModel() {
this.transitionTo("group.index"); this.transitionTo("group.index");
} }
}); });

View File

@ -14,6 +14,6 @@ export default DiscourseRoute.extend({
}, },
setupController(controller, model) { setupController(controller, model) {
controller.setProperties({ model }); controller.set("model", model);
} }
}); });

View File

@ -9,7 +9,8 @@
flairURL=model.flair_url flairURL=model.flair_url
flairBgColor=model.flair_bg_color flairBgColor=model.flair_bg_color
flairColor=model.flair_color flairColor=model.flair_color
groupName=model.name}} groupName=model.name
}}
</span> </span>
{{/if}} {{/if}}
@ -32,24 +33,28 @@
<div class="group-details-button"> <div class="group-details-button">
{{group-membership-button {{group-membership-button
class="inline" class="inline"
model=model model=model
showLogin=(route-action "showLogin")}} showLogin=(route-action "showLogin")
}}
{{#if displayGroupMessageButton}} {{#if displayGroupMessageButton}}
{{d-button {{d-button
action=(action "messageGroup") action=(action "messageGroup")
class="btn-primary group-message-button inline" class="btn-primary group-message-button inline"
icon="envelope" icon="envelope"
label="groups.message"}} label="groups.message"
}}
{{/if}} {{/if}}
{{#if currentUser.admin}} {{#if currentUser.admin}}
{{d-button action=(action "destroy") {{d-button
disabled=destroying action=(action "destroy")
icon="trash-alt" disabled=destroying
class="btn-danger" icon="trash-alt"
label="admin.groups.delete"}} class="btn-danger"
label="admin.groups.delete"
}}
{{/if}} {{/if}}
</div> </div>
</div> </div>