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