discourse-subscriptions/assets/javascripts/discourse/models/admin-product.js.es6

60 lines
1.3 KiB
JavaScript

import computed from "ember-addons/ember-computed-decorators";
import { ajax } from "discourse/lib/ajax";
const AdminProduct = Discourse.Model.extend({
isNew: false,
metadata: {},
@computed("created")
createdFormatted(created) {
return moment.unix(created).format();
},
destroy() {
return ajax(`/patrons/admin/products/${this.id}`, { method: "delete" });
},
save() {
const data = {
name: this.name,
statement_descriptor: this.statement_descriptor,
metadata: this.metadata,
active: this.active
};
return ajax("/patrons/admin/products", { method: "post", data }).then(
product => AdminProduct.create(product)
);
},
update() {
const data = {
name: this.name,
statement_descriptor: this.statement_descriptor,
metadata: this.metadata,
active: this.active
};
return ajax(`/patrons/admin/products/${this.id}`, {
method: "patch",
data
});
}
});
AdminProduct.reopenClass({
findAll() {
return ajax("/patrons/admin/products", { method: "get" }).then(result =>
result.map(product => AdminProduct.create(product))
);
},
find(id) {
return ajax(`/patrons/admin/products/${id}`, { method: "get" }).then(
product => AdminProduct.create(product)
);
}
});
export default AdminProduct;