Skip to content

Commit

Permalink
feat(sdk): add trial plan to props (#758)
Browse files Browse the repository at this point in the history
* feat: add trial plan to props

* fix: reset sub and trial if plan is not found

* fix: add return in catch block
  • Loading branch information
rsbh authored Aug 30, 2024
1 parent 9f5a301 commit 9565abb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
58 changes: 39 additions & 19 deletions sdks/js/packages/core/react/contexts/FrontierContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import {
getActiveSubscription,
getDefaultPaymentMethod,
enrichBasePlan,
defaultFetch
defaultFetch,
getTrialingSubscription
} from '../utils';
import {
DEFAULT_DATE_FORMAT,
Expand Down Expand Up @@ -71,6 +72,7 @@ interface FrontierContextProviderProps {
isBillingAccountLoading: boolean;
setIsBillingAccountLoading: Dispatch<SetStateAction<boolean>>;

trialSubscription: V1Beta1Subscription | undefined;
activeSubscription: V1Beta1Subscription | undefined;
setActiveSubscription: Dispatch<
SetStateAction<V1Beta1Subscription | undefined>
Expand All @@ -81,6 +83,7 @@ interface FrontierContextProviderProps {
isActiveSubscriptionLoading: boolean;
setIsActiveSubscriptionLoading: Dispatch<SetStateAction<boolean>>;

trialPlan: V1Beta1Plan | undefined;
activePlan: V1Beta1Plan | undefined;
setActivePlan: Dispatch<SetStateAction<V1Beta1Plan | undefined>>;

Expand Down Expand Up @@ -144,6 +147,7 @@ const initialValues: FrontierContextProviderProps = {
isBillingAccountLoading: false,
setIsBillingAccountLoading: () => false,

trialSubscription: undefined,
activeSubscription: undefined,
setActiveSubscription: () => undefined,

Expand All @@ -152,6 +156,7 @@ const initialValues: FrontierContextProviderProps = {
isActiveSubscriptionLoading: false,
setIsActiveSubscriptionLoading: () => false,

trialPlan: undefined,
activePlan: undefined,
setActivePlan: () => undefined,

Expand Down Expand Up @@ -212,12 +217,17 @@ export const FrontierContextProvider = ({
useState(false);
const [activeSubscription, setActiveSubscription] =
useState<V1Beta1Subscription>();

const [trialSubscription, setTrialSubscription] =
useState<V1Beta1Subscription>();

const [subscriptions, setSubscriptions] = useState<V1Beta1Subscription[]>([]);

const [allPlans, setAllPlans] = useState<V1Beta1Plan[]>([]);
const [isAllPlansLoading, setIsAllPlansLoading] = useState(false);

const [activePlan, setActivePlan] = useState<V1Beta1Plan>();
const [trialPlan, setTrialPlan] = useState<V1Beta1Plan>();
const [isActivePlanLoading, setIsActivePlanLoading] = useState(false);

const [basePlan, setBasePlan] = useState<V1Beta1Plan>();
Expand Down Expand Up @@ -291,27 +301,42 @@ export const FrontierContextProvider = ({
}, [getFrontierCurrentUserGroups, getFrontierCurrentUserOrganizations, user]);

const getPlan = useCallback(
async (planId: string) => {
async (planId?: string) => {
if (!planId) return;
setIsActivePlanLoading(true);

try {
const resp = await frontierClient?.frontierServiceGetPlan(planId);
const plan = resp?.data?.plan;
if (plan) {
setActivePlan(plan);
}
return resp?.data?.plan;
} catch (err) {
console.error(
'frontier:sdk:: There is problem with fetching active plan'
);
console.error(err);
return;
} finally {
setIsActivePlanLoading(false);
}
},
[frontierClient]
);

const setActiveAndTrialSubscriptions = useCallback(
async (subscriptionsList: V1Beta1Subscription[] = []) => {
const activeSub = getActiveSubscription(subscriptionsList);
setActiveSubscription(activeSub);
const activeSubPlan = await getPlan(activeSub?.plan_id);
setActivePlan(activeSubPlan);

const trialSub = getTrialingSubscription(subscriptionsList);
setTrialSubscription(trialSub);
const trialSubPlan = await getPlan(trialSub?.plan_id);
setTrialPlan(trialSubPlan);

return [activeSub, trialSub];
},
[getPlan]
);

const getSubscription = useCallback(
async (orgId: string, billingId: string) => {
setIsActiveSubscriptionLoading(true);
Expand All @@ -322,17 +347,10 @@ export const FrontierContextProvider = ({
);
const subscriptionsList = resp?.data?.subscriptions || [];
setSubscriptions(subscriptionsList);
if (subscriptionsList.length) {
const activeSub = getActiveSubscription(subscriptionsList);
setActiveSubscription(activeSub);
if (activeSub?.plan_id) {
getPlan(activeSub?.plan_id);
}
return activeSub;
} else {
setActiveSubscription(undefined);
setActivePlan(undefined);
}
const [activeSub] = await setActiveAndTrialSubscriptions(
subscriptionsList
);
return activeSub;
} catch (err: any) {
console.error(
'frontier:sdk:: There is problem with fetching active subscriptions'
Expand All @@ -342,7 +360,7 @@ export const FrontierContextProvider = ({
setIsActiveSubscriptionLoading(false);
}
},
[frontierClient, getPlan]
[frontierClient, setActiveAndTrialSubscriptions]
);

const getBillingAccount = useCallback(
Expand Down Expand Up @@ -449,9 +467,11 @@ export const FrontierContextProvider = ({
setIsBillingAccountLoading,
isActiveSubscriptionLoading,
setIsActiveSubscriptionLoading,
trialSubscription,
activeSubscription,
setActiveSubscription,
subscriptions,
trialPlan,
activePlan,
setActivePlan,
isActivePlanLoading,
Expand Down
10 changes: 10 additions & 0 deletions sdks/js/packages/core/react/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ export const getActiveSubscription = (subscriptions: V1Beta1Subscription[]) => {
return activeSubscriptions[0];
};

export const getTrialingSubscription = (
subscriptions: V1Beta1Subscription[]
) => {
const activeSubscriptions = subscriptions
.filter(sub => sub.state === SUBSCRIPTION_STATES.TRIALING)
.sort((a, b) => (dayjs(a.updated_at).isAfter(b.updated_at) ? -1 : 1));

return activeSubscriptions[0];
};

export interface PlanChangeAction {
btnLabel: string;
btnDoneLabel: string;
Expand Down

0 comments on commit 9565abb

Please sign in to comment.