Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixMalfait committed Dec 19, 2024
1 parent 0e55d5b commit ec9f94c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useIsLogged } from '@/auth/hooks/useIsLogged';
import { useFreePass } from '@/billing/hooks/useFreePass';
import { useDefaultHomePagePath } from '@/navigation/hooks/useDefaultHomePagePath';
import { useOnboardingStatus } from '@/onboarding/hooks/useOnboardingStatus';
import { AppPath } from '@/types/AppPath';
Expand Down Expand Up @@ -38,6 +39,11 @@ const setupMockIsLogged = (isLogged: boolean) => {
jest.mocked(useIsLogged).mockReturnValueOnce(isLogged);
};

jest.mock('@/billing/hooks/useFreePass');
const setupMockFreePass = (freePass: boolean) => {
jest.mocked(useFreePass).mockReturnValueOnce(freePass);
};

const defaultHomePagePath = '/objects/companies';

jest.mock('@/navigation/hooks/useDefaultHomePagePath');
Expand Down Expand Up @@ -146,6 +152,17 @@ const testCases = [
{ loc: AppPath.PlanRequired, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.InviteTeam, res: AppPath.InviteTeam },
{ loc: AppPath.PlanRequired, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.Completed, res: defaultHomePagePath },

{ loc: AppPath.FreePassCheckout, isLoggedIn: true, subscriptionStatus: undefined, onboardingStatus: OnboardingStatus.PlanRequired, res: undefined },
{ loc: AppPath.FreePassCheckout, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Canceled, onboardingStatus: OnboardingStatus.Completed, res: '/settings/billing' },
{ loc: AppPath.FreePassCheckout, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Unpaid, onboardingStatus: OnboardingStatus.Completed, res: '/settings/billing' },
{ loc: AppPath.FreePassCheckout, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.PastDue, onboardingStatus: OnboardingStatus.Completed, res: defaultHomePagePath },
{ loc: AppPath.FreePassCheckout, isLoggedIn: false, subscriptionStatus: undefined, onboardingStatus: undefined, res: AppPath.SignInUp },
{ loc: AppPath.FreePassCheckout, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.WorkspaceActivation, res: AppPath.CreateWorkspace },
{ loc: AppPath.FreePassCheckout, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.ProfileCreation, res: AppPath.CreateProfile },
{ loc: AppPath.FreePassCheckout, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.SyncEmail, res: AppPath.SyncEmails },
{ loc: AppPath.FreePassCheckout, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.InviteTeam, res: AppPath.InviteTeam },
{ loc: AppPath.FreePassCheckout, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.Completed, res: defaultHomePagePath },

{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: undefined, onboardingStatus: OnboardingStatus.PlanRequired, res: AppPath.PlanRequired },
{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Canceled, onboardingStatus: OnboardingStatus.Completed, res: '/settings/billing' },
{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Unpaid, onboardingStatus: OnboardingStatus.Completed, res: '/settings/billing' },
Expand Down Expand Up @@ -275,6 +292,7 @@ describe('usePageChangeEffectNavigateLocation', () => {
setupMockOnboardingStatus(testCase.onboardingStatus);
setupMockSubscriptionStatus(testCase.subscriptionStatus);
setupMockIsLogged(testCase.isLoggedIn);
setupMockFreePass(false);
expect(usePageChangeEffectNavigateLocation()).toEqual(testCase.res);
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { useIsLogged } from '@/auth/hooks/useIsLogged';
import { freePassState } from '@/billing/states/freePassState';
import { useFreePass } from '@/billing/hooks/useFreePass';
import { useDefaultHomePagePath } from '@/navigation/hooks/useDefaultHomePagePath';
import { useOnboardingStatus } from '@/onboarding/hooks/useOnboardingStatus';
import { AppPath } from '@/types/AppPath';
import { SettingsPath } from '@/types/SettingsPath';
import { useSubscriptionStatus } from '@/workspace/hooks/useSubscriptionStatus';
import { useLocation } from 'react-router-dom';
import { useRecoilState } from 'recoil';
import { OnboardingStatus, SubscriptionStatus } from '~/generated/graphql';
import { useIsMatchingLocation } from '~/hooks/useIsMatchingLocation';

Expand All @@ -16,21 +14,7 @@ export const usePageChangeEffectNavigateLocation = () => {
const onboardingStatus = useOnboardingStatus();
const subscriptionStatus = useSubscriptionStatus();
const { defaultHomePagePath } = useDefaultHomePagePath();

const { search } = useLocation();

const [freePass, setFreePass] = useRecoilState(freePassState);

const hasFreePassParameter =
search.includes('freepass') ||
search.includes('freePass') ||
search.includes('free-pass') ||
search.includes('Free-pass') ||
search.includes('FreePass');

if (hasFreePassParameter) {
setFreePass(true);
}
const freePass = useFreePass();

const isMatchingOpenRoute =
isMatchingLocation(AppPath.Invite) ||
Expand All @@ -48,7 +32,8 @@ export const usePageChangeEffectNavigateLocation = () => {
isMatchingLocation(AppPath.SyncEmails) ||
isMatchingLocation(AppPath.InviteTeam) ||
isMatchingLocation(AppPath.PlanRequired) ||
isMatchingLocation(AppPath.PlanRequiredSuccess);
isMatchingLocation(AppPath.PlanRequiredSuccess) ||
isMatchingLocation(AppPath.FreePassCheckout);

if (isMatchingOpenRoute) {
return;
Expand All @@ -60,7 +45,8 @@ export const usePageChangeEffectNavigateLocation = () => {

if (
onboardingStatus === OnboardingStatus.PlanRequired &&
!isMatchingLocation(AppPath.PlanRequired)
!isMatchingLocation(AppPath.PlanRequired) &&
!isMatchingLocation(AppPath.FreePassCheckout)
) {
return freePass ? AppPath.FreePassCheckout : AppPath.PlanRequired;
}
Expand Down
21 changes: 21 additions & 0 deletions packages/twenty-front/src/modules/billing/hooks/useFreePass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { freePassState } from '@/billing/states/freePassState';
import { useLocation } from 'react-router-dom';
import { useRecoilState } from 'recoil';

export const useFreePass = () => {
const { search } = useLocation();
const [freePass, setFreePass] = useRecoilState(freePassState);

const hasFreePassParameter =
search.includes('freepass') ||
search.includes('freePass') ||
search.includes('free-pass') ||
search.includes('Free-pass') ||
search.includes('FreePass');

if (hasFreePassParameter) {
setFreePass(true);
}

return freePass;
};

0 comments on commit ec9f94c

Please sign in to comment.