2020-03-07 00:41:41 +08:00
|
|
|
import Controller, { inject as controller } from "@ember/controller";
|
2019-10-31 04:28:29 +08:00
|
|
|
import { alias, sort } from "@ember/object/computed";
|
2018-01-22 11:10:53 +08:00
|
|
|
import GrantBadgeController from "discourse/mixins/grant-badge-controller";
|
2020-05-14 04:23:41 +08:00
|
|
|
import I18n from "I18n";
|
2019-11-08 05:38:28 +08:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2019-10-30 21:48:24 +08:00
|
|
|
import { next } from "@ember/runloop";
|
2018-12-31 17:46:39 +08:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2022-09-14 23:06:56 +08:00
|
|
|
import { inject as service } from "@ember/service";
|
2015-08-19 09:24:09 +08:00
|
|
|
|
2019-10-24 01:06:54 +08:00
|
|
|
export default Controller.extend(GrantBadgeController, {
|
2020-03-07 00:41:41 +08:00
|
|
|
adminUser: controller(),
|
2022-09-14 23:06:56 +08:00
|
|
|
dialog: service(),
|
2019-10-31 04:28:29 +08:00
|
|
|
user: alias("adminUser.model"),
|
|
|
|
userBadges: alias("model"),
|
|
|
|
allBadges: alias("badges"),
|
|
|
|
sortedBadges: sort("model", "badgeSortOrder"),
|
2019-05-28 18:15:12 +08:00
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
this.badgeSortOrder = ["granted_at:desc"];
|
|
|
|
},
|
2014-03-19 22:27:21 +08:00
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("model", "model.[]", "model.expandedBadges.[]")
|
2019-04-26 18:16:21 +08:00
|
|
|
groupedBadges() {
|
2019-05-27 16:15:39 +08:00
|
|
|
const allBadges = this.model;
|
2015-02-25 09:52:43 +08:00
|
|
|
|
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);
|
|
|
|
});
|
2015-02-25 09:52:43 +08:00
|
|
|
|
2020-09-02 02:24:20 +08:00
|
|
|
let expanded = [];
|
2018-11-26 23:24:46 +08:00
|
|
|
const expandedBadges = allBadges.get("expandedBadges") || [];
|
2015-02-25 09:52:43 +08:00
|
|
|
|
2020-09-02 02:24:20 +08:00
|
|
|
Object.values(grouped).forEach(function (badges) {
|
|
|
|
let lastGranted = badges[0].granted_at;
|
2015-02-25 09:52:43 +08:00
|
|
|
|
2018-11-26 23:24:46 +08:00
|
|
|
badges.forEach((badge) => {
|
2015-02-25 09:52:43 +08:00
|
|
|
lastGranted =
|
|
|
|
lastGranted < badge.granted_at ? badge.granted_at : lastGranted;
|
|
|
|
});
|
|
|
|
|
2018-11-19 19:08:54 +08:00
|
|
|
if (badges.length === 1 || expandedBadges.includes(badges[0].badge.id)) {
|
2018-11-20 21:46:58 +08:00
|
|
|
badges.forEach((badge) => expanded.push(badge));
|
2015-02-25 09:52:43 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-02 02:24:20 +08:00
|
|
|
let result = {
|
2015-02-25 09:52:43 +08:00
|
|
|
badge: badges[0].badge,
|
|
|
|
granted_at: lastGranted,
|
2021-11-10 07:31:41 +08:00
|
|
|
badges,
|
2015-02-25 09:52:43 +08:00
|
|
|
count: badges.length,
|
|
|
|
grouped: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
expanded.push(result);
|
|
|
|
});
|
|
|
|
|
2020-09-02 02:24:20 +08:00
|
|
|
return expanded.sortBy("granted_at").reverse();
|
2019-04-26 18:16:21 +08:00
|
|
|
},
|
2015-02-25 09:52:43 +08:00
|
|
|
|
2014-03-19 22:27:21 +08:00
|
|
|
actions: {
|
2021-11-13 21:01:55 +08:00
|
|
|
expandGroup(userBadge) {
|
2019-05-27 16:15:39 +08:00
|
|
|
const model = this.model;
|
2015-02-25 09:52:43 +08:00
|
|
|
model.set("expandedBadges", model.get("expandedBadges") || []);
|
|
|
|
model.get("expandedBadges").pushObject(userBadge.badge.id);
|
|
|
|
},
|
|
|
|
|
2018-01-22 11:10:53 +08:00
|
|
|
grantBadge() {
|
|
|
|
this.grantBadge(
|
2019-05-27 16:15:39 +08:00
|
|
|
this.selectedBadgeId,
|
2018-01-22 11:10:53 +08:00
|
|
|
this.get("user.username"),
|
2019-05-27 16:15:39 +08:00
|
|
|
this.badgeReason
|
2018-01-22 11:10:53 +08:00
|
|
|
).then(
|
|
|
|
() => {
|
|
|
|
this.set("badgeReason", "");
|
2019-10-30 21:48:24 +08:00
|
|
|
next(() => {
|
2018-01-22 11:10:53 +08:00
|
|
|
// Update the selected badge ID after the combobox has re-rendered.
|
2019-05-27 16:15:39 +08:00
|
|
|
const newSelectedBadge = this.grantableBadges[0];
|
2018-01-22 11:10:53 +08:00
|
|
|
if (newSelectedBadge) {
|
|
|
|
this.set("selectedBadgeId", newSelectedBadge.get("id"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2018-12-31 17:46:39 +08:00
|
|
|
function (error) {
|
|
|
|
popupAjaxError(error);
|
2014-03-25 18:15:34 +08:00
|
|
|
}
|
|
|
|
);
|
2014-03-19 22:27:21 +08:00
|
|
|
},
|
|
|
|
|
2016-10-21 01:26:41 +08:00
|
|
|
revokeBadge(userBadge) {
|
2022-09-14 23:06:56 +08:00
|
|
|
return this.dialog.yesNoConfirm({
|
|
|
|
message: I18n.t("admin.badges.revoke_confirm"),
|
|
|
|
didConfirm: () => {
|
|
|
|
return userBadge.revoke().then(() => {
|
|
|
|
this.model.removeObject(userBadge);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
2014-03-19 22:27:21 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|