From d430098badad978b4687d8c3c642775c0cb736ec Mon Sep 17 00:00:00 2001 From: Justin DiRose Date: Tue, 24 Nov 2020 14:52:00 -0600 Subject: [PATCH] FIX: Properly allow no group to be selected (#33) --- ...scriptions-products-show-plans-show.js.es6 | 32 ++++++++++++++----- ...scriptions-products-show-plans-show.js.es6 | 3 ++ ...subscriptions-products-show-plans-show.hbs | 32 +++++++++---------- 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/assets/javascripts/discourse/controllers/admin-plugins-discourse-subscriptions-products-show-plans-show.js.es6 b/assets/javascripts/discourse/controllers/admin-plugins-discourse-subscriptions-products-show-plans-show.js.es6 index 14a26a2..4843657 100644 --- a/assets/javascripts/discourse/controllers/admin-plugins-discourse-subscriptions-products-show-plans-show.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-plugins-discourse-subscriptions-products-show-plans-show.js.es6 @@ -1,14 +1,31 @@ import discourseComputed from "discourse-common/utils/decorators"; import DiscourseURL from "discourse/lib/url"; import Controller from "@ember/controller"; +import { alias } from "@ember/object/computed"; const RECURRING = "recurring"; const ONE_TIME = "one_time"; export default Controller.extend({ // Also defined in settings. - selectedCurrency: Ember.computed.alias("model.plan.currency"), - selectedInterval: Ember.computed.alias("model.plan.interval"), + selectedCurrency: alias("model.plan.currency"), + selectedInterval: alias("model.plan.interval"), + + @discourseComputed("model.plan.metadata.group_name") + selectedGroup(groupName) { + return groupName || "no-group"; + }, + + @discourseComputed("model.groups") + availableGroups(groups) { + return [ + { + id: null, + name: "no-group", + }, + ...groups, + ]; + }, @discourseComputed currencies() { @@ -57,13 +74,9 @@ export default Controller.extend({ }, createPlan() { - // TODO: set default group name beforehand - if (this.get("model.plan.metadata.group_name") === undefined) { - this.set("model.plan.metadata", { - group_name: this.get("model.groups.firstObject.name"), - }); + if (this.model.plan.metadata.group_name === "no-group") { + this.set("model.plan.metadata.group_name", null); } - this.get("model.plan") .save() .then(() => this.redirect(this.productId)) @@ -73,6 +86,9 @@ export default Controller.extend({ }, updatePlan() { + if (this.model.plan.metadata.group_name === "no-group") { + this.set("model.plan.metadata.group_name", null); + } this.get("model.plan") .update() .then(() => this.redirect(this.productId)) diff --git a/assets/javascripts/discourse/routes/admin-plugins-discourse-subscriptions-products-show-plans-show.js.es6 b/assets/javascripts/discourse/routes/admin-plugins-discourse-subscriptions-products-show-plans-show.js.es6 index cba5b77..037477d 100644 --- a/assets/javascripts/discourse/routes/admin-plugins-discourse-subscriptions-products-show-plans-show.js.es6 +++ b/assets/javascripts/discourse/routes/admin-plugins-discourse-subscriptions-products-show-plans-show.js.es6 @@ -20,6 +20,9 @@ export default Route.extend({ isRecurring: true, currency: Discourse.SiteSettings.discourse_subscriptions_currency, product: product.get("id"), + metadata: { + group_name: null, + }, }); } else { plan = AdminPlan.find(id).then((result) => { diff --git a/assets/javascripts/discourse/templates/admin/plugins-discourse-subscriptions-products-show-plans-show.hbs b/assets/javascripts/discourse/templates/admin/plugins-discourse-subscriptions-products-show-plans-show.hbs index d9ae1d4..8918d47 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-discourse-subscriptions-products-show-plans-show.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-discourse-subscriptions-products-show-plans-show.hbs @@ -15,10 +15,10 @@

- {{combo-box + {{combo-box valueProperty="name" - content=model.groups - value=model.plan.metadata.group_name + content=availableGroups + value=selectedGroup onChange=(action (mut model.plan.metadata.group_name)) }}

@@ -30,10 +30,10 @@ {{#if planFieldDisabled}} {{input class="plan-amount plan-currency" disabled=true value=model.plan.currency}} {{else}} - {{combo-box - disabled=planFieldDisabled - content=currencies - value=model.plan.currency + {{combo-box + disabled=planFieldDisabled + content=currencies + value=model.plan.currency onChange=(action (mut model.plan.currency)) }} {{/if}} @@ -44,16 +44,16 @@ {{i18n 'discourse_subscriptions.admin.plans.plan.recurring'}} {{#if planFieldDisabled}} - {{input - type="checkbox" - name="recurring" + {{input + type="checkbox" + name="recurring" checked=model.plan.isRecurring disabled=true }} {{else}} - {{input - type="checkbox" - name="recurring" + {{input + type="checkbox" + name="recurring" checked=model.plan.isRecurring change=(action 'changeRecurring') }} @@ -67,10 +67,10 @@ {{#if planFieldDisabled}} {{input disabled=true value=selectedInterval}} {{else}} - {{combo-box + {{combo-box valueProperty="name" - content=availableIntervals - value=selectedInterval + content=availableIntervals + value=selectedInterval onChange=(action (mut selectedInterval)) }} {{/if}}