From 09e1235832778188d469aaa67e294fec86afa1d1 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Wed, 8 Nov 2023 18:38:12 -0800 Subject: [PATCH] Auto update billing card UI on (re/un-)subscribe click on web app Previously required a page load to see the updated billing state after clicking resubscribe or unsubscribe buttons --- src/khoj/interface/web/config.html | 69 +++++++++++++++++++++++------- src/khoj/routers/subscription.py | 2 +- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/khoj/interface/web/config.html b/src/khoj/interface/web/config.html index 58c68349..082c5d01 100644 --- a/src/khoj/interface/web/config.html +++ b/src/khoj/interface/web/config.html @@ -174,31 +174,47 @@
- {% if subscription_state == "subscribed" %} -

You are subscribed to Khoj Cloud. Subscription will renew on {{ subscription_renewal_date }}

- {% elif subscription_state == "unsubscribed" %} -

You are subscribed to Khoj Cloud. Subscription will expire on {{ subscription_renewal_date }}

- {% elif subscription_state == "expired" %} -

Subscribe to Khoj Cloud. Subscription expired on {{ subscription_renewal_date }}

- {% else %} -

Subscribe to Khoj Cloud

- {% endif %} +

+ Subscribe to Khoj Cloud +

+

+ You are subscribed to Khoj Cloud. Subscription will renew on {{ subscription_renewal_date }} +

+

+ You are subscribed to Khoj Cloud. Subscription will expire on {{ subscription_renewal_date }} +

+

+ Subscribe to Khoj Cloud. Subscription expired on {{ subscription_renewal_date }} +

- {% if subscription_state == "subscribed" %} - - {% elif subscription_state == "unsubscribed" %} - - {% else %} - + Subscribe - {% endif %}
@@ -274,6 +290,17 @@ 'Content-Type': 'application/json', }, }) + .then(response => response.json()) + .then(data => { + if (data.success) { + document.getElementById("unsubscribe-description").style.display = "none"; + document.getElementById("unsubscribe-button").style.display = "none"; + + document.getElementById("resubscribe-description").style.display = ""; + document.getElementById("resubscribe-button").style.display = ""; + + } + }) } function resubscribe() { @@ -283,6 +310,16 @@ 'Content-Type': 'application/json', }, }) + .then(response => response.json()) + .then(data => { + if (data.success) { + document.getElementById("resubscribe-description").style.display = "none"; + document.getElementById("resubscribe-button").style.display = "none"; + + document.getElementById("unsubscribe-description").style.display = ""; + document.getElementById("unsubscribe-button").style.display = ""; + } + }) } var configure = document.getElementById("configure"); diff --git a/src/khoj/routers/subscription.py b/src/khoj/routers/subscription.py index a9862f51..5b6998c0 100644 --- a/src/khoj/routers/subscription.py +++ b/src/khoj/routers/subscription.py @@ -64,7 +64,7 @@ async def subscribe(request: Request): elif event_type in {"customer.subscription.updated"}: user_subscription = await sync_to_async(adapters.get_user_subscription)(customer_email) # Allow updating subscription status if paid user - if user_subscription.renewal_date: + if user_subscription and user_subscription.renewal_date: # Mark user as unsubscribed or resubscribed is_recurring = not subscription["cancel_at_period_end"] updated_user = await adapters.set_user_subscription(customer_email, is_recurring=is_recurring)