Skip to content

Commit 6d11ad2

Browse files
authored
Merge pull request Expensify#70871 from allgandalf/fix/66459
Remove Onyx.connect() for the key: ONYXKEYS.NVP_INTRO_SELECTED in src/libs/SubscriptionUtils.ts
2 parents df844f7 + 669f36b commit 6d11ad2

File tree

5 files changed

+46
-14
lines changed

5 files changed

+46
-14
lines changed

src/libs/SubscriptionUtils.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,6 @@ Onyx.connect({
175175
},
176176
});
177177

178-
let introSelected: OnyxEntry<IntroSelected>;
179-
Onyx.connect({
180-
key: ONYXKEYS.NVP_INTRO_SELECTED,
181-
callback: (value) => (introSelected = value),
182-
});
183-
184178
/**
185179
* @returns The date when the grace period ends.
186180
*/
@@ -244,7 +238,7 @@ function hasInsufficientFundsError() {
244238
return billingStatus?.declineReason === 'insufficient_funds' && getAmountOwed() !== 0;
245239
}
246240

247-
function shouldShowPreTrialBillingBanner(): boolean {
241+
function shouldShowPreTrialBillingBanner(introSelected: OnyxEntry<IntroSelected>): boolean {
248242
// We don't want to show the Pre Trial banner if the user was a Test Drive Receiver that created their workspace
249243
// with the promo code.
250244
const wasUserTestDriveReceiver = introSelected?.previousChoices?.some((choice) => choice === CONST.ONBOARDING_CHOICES.TEST_DRIVE_RECEIVER);
@@ -488,13 +482,13 @@ function calculateRemainingFreeTrialDays(): number {
488482
* @param policies - The policies collection.
489483
* @returns The free trial badge text .
490484
*/
491-
function getFreeTrialText(policies: OnyxCollection<Policy> | null): string | undefined {
485+
function getFreeTrialText(policies: OnyxCollection<Policy> | null, introSelected: OnyxEntry<IntroSelected>): string | undefined {
492486
const ownedPaidPolicies = getOwnedPaidPolicies(policies, currentUserAccountID);
493487
if (isEmptyObject(ownedPaidPolicies)) {
494488
return undefined;
495489
}
496490

497-
if (shouldShowPreTrialBillingBanner()) {
491+
if (shouldShowPreTrialBillingBanner(introSelected)) {
498492
return translateLocal('subscription.billingBanner.preTrial.title');
499493
}
500494
if (isUserOnFreeTrial()) {

src/pages/settings/InitialSettingsPage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ function InitialSettingsPage({currentUserPersonalDetails}: InitialSettingsPagePr
9292
const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: true});
9393
const [stripeCustomerId] = useOnyx(ONYXKEYS.NVP_PRIVATE_STRIPE_CUSTOMER_ID, {canBeMissing: true});
9494
const [session] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: false});
95+
const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {canBeMissing: true});
9596

9697
const {shouldUseNarrowLayout} = useResponsiveLayout();
9798
const network = useNetwork();
@@ -111,7 +112,7 @@ function InitialSettingsPage({currentUserPersonalDetails}: InitialSettingsPagePr
111112

112113
const shouldLogout = useRef(false);
113114

114-
const freeTrialText = getFreeTrialText(policies);
115+
const freeTrialText = getFreeTrialText(policies, introSelected);
115116

116117
const shouldDisplayLHB = !shouldUseNarrowLayout;
117118

src/pages/settings/Subscription/CardSection/CardSection.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function CardSection() {
5252
const [session] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: true});
5353
const [fundList] = useOnyx(ONYXKEYS.FUND_LIST, {canBeMissing: true});
5454
const [purchaseList] = useOnyx(ONYXKEYS.PURCHASE_LIST, {canBeMissing: true});
55+
const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {canBeMissing: true});
5556
const hasTeam2025Pricing = useHasTeam2025Pricing();
5657
const subscriptionPlan = useSubscriptionPlan();
5758
const [subscriptionRetryBillingStatusPending] = useOnyx(ONYXKEYS.SUBSCRIPTION_RETRY_BILLING_STATUS_PENDING, {canBeMissing: true});
@@ -129,7 +130,7 @@ function CardSection() {
129130
let BillingBanner: React.ReactNode | undefined;
130131
if (shouldShowDiscountBanner(hasTeam2025Pricing, subscriptionPlan)) {
131132
BillingBanner = <EarlyDiscountBanner isSubscriptionPage />;
132-
} else if (shouldShowPreTrialBillingBanner()) {
133+
} else if (shouldShowPreTrialBillingBanner(introSelected)) {
133134
BillingBanner = <PreTrialBillingBanner />;
134135
} else if (isUserOnFreeTrial()) {
135136
BillingBanner = <TrialStartedBillingBanner />;

src/pages/settings/Subscription/FreeTrial.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function FreeTrial({badgeStyles, pressable = false, addSpacing = false, success
2626
const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: true});
2727
const [firstDayFreeTrial] = useOnyx(ONYXKEYS.NVP_FIRST_DAY_FREE_TRIAL, {canBeMissing: true});
2828
const [lastDayFreeTrial] = useOnyx(ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL, {canBeMissing: true});
29+
const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {canBeMissing: true});
2930
const privateSubscription = usePrivateSubscription();
3031

3132
const [freeTrialText, setFreeTrialText] = useState<string | undefined>(undefined);
@@ -35,8 +36,8 @@ function FreeTrial({badgeStyles, pressable = false, addSpacing = false, success
3536
if (!privateSubscription && !isOffline) {
3637
return;
3738
}
38-
setFreeTrialText(getFreeTrialText(policies));
39-
}, [isOffline, privateSubscription, policies, firstDayFreeTrial, lastDayFreeTrial]);
39+
setFreeTrialText(getFreeTrialText(policies, introSelected));
40+
}, [isOffline, privateSubscription, policies, firstDayFreeTrial, lastDayFreeTrial, introSelected]);
4041

4142
if (!freeTrialText) {
4243
return null;

tests/unit/SubscriptionUtilsTest.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {act} from '@testing-library/react-native';
12
import {addDays, addMinutes, format as formatDate, getUnixTime, subDays} from 'date-fns';
23
import Onyx from 'react-native-onyx';
34
import type {OnyxEntry} from 'react-native-onyx';
@@ -11,12 +12,14 @@ import {
1112
PAYMENT_STATUS,
1213
shouldRestrictUserBillableActions,
1314
shouldShowDiscountBanner,
15+
shouldShowPreTrialBillingBanner,
1416
} from '@libs/SubscriptionUtils';
1517
import CONST from '@src/CONST';
1618
import ONYXKEYS from '@src/ONYXKEYS';
17-
import type {BillingGraceEndPeriod, BillingStatus, FundList, StripeCustomerID} from '@src/types/onyx';
19+
import type {BillingGraceEndPeriod, BillingStatus, FundList, IntroSelected, StripeCustomerID} from '@src/types/onyx';
1820
import createRandomPolicy from '../utils/collections/policies';
1921
import {STRIPE_CUSTOMER_ID} from '../utils/TestHelper';
22+
import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct';
2023

2124
const billingGraceEndPeriod: BillingGraceEndPeriod = {
2225
value: 0,
@@ -624,4 +627,36 @@ describe('SubscriptionUtils', () => {
624627
expect(getEarlyDiscountInfo()).toBeNull();
625628
});
626629
});
630+
describe('shouldShowPreTrialBillingBanner', () => {
631+
it('should return true if the user is NOT on a free trial and trial has not ended', async () => {
632+
// Free trial starts in the future → user is not currently on trial
633+
await act(async () => {
634+
await Onyx.multiSet({
635+
[ONYXKEYS.NVP_FIRST_DAY_FREE_TRIAL]: formatDate(addDays(new Date(), 5), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
636+
[ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL]: formatDate(addDays(new Date(), 10), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
637+
});
638+
});
639+
640+
await waitForBatchedUpdatesWithAct();
641+
642+
const introSelected: OnyxEntry<IntroSelected> = {
643+
choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM,
644+
};
645+
646+
expect(shouldShowPreTrialBillingBanner(introSelected)).toBeTruthy();
647+
});
648+
649+
it('should return false if the free trial has ended', async () => {
650+
await Onyx.multiSet({
651+
[ONYXKEYS.NVP_FIRST_DAY_FREE_TRIAL]: formatDate(subDays(new Date(), 20), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
652+
[ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL]: formatDate(subDays(new Date(), 5), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
653+
});
654+
655+
const introSelected: OnyxEntry<IntroSelected> = {
656+
choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM,
657+
};
658+
659+
expect(shouldShowPreTrialBillingBanner(introSelected)).toBeFalsy();
660+
});
661+
});
627662
});

0 commit comments

Comments
 (0)