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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

106 lines
2.8 KiB
JavaScript
Raw Normal View History

import { action } from "@ember/object";
import { inject as service } from "@ember/service";
import { alias, sort } from "@ember/object/computed";
import Controller, { inject as controller } from "@ember/controller";
import GrantBadgeController from "discourse/mixins/grant-badge-controller";
import I18n from "I18n";
import discourseComputed from "discourse-common/utils/decorators";
import { next } from "@ember/runloop";
import { popupAjaxError } from "discourse/lib/ajax-error";
2015-08-19 09:24:09 +08:00
export default class AdminUserBadgesController extends Controller.extend(
GrantBadgeController
) {
@service dialog;
@controller adminUser;
@alias("adminUser.model") user;
@alias("model") userBadges;
@alias("badges") allBadges;
@sort("model", "badgeSortOrder") sortedBadges;
badgeSortOrder = ["granted_at:desc"];
@discourseComputed("model", "model.[]", "model.expandedBadges.[]")
groupedBadges() {
const allBadges = this.model;
2020-09-02 02:24:20 +08:00
let grouped = {};
allBadges.forEach((b) => {
grouped[b.badge_id] = grouped[b.badge_id] || [];
grouped[b.badge_id].push(b);
});
2020-09-02 02:24:20 +08:00
let expanded = [];
2018-11-26 23:24:46 +08:00
const expandedBadges = allBadges.get("expandedBadges") || [];
2020-09-02 02:24:20 +08:00
Object.values(grouped).forEach(function (badges) {
let lastGranted = badges[0].granted_at;
2018-11-26 23:24:46 +08:00
badges.forEach((badge) => {
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;
}
2020-09-02 02:24:20 +08:00
let result = {
badge: badges[0].badge,
granted_at: lastGranted,
badges,
count: badges.length,
grouped: true,
};
expanded.push(result);
});
2020-09-02 02:24:20 +08:00
return expanded.sortBy("granted_at").reverse();
}
@action
expandGroup(userBadge) {
const model = this.model;
model.set("expandedBadges", model.get("expandedBadges") || []);
model.get("expandedBadges").pushObject(userBadge.badge.id);
}
@action
performGrantBadge() {
this.grantBadge(
this.selectedBadgeId,
this.get("user.username"),
this.badgeReason
).then(
() => {
this.set("badgeReason", "");
next(() => {
// Update the selected badge ID after the combobox has re-rendered.
const newSelectedBadge = this.grantableBadges[0];
if (newSelectedBadge) {
this.set("selectedBadgeId", newSelectedBadge.get("id"));
}
});
},
function (error) {
popupAjaxError(error);
}
);
}
@action
revokeBadge(userBadge) {
return this.dialog.yesNoConfirm({
message: I18n.t("admin.badges.revoke_confirm"),
didConfirm: () => {
return userBadge.revoke().then(() => {
this.model.removeObject(userBadge);
});
},
});
}
}