REFACTORING: admin-edit-badge-groupings (#7015)

This commit is contained in:
Joffrey JAFFEUX
2019-02-19 09:30:24 +01:00
committed by GitHub
parent bf2059baf5
commit ee692414ce
2 changed files with 43 additions and 46 deletions

View File

@ -1,22 +1,24 @@
import { ajax } from "discourse/lib/ajax";
import ModalFunctionality from "discourse/mixins/modal-functionality";
import { observes } from "ember-addons/ember-computed-decorators";
export default Ember.Controller.extend(ModalFunctionality, {
modelChanged: function() {
@observes("model")
modelChanged() {
const model = this.get("model");
const copy = Ember.A();
const store = this.store;
if (model) {
model.forEach(function(o) {
copy.pushObject(store.createRecord("badge-grouping", o));
});
model.forEach(o =>
copy.pushObject(store.createRecord("badge-grouping", o))
);
}
this.set("workingCopy", copy);
}.observes("model"),
},
moveItem: function(item, delta) {
moveItem(item, delta) {
const copy = this.get("workingCopy");
const index = copy.indexOf(item);
if (index + delta < 0 || index + delta >= copy.length) {
@ -28,60 +30,51 @@ export default Ember.Controller.extend(ModalFunctionality, {
},
actions: {
up: function(item) {
up(item) {
this.moveItem(item, -1);
},
down: function(item) {
down(item) {
this.moveItem(item, 1);
},
delete: function(item) {
delete(item) {
this.get("workingCopy").removeObject(item);
},
cancel: function() {
this.set("model", null);
this.set("workingCopy", null);
cancel() {
this.setProperties({ model: null, workingCopy: null });
this.send("closeModal");
},
edit: function(item) {
edit(item) {
item.set("editing", true);
},
save: function(item) {
save(item) {
item.set("editing", false);
},
add: function() {
add() {
const obj = this.store.createRecord("badge-grouping", {
editing: true,
name: I18n.t("admin.badges.badge_grouping")
});
this.get("workingCopy").pushObject(obj);
},
saveAll: function() {
const self = this;
var items = this.get("workingCopy");
const groupIds = items.map(function(i) {
return i.get("id") || -1;
});
const names = items.map(function(i) {
return i.get("name");
});
saveAll() {
let items = this.get("workingCopy");
const groupIds = items.map(i => i.get("id") || -1);
const names = items.map(i => i.get("name"));
ajax("/admin/badges/badge_groupings", {
data: { ids: groupIds, names: names },
data: { ids: groupIds, names },
method: "POST"
}).then(
function(data) {
items = self.get("model");
data => {
items = this.get("model");
items.clear();
data.badge_groupings.forEach(function(g) {
items.pushObject(self.store.createRecord("badge-grouping", g));
data.badge_groupings.forEach(g => {
items.pushObject(this.store.createRecord("badge-grouping", g));
});
self.set("model", null);
self.set("workingCopy", null);
self.send("closeModal");
this.setProperties({ model: null, workingCopy: null });
this.send("closeModal");
},
function() {
bootbox.alert(I18n.t("generic_error"));
}
() => bootbox.alert(I18n.t("generic_error"))
);
}
}