From e2f1f0f523826a7d8913be63044867b38db25342 Mon Sep 17 00:00:00 2001 From: Rimian Perkins Date: Wed, 16 Oct 2019 11:22:58 +1100 Subject: [PATCH] destroy plans --- app/controllers/admin/plans_controller.rb | 20 +++++++++++++++---- .../discourse/models/admin-plan.js.es6 | 6 ++++-- ...ugins-discourse-patrons-plans-index.js.es6 | 7 +++++-- config/locales/client.en.yml | 2 +- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/app/controllers/admin/plans_controller.rb b/app/controllers/admin/plans_controller.rb index ac1514a..89e1ec0 100644 --- a/app/controllers/admin/plans_controller.rb +++ b/app/controllers/admin/plans_controller.rb @@ -8,8 +8,14 @@ module DiscoursePatrons before_action :set_api_key def index - plans = ::Stripe::Plan.list - render_json_dump plans.data + begin + plans = ::Stripe::Plan.list + + render_json_dump plans.data + + rescue ::Stripe::InvalidRequestError => e + return render_json_error e.message + end end def create @@ -31,8 +37,14 @@ module DiscoursePatrons end def destroy - plan = ::Stripe::Plan.delete(params[:id]) - render_json_dump plan + begin + plan = ::Stripe::Plan.delete(params[:id]) + + render_json_dump plan + + rescue ::Stripe::InvalidRequestError => e + return render_json_error e.message + end end private diff --git a/assets/javascripts/discourse/models/admin-plan.js.es6 b/assets/javascripts/discourse/models/admin-plan.js.es6 index 9417e63..a11c989 100644 --- a/assets/javascripts/discourse/models/admin-plan.js.es6 +++ b/assets/javascripts/discourse/models/admin-plan.js.es6 @@ -6,7 +6,9 @@ const AdminPlan = Discourse.Model.extend({ amount: 0, intervals: ["day", "week", "month", "year"], - destroy() {}, + destroy() { + return ajax(`/patrons/admin/plans/${this.id}`, { method: "delete" }); + }, save() { const data = { @@ -20,7 +22,7 @@ const AdminPlan = Discourse.Model.extend({ }); AdminPlan.reopenClass({ - find() { + findAll() { return ajax("/patrons/admin/plans", { method: "get" }).then(result => result.map(plan => AdminPlan.create(plan)) ); diff --git a/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans-index.js.es6 b/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans-index.js.es6 index 8863e99..3d78adb 100644 --- a/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans-index.js.es6 +++ b/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans-index.js.es6 @@ -2,7 +2,7 @@ import AdminPlan from "discourse/plugins/discourse-patrons/discourse/models/admi export default Discourse.Route.extend({ model() { - return AdminPlan.find(); + return AdminPlan.findAll(); }, actions: { @@ -13,9 +13,12 @@ export default Discourse.Route.extend({ I18n.t("yes_value"), confirmed => { if (confirmed) { - this.controllerFor("adminPluginsDiscoursePatronsPlansIndex") + plan.destroy().then(() => { + this.controllerFor("adminPluginsDiscoursePatronsPlansIndex") .get("model") .removeObject(plan); + }) + .catch(data => bootbox.alert(data.jqXHR.responseJSON.errors.join("\n"))); } } ); diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 640dd1b..9942488 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -68,7 +68,7 @@ en: active: Active product: product_id: Product ID - group: Group + group: User Group active: Active operations: destroy: