2015-08-18 21:24:09 -04:00
|
|
|
import Badge from "discourse/models/badge";
|
2020-05-13 16:23:41 -04:00
|
|
|
import I18n from "I18n";
|
2019-10-23 12:39:32 -04:00
|
|
|
import Route from "@ember/routing/route";
|
2016-06-30 13:55:44 -04:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2020-08-26 12:57:13 -04:00
|
|
|
import bootbox from "bootbox";
|
2019-10-31 16:28:10 -04:00
|
|
|
import { get } from "@ember/object";
|
2015-03-10 15:01:15 -04:00
|
|
|
import showModal from "discourse/lib/show-modal";
|
|
|
|
|
2019-10-23 12:39:32 -04:00
|
|
|
export default Route.extend({
|
2015-03-10 15:01:15 -04:00
|
|
|
serialize(m) {
|
2019-10-31 16:28:10 -04:00
|
|
|
return { badge_id: get(m, "id") || "new" };
|
2014-10-17 14:27:40 -04:00
|
|
|
},
|
|
|
|
|
2015-03-10 15:01:15 -04:00
|
|
|
model(params) {
|
2014-10-17 14:27:40 -04:00
|
|
|
if (params.badge_id === "new") {
|
2015-08-18 21:24:09 -04:00
|
|
|
return Badge.create({
|
2014-10-17 14:27:40 -04:00
|
|
|
name: I18n.t("admin.badges.new_badge"),
|
|
|
|
});
|
|
|
|
}
|
2019-11-12 05:49:31 -05:00
|
|
|
return this.modelFor("adminBadges").findBy(
|
|
|
|
"id",
|
|
|
|
parseInt(params.badge_id, 10)
|
|
|
|
);
|
2014-10-17 14:27:40 -04:00
|
|
|
},
|
|
|
|
|
2021-03-17 01:55:23 -04:00
|
|
|
setupController(controller, model) {
|
|
|
|
this._super(...arguments);
|
|
|
|
if (model.image_url) {
|
|
|
|
controller.showImageUploader();
|
|
|
|
} else if (model.icon) {
|
|
|
|
controller.showIconSelector();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-10-17 14:27:40 -04:00
|
|
|
actions: {
|
2015-03-10 15:01:15 -04:00
|
|
|
saveError(e) {
|
|
|
|
let msg = I18n.t("generic_error");
|
2014-10-17 14:27:40 -04:00
|
|
|
if (e.responseJSON && e.responseJSON.errors) {
|
|
|
|
msg = I18n.t("generic_error_with_reason", {
|
|
|
|
error: e.responseJSON.errors.join(". "),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
bootbox.alert(msg);
|
|
|
|
},
|
|
|
|
|
2015-03-10 15:01:15 -04:00
|
|
|
editGroupings() {
|
2015-04-09 18:33:37 -04:00
|
|
|
const model = this.controllerFor("admin-badges").get("badgeGroupings");
|
2016-11-15 17:09:55 -05:00
|
|
|
showModal("admin-edit-badge-groupings", { model, admin: true });
|
2014-10-17 14:27:40 -04:00
|
|
|
},
|
|
|
|
|
2015-03-10 15:01:15 -04:00
|
|
|
preview(badge, explain) {
|
2014-10-17 14:27:40 -04:00
|
|
|
badge.set("preview_loading", true);
|
2016-06-30 13:55:44 -04:00
|
|
|
ajax("/admin/badges/preview.json", {
|
2020-03-26 16:00:10 -04:00
|
|
|
type: "POST",
|
2014-10-17 14:27:40 -04:00
|
|
|
data: {
|
|
|
|
sql: badge.get("query"),
|
|
|
|
target_posts: !!badge.get("target_posts"),
|
|
|
|
trigger: badge.get("trigger"),
|
2015-03-10 15:01:15 -04:00
|
|
|
explain,
|
2014-10-17 14:27:40 -04:00
|
|
|
},
|
2015-04-09 18:33:37 -04:00
|
|
|
})
|
|
|
|
.then(function (model) {
|
2014-10-17 14:27:40 -04:00
|
|
|
badge.set("preview_loading", false);
|
2016-11-15 17:09:55 -05:00
|
|
|
showModal("admin-badge-preview", { model, admin: true });
|
2014-10-17 14:27:40 -04:00
|
|
|
})
|
|
|
|
.catch(function (error) {
|
|
|
|
badge.set("preview_loading", false);
|
2019-11-07 12:20:35 -05:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(error);
|
2014-10-17 14:27:40 -04:00
|
|
|
bootbox.alert("Network error");
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|