discourse/app/assets/javascripts/admin/controllers/admin-user-badges.js

Failed to ignore revisions in .git-blame-ignore-revs.

105 lines
2.8 KiB
JavaScript
Raw Normal View History

import discourseComputed from "discourse-common/utils/decorators";
import { alias, sort } from "@ember/object/computed";
import { next } from "@ember/runloop";
import Controller, { inject as controller } from "@ember/controller";
import GrantBadgeController from "discourse/mixins/grant-badge-controller";
import { popupAjaxError } from "discourse/lib/ajax-error";
2015-08-18 21:24:09 -04:00
export default Controller.extend(GrantBadgeController, {
adminUser: controller(),
user: alias("adminUser.model"),
userBadges: alias("model"),
allBadges: alias("badges"),
sortedBadges: sort("model", "badgeSortOrder"),
init() {
this._super(...arguments);
this.badgeSortOrder = ["granted_at:desc"];
},
@discourseComputed("model", "model.[]", "model.expandedBadges.[]")
groupedBadges() {
const allBadges = this.model;
2015-08-13 15:14:08 -04:00
var grouped = _.groupBy(allBadges, badge => badge.badge_id);
var expanded = [];
2018-11-26 10:24:46 -05:00
const expandedBadges = allBadges.get("expandedBadges") || [];
2018-06-15 11:03:24 -04:00
_(grouped).each(function(badges) {
var lastGranted = badges[0].granted_at;
2018-11-26 10:24:46 -05:00
badges.forEach(badge => {
2018-06-15 11:03:24 -04:00
lastGranted =
lastGranted < badge.granted_at ? badge.granted_at : lastGranted;
});
if (badges.length === 1 || expandedBadges.includes(badges[0].badge.id)) {
badges.forEach(badge => expanded.push(badge));
return;
}
var result = {
badge: badges[0].badge,
granted_at: lastGranted,
badges: badges,
count: badges.length,
grouped: true
};
expanded.push(result);
});
2018-06-15 11:03:24 -04:00
return _(expanded)
.sortBy(group => group.granted_at)
.reverse()
.value();
},
actions: {
2018-06-15 11:03:24 -04:00
expandGroup: function(userBadge) {
const model = this.model;
2018-06-15 11:03:24 -04:00
model.set("expandedBadges", model.get("expandedBadges") || []);
model.get("expandedBadges").pushObject(userBadge.badge.id);
},
grantBadge() {
2018-06-15 11:03:24 -04:00
this.grantBadge(
this.selectedBadgeId,
2018-06-15 11:03:24 -04:00
this.get("user.username"),
this.badgeReason
2018-06-15 11:03:24 -04:00
).then(
() => {
this.set("badgeReason", "");
next(() => {
// Update the selected badge ID after the combobox has re-rendered.
const newSelectedBadge = this.grantableBadges[0];
if (newSelectedBadge) {
2018-06-15 11:03:24 -04:00
this.set("selectedBadgeId", newSelectedBadge.get("id"));
}
});
2018-06-15 11:03:24 -04:00
},
function(error) {
popupAjaxError(error);
2018-06-15 11:03:24 -04:00
}
);
},
revokeBadge(userBadge) {
2018-06-15 11:03:24 -04:00
return bootbox.confirm(
I18n.t("admin.badges.revoke_confirm"),
I18n.t("no_value"),
I18n.t("yes_value"),
result => {
if (result) {
userBadge.revoke().then(() => {
this.model.removeObject(userBadge);
2018-06-15 11:03:24 -04:00
});
}
}
2018-06-15 11:03:24 -04:00
);
}
}
});