diff --git a/app/controllers/admin/products_controller.rb b/app/controllers/admin/products_controller.rb index 0cdde2f..fb7f5d4 100644 --- a/app/controllers/admin/products_controller.rb +++ b/app/controllers/admin/products_controller.rb @@ -19,7 +19,7 @@ module DiscoursePatrons name: params[:name], active: params[:active], metadata: { - group_name: params[:groupName] + group_name: params[:group_name] } ) @@ -29,6 +29,17 @@ module DiscoursePatrons return render_json_error e.message end end + + def destroy + begin + product = ::Stripe::Product.delete(params[:id]) + + render_json_dump product + + rescue ::Stripe::InvalidRequestError => e + return render_json_error e.message + end + end end end end diff --git a/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-products-index.js.es6 b/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-products-index.js.es6 index e240694..6f1cd71 100644 --- a/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-products-index.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-products-index.js.es6 @@ -2,14 +2,6 @@ import DiscourseURL from "discourse/lib/url"; export default Ember.Controller.extend({ actions: { - destroyProduct(product) { - product.destroy().then(() => - this.controllerFor("adminPluginsDiscoursePatronsProductsIndex") - .get("model") - .removeObject(product) - ); - }, - editProduct(id) { return DiscourseURL.redirectTo( `/admin/plugins/discourse-patrons/products/${id}` diff --git a/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-products-show.js.es6 b/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-products-show.js.es6 index 165c325..7533876 100644 --- a/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-products-show.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-products-show.js.es6 @@ -4,8 +4,11 @@ export default Ember.Controller.extend({ actions: { createProduct() { // TODO: set default group name beforehand - if(this.get("model.product.groupName") === undefined) { - this.set("model.product.groupName", this.get("model.group.firstObject")); + if (this.get("model.product.groupName") === undefined) { + this.set( + "model.product.groupName", + this.get("model.group.firstObject") + ); } this.get("model.product") diff --git a/assets/javascripts/discourse/models/admin-product.js.es6 b/assets/javascripts/discourse/models/admin-product.js.es6 index 82a904b..c875c43 100644 --- a/assets/javascripts/discourse/models/admin-product.js.es6 +++ b/assets/javascripts/discourse/models/admin-product.js.es6 @@ -1,9 +1,9 @@ import { ajax } from "discourse/lib/ajax"; const AdminProduct = Discourse.Model.extend({ - active: true, - - destroy() {}, + destroy() { + return ajax(`/patrons/admin/products/${this.id}`, { method: "delete" }); + }, save() { const data = { @@ -17,7 +17,7 @@ const AdminProduct = Discourse.Model.extend({ }); AdminProduct.reopenClass({ - find() { + findAll() { return ajax("/patrons/admin/products", { method: "get" }).then(result => result.map(product => AdminProduct.create(product)) ); diff --git a/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-products-index.js.es6 b/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-products-index.js.es6 index 33f20eb..b814351 100644 --- a/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-products-index.js.es6 +++ b/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-products-index.js.es6 @@ -2,7 +2,7 @@ import AdminProduct from "discourse/plugins/discourse-patrons/discourse/models/a export default Discourse.Route.extend({ model() { - return AdminProduct.find(); + return AdminProduct.findAll(); }, actions: { @@ -13,9 +13,12 @@ export default Discourse.Route.extend({ I18n.t("yes_value"), confirmed => { if (confirmed) { - this.controllerFor("adminPluginsDiscoursePatronsProductsIndex") - .get("model") - .removeObject(product); + product.destroy().then(() => { + this.controllerFor("adminPluginsDiscoursePatronsProductsIndex") + .get("model") + .removeObject(product); + }) + .catch(data => bootbox.alert(data.jqXHR.responseJSON.errors.join("\n"))); } } ); diff --git a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-products-index.hbs b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-products-index.hbs index 9279e76..fcf34aa 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-products-index.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-products-index.hbs @@ -9,7 +9,7 @@ {{#each model as |product|}}