Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: subscriber middleware logic #242

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion components/checkout/plan/PlanSelection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -41,6 +42,8 @@ function next() {
});
}

const canBuy = reactive(canBuyNewSubscription)(account);

const css = useCssModule();
</script>

Expand Down Expand Up @@ -93,7 +96,7 @@ const css = useCssModule();

<div :class="css.continue">
<RuiButton
:disabled="!selected"
:disabled="!selected || !canBuy"
:loading="processing"
class="w-full"
color="primary"
Expand All @@ -103,6 +106,22 @@ const css = useCssModule();
{{ t('actions.continue') }}
</RuiButton>
</div>

<div
v-if="!canBuy"
class="inline text-sm text-rui-text-secondary mt-2"
>
<span>* {{ t('home.plans.cannot_continue') }}</span>
<ButtonLink
to="/home/subscription"
variant="text"
color="primary"
inline
class="leading-[0] hover:underline"
>
{{ t('page_header.manage_premium') }}
</ButtonLink>
</div>
</div>
</div>
</template>
Expand Down
2 changes: 1 addition & 1 deletion components/common/AppShowcaseSlider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ scanImages();
<SwiperSlide
v-for="(image, i) in images"
:key="i"
class="relative pt-[56.2%]"
class="relative pt-[56.2%] bg-rui-grey-100"
>
<img
:src="image"
Expand Down
13 changes: 0 additions & 13 deletions components/products/ProductsButtons.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ withDefaults(

const store = useMainStore();
const { account } = storeToRefs(store);
const hasActiveSubscription = computed(
() => !!get(account)?.hasActiveSubscription,
);

const allowNavigation = computed(() => {
if (!isDefined(account))
Expand All @@ -45,7 +42,6 @@ const { t } = useI18n();
</ButtonLink>

<RuiTooltip
v-if="!hasActiveSubscription"
:disabled="allowNavigation"
tooltip-class="max-w-[20rem]"
>
Expand All @@ -62,14 +58,5 @@ const { t } = useI18n();
</template>
{{ t('subscription.error.unverified_email') }}
</RuiTooltip>
<ButtonLink
v-else
to="/home/subscription"
size="lg"
variant="filled"
:color="color"
>
{{ t('page_header.manage_premium') }}
</ButtonLink>
</div>
</template>
1 change: 1 addition & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 4 additions & 11 deletions middleware/subscriber.ts
Original file line number Diff line number Diff line change
@@ -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');
}
});
2 changes: 1 addition & 1 deletion pages/checkout/pay/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { commonAttrs, noIndex } from '~/utils/metadata';

definePageMeta({
middleware: ['maintenance', 'unverified', 'pending-payment', 'subscriber'],
middleware: ['maintenance', 'unverified', 'pending-payment'],
});

useHead({
Expand Down
18 changes: 18 additions & 0 deletions utils/subscription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { get } from '@vueuse/core';
import type { Account } from '~/types';

export function canBuyNewSubscription(account: Ref<Account | null>): 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;
}
Loading