From a58b13e9e4eae8a2288edc726404b1d4f22ab093 Mon Sep 17 00:00:00 2001 From: merefield Date: Wed, 15 Nov 2023 15:36:21 +0000 Subject: [PATCH] only run remote sub update retrieve on update action --- app/controllers/custom_wizard/admin/subscription.rb | 7 ++++++- .../components/wizard-subscription-badge.js | 2 +- .../javascripts/discourse/services/subscription.js | 13 +++++++++++++ lib/custom_wizard/subscription.rb | 7 +++++-- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/controllers/custom_wizard/admin/subscription.rb b/app/controllers/custom_wizard/admin/subscription.rb index 141b283a..7b596ec6 100644 --- a/app/controllers/custom_wizard/admin/subscription.rb +++ b/app/controllers/custom_wizard/admin/subscription.rb @@ -3,7 +3,12 @@ class CustomWizard::SubscriptionController < ::Admin::AdminController before_action :ensure_admin def index - subscription = CustomWizard::Subscription.new + if params[:update_from_remote] + subscription = CustomWizard::Subscription.new(true) + else + subscription = CustomWizard::Subscription.new + end + render_json_dump( subscribed: subscription.subscribed?, subscription_type: subscription.type, diff --git a/assets/javascripts/discourse/components/wizard-subscription-badge.js b/assets/javascripts/discourse/components/wizard-subscription-badge.js index 39b6cf81..f2115fdb 100644 --- a/assets/javascripts/discourse/components/wizard-subscription-badge.js +++ b/assets/javascripts/discourse/components/wizard-subscription-badge.js @@ -39,7 +39,7 @@ export default class WizardSubscriptionBadge extends Component { update() { this.updating = true; this.updateIcon = "check"; - this.subscription.retrieveSubscriptionStatus().finally(() => { + this.subscription.updateSubscriptionStatus().finally(() => { this.updating = false; setTimeout(() => { this.updateIcon = null; diff --git a/assets/javascripts/discourse/services/subscription.js b/assets/javascripts/discourse/services/subscription.js index 7ee5454c..0a784519 100644 --- a/assets/javascripts/discourse/services/subscription.js +++ b/assets/javascripts/discourse/services/subscription.js @@ -36,6 +36,19 @@ export default class SubscriptionService extends Service { this.standardSubscription = this.subscriptionType === "standard"; } + async updateSubscriptionStatus() { + let result = await ajax("/admin/wizards/subscription?update_from_remote=true").catch( + popupAjaxError + ); + + this.subscribed = result.subscribed; + this.subscriptionType = result.subscription_type; + this.subscriptionAttributes = result.subscription_attributes; + this.businessSubscription = this.subscriptionType === "business"; + this.communitySubscription = this.subscriptionType === "community"; + this.standardSubscription = this.subscriptionType === "standard"; + } + get subscriptionLink() { return this.subscriptionLandingUrl; } diff --git a/lib/custom_wizard/subscription.rb b/lib/custom_wizard/subscription.rb index 47c7599c..8d9d122e 100644 --- a/lib/custom_wizard/subscription.rb +++ b/lib/custom_wizard/subscription.rb @@ -106,8 +106,11 @@ class CustomWizard::Subscription attr_accessor :product_id, :product_slug - def initialize - ::DiscourseSubscriptionClient::Subscriptions.update + def initialize(update = false) + if update + ::DiscourseSubscriptionClient::Subscriptions.update + end + result = ::DiscourseSubscriptionClient.find_subscriptions("discourse-custom-wizard") if result&.any?