REFACTOR: edit-category controller (#7527)

This commit is contained in:
Joffrey JAFFEUX 2019-05-13 11:30:32 +02:00 committed by GitHub
parent 64c117519e
commit e64ed9dbc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,9 +1,12 @@
import ModalFunctionality from "discourse/mixins/modal-functionality";
import DiscourseURL from "discourse/lib/url";
import { extractError } from "discourse/lib/ajax-error";
import computed from "ember-addons/ember-computed-decorators";
import {
default as computed,
on,
observes
} from "ember-addons/ember-computed-decorators";
// Modal for editing / creating a category
export default Ember.Controller.extend(ModalFunctionality, {
selectedTab: null,
saving: false,
@ -11,9 +14,10 @@ export default Ember.Controller.extend(ModalFunctionality, {
panels: null,
hiddenTooltip: true,
_initPanels: function() {
@on("init")
_initPanels() {
this.set("panels", []);
}.on("init"),
},
onShow() {
this.changeSize();
@ -21,27 +25,29 @@ export default Ember.Controller.extend(ModalFunctionality, {
this.set("hiddenTooltip", true);
},
changeSize: function() {
@observes("model.description")
changeSize() {
if (!Ember.isEmpty(this.get("model.description"))) {
this.set("modal.modalClass", "edit-category-modal full");
} else {
this.set("modal.modalClass", "edit-category-modal small");
}
}.observes("model.description"),
},
@computed("model.id", "model.name")
title(id, name) {
if (id) {
@computed("model.{id,name}")
title(model) {
if (model.id) {
return I18n.t("category.edit_dialog_title", {
categoryName: name
categoryName: model.name
});
}
return I18n.t("category.create");
},
titleChanged: function() {
this.set("modal.title", this.get("title"));
}.observes("title"),
@observes("title")
titleChanged() {
this.set("modal.title", this.title);
},
@computed("saving", "model.name", "model.color", "deleting")
disabled(saving, name, color, deleting) {
@ -70,34 +76,33 @@ export default Ember.Controller.extend(ModalFunctionality, {
actions: {
saveCategory() {
const self = this,
model = this.get("model"),
parentCategory = this.site
.get("categories")
.findBy("id", parseInt(model.get("parent_category_id"), 10));
const model = this.model;
const parentCategory = this.site.categories.findBy(
"id",
parseInt(model.parent_category_id, 10)
);
this.set("saving", true);
model.set("parentCategory", parentCategory);
this.get("model")
model
.save()
.then(function(result) {
self.set("saving", false);
self.send("closeModal");
.then(result => {
this.set("saving", false);
this.send("closeModal");
model.setProperties({
slug: result.category.slug,
id: result.category.id
});
DiscourseURL.redirectTo("/c/" + Discourse.Category.slugFor(model));
})
.catch(function(error) {
self.flash(extractError(error), "error");
self.set("saving", false);
.catch(error => {
this.flash(extractError(error), "error");
this.set("saving", false);
});
},
deleteCategory() {
const self = this;
this.set("deleting", true);
this.send("hideModal");
@ -105,27 +110,24 @@ export default Ember.Controller.extend(ModalFunctionality, {
I18n.t("category.delete_confirm"),
I18n.t("no_value"),
I18n.t("yes_value"),
function(result) {
result => {
if (result) {
self
.get("model")
.destroy()
.then(
function() {
// success
self.send("closeModal");
DiscourseURL.redirectTo("/categories");
},
function(error) {
self.flash(extractError(error), "error");
self.send("reopenModal");
self.displayErrors([I18n.t("category.delete_error")]);
self.set("deleting", false);
}
);
this.model.destroy().then(
() => {
// success
this.send("closeModal");
DiscourseURL.redirectTo("/categories");
},
error => {
this.flash(extractError(error), "error");
this.send("reopenModal");
this.displayErrors([I18n.t("category.delete_error")]);
this.set("deleting", false);
}
);
} else {
self.send("reopenModal");
self.set("deleting", false);
this.send("reopenModal");
this.set("deleting", false);
}
}
);