Skip to content

Commit

Permalink
chore(dashboard): added suggestions from the pr #6612
Browse files Browse the repository at this point in the history
  • Loading branch information
LetItRock committed Oct 4, 2024
1 parent f859800 commit db593ff
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ const pluralizeDaysLeft = (numberOfDays: number) => {
};

export const FreeTrialCard = () => {
const { subscription, isLoading } = useBillingSubscription();
const { trial, hasPaymentMethod } = subscription;
const { subscription, daysLeft, isLoading } = useBillingSubscription();

if (isLoading || !trial.isActive || hasPaymentMethod) {
if (isLoading || !subscription || !subscription.trial.isActive || subscription?.hasPaymentMethod) {
return null;
}

const pluralizedDays = pluralizeDaysLeft(trial.daysLeft);
const pluralizedDays = pluralizeDaysLeft(daysLeft);

return (
<a
Expand Down Expand Up @@ -60,7 +59,7 @@ export const FreeTrialCard = () => {
Experience novu without any limits for free for the next {pluralizedDays}.
</span>
<div className={`max-h-3 overflow-hidden opacity-100 ${transition} group-hover:max-h-0 group-hover:opacity-0`}>
<Progress value={trial.daysLeft} max={trial.daysTotal > 0 ? trial.daysTotal : 100} />
<Progress value={daysLeft} max={subscription.trial.daysTotal > 0 ? subscription.trial.daysTotal : 100} />
</div>
<div
className={`-mt-2 max-h-0 overflow-hidden opacity-0 ${transition} group-hover:max-h-8 group-hover:opacity-100`}
Expand Down
34 changes: 5 additions & 29 deletions apps/dashboard/src/hooks/use-billing-subscription.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useQuery } from '@tanstack/react-query';
import { useMemo } from 'react';
import { differenceInDays, isSameDay } from 'date-fns';
import { ApiServiceLevelEnum, GetSubscriptionDto } from '@novu/shared';
import { GetSubscriptionDto } from '@novu/shared';
import { useAuth } from '@/context';
import { getBillingSubscription } from '@/api/billing';
import { QueryKeys } from '@/utils/query-keys';

const today = new Date();

export type UseSubscriptionType = GetSubscriptionDto & { trial: { daysLeft: number }; isLoading: boolean };
export type UseSubscriptionType = GetSubscriptionDto & { daysLeft: number; isLoading: boolean };

export const useBillingSubscription = () => {
const { currentOrganization } = useAuth();
Expand All @@ -18,25 +18,6 @@ export const useBillingSubscription = () => {
getBillingSubscription,
{
enabled: !!currentOrganization,
initialData: {
apiServiceLevel: ApiServiceLevelEnum.FREE,
isActive: false,
hasPaymentMethod: false,
status: 'trialing',
currentPeriodStart: null,
currentPeriodEnd: null,
billingInterval: null,
events: {
current: 0,
included: 0,
},
trial: {
isActive: false,
start: today.toISOString(),
end: today.toISOString(),
daysTotal: 0,
},
},
}
);

Expand All @@ -46,16 +27,11 @@ export const useBillingSubscription = () => {
return isSameDay(new Date(subscription.trial.end), today)
? 0
: differenceInDays(new Date(subscription.trial.end), today);
}, [subscription.trial.end]);
}, [subscription?.trial.end]);

return {
isLoading: isLoadingSubscription,
subscription: {
...subscription,
trial: {
...subscription.trial,
daysLeft,
},
},
subscription,
daysLeft,
};
};

0 comments on commit db593ff

Please sign in to comment.