-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refacto to introduce billing plan instead
- Loading branch information
1 parent
d24680a
commit 3db9401
Showing
22 changed files
with
409 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/twenty-front/src/modules/billing/hooks/__tests__/useBillingPlan.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { MemoryRouter } from 'react-router-dom'; | ||
import { RecoilRoot } from 'recoil'; | ||
|
||
import { useBillingPlan } from '@/billing/hooks/useBillingPlan'; | ||
import { BillingPlanKey } from '@/billing/types/billing'; | ||
|
||
const Wrapper = ({ children, initialUrl = '' }: any) => ( | ||
<MemoryRouter initialEntries={[initialUrl]}> | ||
<RecoilRoot>{children}</RecoilRoot> | ||
</MemoryRouter> | ||
); | ||
|
||
describe('useBillingPlan', () => { | ||
it('should return FREE as default plan', () => { | ||
const { result } = renderHook(() => useBillingPlan(), { | ||
wrapper: Wrapper, | ||
}); | ||
|
||
expect(result.current).toBe(BillingPlanKey.FREE); | ||
}); | ||
|
||
it('should set plan from URL parameter - FREE', () => { | ||
const { result } = renderHook(() => useBillingPlan(), { | ||
wrapper: ({ children }) => ( | ||
<Wrapper initialUrl="?plan=free">{children}</Wrapper> | ||
), | ||
}); | ||
|
||
expect(result.current).toBe(BillingPlanKey.FREE); | ||
}); | ||
|
||
it('should set plan from URL parameter - PRO', () => { | ||
const { result } = renderHook(() => useBillingPlan(), { | ||
wrapper: ({ children }) => ( | ||
<Wrapper initialUrl="?plan=pro">{children}</Wrapper> | ||
), | ||
}); | ||
|
||
expect(result.current).toBe(BillingPlanKey.PRO); | ||
}); | ||
|
||
it('should set plan from URL parameter - ENTERPRISE', () => { | ||
const { result } = renderHook(() => useBillingPlan(), { | ||
wrapper: ({ children }) => ( | ||
<Wrapper initialUrl="?plan=enterprise">{children}</Wrapper> | ||
), | ||
}); | ||
|
||
expect(result.current).toBe(BillingPlanKey.ENTERPRISE); | ||
}); | ||
|
||
it('should ignore invalid plan from URL parameter', () => { | ||
const { result } = renderHook(() => useBillingPlan(), { | ||
wrapper: ({ children }) => ( | ||
<Wrapper initialUrl="?plan=invalid">{children}</Wrapper> | ||
), | ||
}); | ||
|
||
expect(result.current).toBe(BillingPlanKey.FREE); | ||
}); | ||
|
||
it('should handle URL without plan parameter', () => { | ||
const { result } = renderHook(() => useBillingPlan(), { | ||
wrapper: ({ children }) => ( | ||
<Wrapper initialUrl="?other=param">{children}</Wrapper> | ||
), | ||
}); | ||
|
||
expect(result.current).toBe(BillingPlanKey.FREE); | ||
}); | ||
}); |
38 changes: 38 additions & 0 deletions
38
packages/twenty-front/src/modules/billing/hooks/useBillingPlan.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useLocation } from 'react-router-dom'; | ||
import { atom, useRecoilState } from 'recoil'; | ||
|
||
import { BillingPlanKey } from '@/billing/types/billing'; | ||
|
||
const billingPlanState = atom<BillingPlanKey | null>({ | ||
key: 'billingPlanState', | ||
default: null, | ||
}); | ||
|
||
export const useBillingPlan = () => { | ||
const { search } = useLocation(); | ||
const [billingPlan, setBillingPlan] = useRecoilState(billingPlanState); | ||
|
||
const hasFreePassParameter = | ||
search.includes('freepass') || | ||
search.includes('freePass') || | ||
search.includes('free-pass') || | ||
search.includes('Free-pass') || | ||
search.includes('FreePass'); | ||
|
||
if (hasFreePassParameter) { | ||
setBillingPlan(BillingPlanKey.FREE); | ||
return billingPlan; | ||
} | ||
|
||
const planFromUrl = search.match(/[?&]plan=([^&]+)/)?.[1]?.toUpperCase(); | ||
|
||
if ( | ||
planFromUrl !== null && | ||
planFromUrl !== undefined && | ||
Object.values(BillingPlanKey).includes(planFromUrl as BillingPlanKey) | ||
) { | ||
setBillingPlan(planFromUrl as BillingPlanKey); | ||
} | ||
|
||
return billingPlan; | ||
}; |
21 changes: 0 additions & 21 deletions
21
packages/twenty-front/src/modules/billing/hooks/useFreePass.ts
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
packages/twenty-front/src/modules/billing/states/freePassState.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export enum BillingPlanKey { | ||
FREE = 'FREE', | ||
PRO = 'PRO', | ||
ENTERPRISE = 'ENTERPRISE', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.