From 19a03aa2ca0e43ab6582d9e82fd1bb87789bcfd7 Mon Sep 17 00:00:00 2001 From: Rimian Perkins Date: Tue, 8 Oct 2019 19:37:22 +1100 Subject: [PATCH] plan model with destroy and find --- ...ugins-discourse-patrons-plans-index.js.es6 | 19 +++++++++-------- .../javascripts/discourse/models/plan.js.es6 | 14 +++++++++++++ ...ugins-discourse-patrons-plans-index.js.es6 | 21 +++++++++++++++++-- ...min-plugins-discourse-patrons-plans.js.es6 | 1 - .../plugins-discourse-patrons-plans-index.hbs | 13 +++++++++--- config/locales/client.en.yml | 3 +++ 6 files changed, 56 insertions(+), 15 deletions(-) create mode 100644 assets/javascripts/discourse/models/plan.js.es6 diff --git a/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-plans-index.js.es6 b/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-plans-index.js.es6 index aee4af9..b6b536b 100644 --- a/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-plans-index.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-plans-index.js.es6 @@ -1,20 +1,21 @@ import { ajax } from "discourse/lib/ajax"; -import computed from "ember-addons/ember-computed-decorators"; import DiscourseURL from "discourse/lib/url"; +import Plan from "discourse/plugins/discourse-patrons/discourse/models/plan"; export default Ember.Controller.extend({ - @computed("model.plans") - plans(plans) { - return plans.filter(plan => !plan.deleted); - }, - actions: { - deletePlan(id) { - return ajax(`/patrons/admin/plans/${id}`, { method: "delete" }); + destroyPlan(plan) { + plan.destroy().then(() => + this.controllerFor("adminBackupsIndex") + .get("model") + .removeObject(backup) + ); }, editPlan(id) { - return DiscourseURL.redirectTo(`/admin/plugins/discourse-patrons/plans/${id}`); + return DiscourseURL.redirectTo( + `/admin/plugins/discourse-patrons/plans/${id}` + ); } } }); diff --git a/assets/javascripts/discourse/models/plan.js.es6 b/assets/javascripts/discourse/models/plan.js.es6 new file mode 100644 index 0000000..a2524a5 --- /dev/null +++ b/assets/javascripts/discourse/models/plan.js.es6 @@ -0,0 +1,14 @@ +import { ajax } from "discourse/lib/ajax"; + +const Plan = Discourse.Model.extend({ + destroy() { } +}); + +Plan.reopenClass({ + find() { + return ajax("/patrons/admin/plans", { method: "get" }) + .then(result => result.plans.map(plan => Plan.create(plan))); + } +}); + +export default 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 9809f55..07ad8ff 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 @@ -1,7 +1,24 @@ -import { ajax } from "discourse/lib/ajax"; +import Plan from "discourse/plugins/discourse-patrons/discourse/models/plan"; export default Discourse.Route.extend({ model() { - return ajax("/patrons/admin/plans", { method: "get" }); + return Plan.find(); + }, + + actions: { + destroyPlan(plan) { + bootbox.confirm( + I18n.t("discourse-patrons.plans.operations.destroy.confirm"), + I18n.t("no_value"), + I18n.t("yes_value"), + confirmed => { + if (confirmed) { + this.controllerFor("adminPluginsDiscoursePatronsPlansIndex") + .get("model") + .removeObject(plan); + } + } + ); + } } }); diff --git a/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans.js.es6 b/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans.js.es6 index 6408cd8..55af882 100644 --- a/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans.js.es6 +++ b/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans.js.es6 @@ -1,2 +1 @@ - export default Discourse.Route.extend({}); diff --git a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans-index.hbs b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans-index.hbs index 25fb0d1..d2aa9bc 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans-index.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans-index.hbs @@ -8,15 +8,22 @@ {{i18n 'discourse_patrons.admin.plans.plan.interval'}} {{i18n 'discourse_patrons.admin.plans.plan.amount'}} - {{#each plans as |plan|}} + {{#each model as |plan|}} {{plan.id}} {{plan.nickname}} {{plan.interval}} {{plan.amount}} - {{d-button action=(action "editPlan" plan.id) icon="far-edit" class="btn no-text btn-icon"}} - {{d-button action=(action "deletePlan" plan.id) icon="trash-alt" class="btn-danger btn no-text btn-icon"}} + {{d-button + action=(action "editPlan" plan.id) + icon="far-edit" + class="btn no-text btn-icon"}} + {{d-button + action=(route-action "destroyPlan") + actionParam=plan + icon="trash-alt" + class="btn-danger btn no-text btn-icon"}} {{/each}} diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 21574be..2150342 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -60,5 +60,8 @@ en: nickname: Nickname interval: Interval amount: Amount + operations: + destroy: + confirm: Are you sure you want to destroy this plan? subscriptions: title: Subscriptions