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