This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/controllers/group.js.es6
Dan Ungureanu 352d43b101
FIX: Better handling of Group model state (#8356)
The group card and group members page were affecting each other and were
leaking members list and the query parameters which led to bad UX
experience and sub-optimal performance (client made more queries because
it was loading fewer members).

This commit refactors the group model to make it more consistent, remove
dead code, move error handling outside of model.
2019-11-18 14:59:28 +02:00

161 lines
3.8 KiB
JavaScript

import EmberObject from "@ember/object";
import { inject } from "@ember/controller";
import Controller from "@ember/controller";
import { default as discourseComputed } from "discourse-common/utils/decorators";
const Tab = EmberObject.extend({
init() {
this._super(...arguments);
let name = this.name;
this.set("route", this.route || `group.` + name);
this.set("message", I18n.t(`groups.${this.i18nKey || name}`));
}
});
export default Controller.extend({
application: inject(),
counts: null,
showing: "members",
destroying: null,
@discourseComputed(
"showMessages",
"model.user_count",
"model.request_count",
"canManageGroup",
"model.allow_membership_requests"
)
tabs(
showMessages,
userCount,
requestCount,
canManageGroup,
allowMembershipRequests
) {
const membersTab = Tab.create({
name: "members",
route: "group.index",
icon: "users",
i18nKey: "members.title"
});
membersTab.set("count", userCount);
const defaultTabs = [membersTab, Tab.create({ name: "activity" })];
if (canManageGroup && allowMembershipRequests) {
defaultTabs.push(
Tab.create({
name: "requests",
i18nKey: "requests.title",
icon: "user-plus",
count: requestCount
})
);
}
if (showMessages) {
defaultTabs.push(
Tab.create({
name: "messages",
i18nKey: "messages"
})
);
}
if (canManageGroup) {
defaultTabs.push(
Tab.create({
name: "manage",
i18nKey: "manage.title",
icon: "wrench"
})
);
}
return defaultTabs;
},
@discourseComputed("model.is_group_user")
showMessages(isGroupUser) {
if (!this.siteSettings.enable_personal_messages) {
return false;
}
return isGroupUser || (this.currentUser && this.currentUser.admin);
},
@discourseComputed("model.is_group_owner", "model.automatic")
canEditGroup(isGroupOwner, automatic) {
return !automatic && isGroupOwner;
},
@discourseComputed("model.displayName", "model.full_name")
groupName(displayName, fullName) {
return (fullName || displayName).capitalize();
},
@discourseComputed(
"model.name",
"model.flair_url",
"model.flair_bg_color",
"model.flair_color"
)
avatarFlairAttributes(groupName, flairURL, flairBgColor, flairColor) {
return {
primary_group_flair_url: flairURL,
primary_group_flair_bg_color: flairBgColor,
primary_group_flair_color: flairColor,
primary_group_name: groupName
};
},
@discourseComputed("model.messageable")
displayGroupMessageButton(messageable) {
return this.currentUser && messageable;
},
@discourseComputed("model", "model.automatic")
canManageGroup(model, automatic) {
return (
this.currentUser &&
(this.currentUser.canManageGroup(model) ||
(this.currentUser.admin && automatic))
);
},
actions: {
messageGroup() {
this.send("createNewMessageViaParams", this.get("model.name"));
},
destroy() {
const group = this.model;
this.set("destroying", true);
bootbox.confirm(
I18n.t("admin.groups.delete_confirm"),
I18n.t("no_value"),
I18n.t("yes_value"),
confirmed => {
if (confirmed) {
group
.destroy()
.then(() => {
this.transitionToRoute("groups.index");
})
.catch(error => {
// eslint-disable-next-line no-console
console.error(error);
bootbox.alert(I18n.t("admin.groups.delete_failed"));
})
.finally(() => this.set("destroying", false));
} else {
this.set("destroying", false);
}
}
);
}
}
});