From b9e8eeb28053504cb1c4999724e804a958a937a4 Mon Sep 17 00:00:00 2001 From: lukicenturi Date: Wed, 4 Sep 2024 16:57:15 +0700 Subject: [PATCH] fix: subscriber middleware logic --- components/checkout/plan/PlanSelection.vue | 21 ++++++++++++++++++++- components/common/AppShowcaseSlider.vue | 2 +- components/products/ProductsButtons.vue | 13 ------------- locales/en.json | 1 + middleware/subscriber.ts | 15 ++++----------- pages/checkout/pay/index.vue | 2 +- utils/subscription.ts | 18 ++++++++++++++++++ 7 files changed, 45 insertions(+), 27 deletions(-) create mode 100644 utils/subscription.ts diff --git a/components/checkout/plan/PlanSelection.vue b/components/checkout/plan/PlanSelection.vue index 940e72fe..8b27a308 100644 --- a/components/checkout/plan/PlanSelection.vue +++ b/components/checkout/plan/PlanSelection.vue @@ -2,6 +2,7 @@ import { storeToRefs } from 'pinia'; import { get, set } from '@vueuse/core'; import { useMainStore } from '~/store'; +import { canBuyNewSubscription } from '~/utils/subscription'; import type { Plan } from '~/types'; const { t } = useI18n(); @@ -41,6 +42,8 @@ function next() { }); } +const canBuy = reactive(canBuyNewSubscription)(account); + const css = useCssModule(); @@ -93,7 +96,7 @@ const css = useCssModule();
+ +
+ * {{ t('home.plans.cannot_continue') }} + + {{ t('page_header.manage_premium') }} + +
diff --git a/components/common/AppShowcaseSlider.vue b/components/common/AppShowcaseSlider.vue index 6f3b5499..e5deb1ed 100644 --- a/components/common/AppShowcaseSlider.vue +++ b/components/common/AppShowcaseSlider.vue @@ -54,7 +54,7 @@ scanImages(); !!get(account)?.hasActiveSubscription, -); const allowNavigation = computed(() => { if (!isDefined(account)) @@ -45,7 +42,6 @@ const { t } = useI18n(); @@ -62,14 +58,5 @@ const { t } = useI18n(); {{ t('subscription.error.unverified_email') }} - - {{ t('page_header.manage_premium') }} - diff --git a/locales/en.json b/locales/en.json index 7f3d888b..cb2eb7cd 100644 --- a/locales/en.json +++ b/locales/en.json @@ -479,6 +479,7 @@ } }, "plans": { + "cannot_continue": "You cannot continue because you have an active subscription. Click here to", "title": "Start using rotki today!", "most_popular": "Most popular", "choose": "Choose Plan", diff --git a/middleware/subscriber.ts b/middleware/subscriber.ts index c7945a26..93a480ee 100644 --- a/middleware/subscriber.ts +++ b/middleware/subscriber.ts @@ -1,20 +1,13 @@ import { storeToRefs } from 'pinia'; -import { get } from '@vueuse/core'; import { useMainStore } from '~/store'; +import { canBuyNewSubscription } from '~/utils/subscription'; export default defineNuxtRouteMiddleware(async () => { const { account } = storeToRefs(useMainStore()); - if (!isDefined(account)) - return; + const canBuy = canBuyNewSubscription(account); - const { hasActiveSubscription, subscriptions } = get(account); - if (hasActiveSubscription) { - const renewableSubscriptions = subscriptions.filter(({ actions }) => - actions.includes('renew'), - ); - - if (!renewableSubscriptions) - return navigateTo('/home/subscription'); + if (!canBuy) { + return navigateTo('/home/subscription'); } }); diff --git a/pages/checkout/pay/index.vue b/pages/checkout/pay/index.vue index 3c59971c..ad2a3e3f 100644 --- a/pages/checkout/pay/index.vue +++ b/pages/checkout/pay/index.vue @@ -2,7 +2,7 @@ import { commonAttrs, noIndex } from '~/utils/metadata'; definePageMeta({ - middleware: ['maintenance', 'unverified', 'pending-payment', 'subscriber'], + middleware: ['maintenance', 'unverified', 'pending-payment'], }); useHead({ diff --git a/utils/subscription.ts b/utils/subscription.ts new file mode 100644 index 00000000..f0211cc1 --- /dev/null +++ b/utils/subscription.ts @@ -0,0 +1,18 @@ +import { get } from '@vueuse/core'; +import type { Account } from '~/types'; + +export function canBuyNewSubscription(account: Ref): boolean { + if (!isDefined(account)) + return true; + + const { hasActiveSubscription, subscriptions } = get(account); + + if (!hasActiveSubscription) + return true; + + const renewableSubscriptions = subscriptions.filter(({ actions }) => + actions.includes('renew'), + ); + + return renewableSubscriptions.length > 0; +}