-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Payment card / Subscription] Integrate “Your plan” section with backend data and related screens #43128
[Payment card / Subscription] Integrate “Your plan” section with backend data and related screens #43128
Changes from all commits
044c287
4ec5cc3
6e5e314
eb0f8d0
56a64f1
02c0354
6f67061
457b4a2
1e04b25
0472281
1f77593
50880d2
1385573
b330d98
e393f85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {useMemo} from 'react'; | ||
import {useOnyx} from 'react-native-onyx'; | ||
import type {ValueOf} from 'type-fest'; | ||
import CONST from '@src/CONST'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
|
||
type PreferredCurrency = ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>; | ||
|
||
/** | ||
* Get user's preferred currency in the following order: | ||
* | ||
* 1. Payment card currency | ||
* 2. User's local currency (if it's a valid payment card currency) | ||
* 3. USD (default currency) | ||
* | ||
*/ | ||
function usePreferredCurrency(): PreferredCurrency { | ||
const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST); | ||
const [session] = useOnyx(ONYXKEYS.SESSION); | ||
const [fundList] = useOnyx(ONYXKEYS.FUND_LIST); | ||
|
||
const paymentCardCurrency = useMemo(() => Object.values(fundList ?? {}).find((card) => card.accountData?.additionalData?.isBillingCard)?.accountData?.currency, [fundList]); | ||
|
||
if (paymentCardCurrency) { | ||
return paymentCardCurrency; | ||
} | ||
|
||
const currentUserLocalCurrency = (personalDetails?.[session?.accountID ?? '-1']?.localCurrencyCode ?? CONST.PAYMENT_CARD_CURRENCY.USD) as PreferredCurrency; | ||
|
||
return Object.values(CONST.PAYMENT_CARD_CURRENCY).includes(currentUserLocalCurrency) ? currentUserLocalCurrency : CONST.PAYMENT_CARD_CURRENCY.USD; | ||
} | ||
|
||
export default usePreferredCurrency; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import {useOnyx} from 'react-native-onyx'; | ||
import CONST from '@src/CONST'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import usePreferredCurrency from './usePreferredCurrency'; | ||
import useSubscriptionPlan from './useSubscriptionPlan'; | ||
|
||
const SUBSCRIPTION_PRICES = { | ||
[CONST.PAYMENT_CARD_CURRENCY.USD]: { | ||
[CONST.POLICY.TYPE.CORPORATE]: { | ||
[CONST.SUBSCRIPTION.TYPE.ANNUAL]: 900, | ||
[CONST.SUBSCRIPTION.TYPE.PAYPERUSE]: 1800, | ||
}, | ||
[CONST.POLICY.TYPE.TEAM]: { | ||
[CONST.SUBSCRIPTION.TYPE.ANNUAL]: 500, | ||
[CONST.SUBSCRIPTION.TYPE.PAYPERUSE]: 1000, | ||
}, | ||
}, | ||
[CONST.PAYMENT_CARD_CURRENCY.AUD]: { | ||
[CONST.POLICY.TYPE.CORPORATE]: { | ||
[CONST.SUBSCRIPTION.TYPE.ANNUAL]: 1500, | ||
[CONST.SUBSCRIPTION.TYPE.PAYPERUSE]: 3000, | ||
}, | ||
[CONST.POLICY.TYPE.TEAM]: { | ||
[CONST.SUBSCRIPTION.TYPE.ANNUAL]: 700, | ||
[CONST.SUBSCRIPTION.TYPE.PAYPERUSE]: 1400, | ||
}, | ||
}, | ||
[CONST.PAYMENT_CARD_CURRENCY.GBP]: { | ||
[CONST.POLICY.TYPE.CORPORATE]: { | ||
[CONST.SUBSCRIPTION.TYPE.ANNUAL]: 700, | ||
[CONST.SUBSCRIPTION.TYPE.PAYPERUSE]: 1400, | ||
}, | ||
[CONST.POLICY.TYPE.TEAM]: { | ||
[CONST.SUBSCRIPTION.TYPE.ANNUAL]: 400, | ||
[CONST.SUBSCRIPTION.TYPE.PAYPERUSE]: 800, | ||
}, | ||
}, | ||
[CONST.PAYMENT_CARD_CURRENCY.NZD]: { | ||
[CONST.POLICY.TYPE.CORPORATE]: { | ||
[CONST.SUBSCRIPTION.TYPE.ANNUAL]: 1600, | ||
[CONST.SUBSCRIPTION.TYPE.PAYPERUSE]: 3200, | ||
}, | ||
[CONST.POLICY.TYPE.TEAM]: { | ||
[CONST.SUBSCRIPTION.TYPE.ANNUAL]: 800, | ||
[CONST.SUBSCRIPTION.TYPE.PAYPERUSE]: 1600, | ||
}, | ||
}, | ||
} as const; | ||
|
||
function useSubscriptionPrice(): number { | ||
const preferredCurrency = usePreferredCurrency(); | ||
const subscriptionPlan = useSubscriptionPlan(); | ||
const [privateSubscription] = useOnyx(ONYXKEYS.NVP_PRIVATE_SUBSCRIPTION); | ||
|
||
if (!subscriptionPlan || !privateSubscription?.type) { | ||
return 0; | ||
} | ||
|
||
return SUBSCRIPTION_PRICES[preferredCurrency][subscriptionPlan][privateSubscription.type]; | ||
} | ||
|
||
export default useSubscriptionPrice; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,6 +129,26 @@ function convertToDisplayString(amountInCents = 0, currency: string = CONST.CURR | |
}); | ||
} | ||
|
||
/** | ||
* Given the amount in the "cents", convert it to a short string (no decimals) for display in the UI. | ||
* The backend always handle things in "cents" (subunit equal to 1/100) | ||
* | ||
* @param amountInCents – should be an integer. Anything after a decimal place will be dropped. | ||
* @param currency - IOU currency | ||
*/ | ||
function convertToShortDisplayString(amountInCents = 0, currency: string = CONST.CURRENCY.USD): string { | ||
JKobrynski marked this conversation as resolved.
Show resolved
Hide resolved
JKobrynski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const convertedAmount = convertToFrontendAmountAsInteger(amountInCents); | ||
|
||
return NumberFormatUtils.format(BaseLocaleListener.getPreferredLocale(), convertedAmount, { | ||
style: 'currency', | ||
currency, | ||
|
||
// There will be no decimals displayed (e.g. $9) | ||
minimumFractionDigits: 0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just out of curiosity, does adding this line fix the crash on Android? If so, why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it does fix it! Hard to say why, it just doesn't work without it, the app crashes and doesn't show any useful crash details, even when using |
||
maximumFractionDigits: 0, | ||
}); | ||
} | ||
|
||
/** | ||
* Given an amount, convert it to a string for display in the UI. | ||
* | ||
|
@@ -184,4 +204,5 @@ export { | |
convertAmountToDisplayString, | ||
convertToDisplayStringWithoutCurrency, | ||
isValidCurrencyCode, | ||
convertToShortDisplayString, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it better to move it to CONST.ts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its values are based on
CONST
, so it's impossible to put it in that object, and I also can't see any otherconst
being exported from that file, only typesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JKobrynski Hmm, I was thinking if we should code subscription prices in CONST.ts too - then it'll be easier for the team to maintain them, like team members from business team
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree this is not ideal but I'm also not seeing a better way to handle it so I think it is what it is