Skip to content

Commit

Permalink
Merge pull request #44538 from MrMuzyk/feat/subscription-localize-cur…
Browse files Browse the repository at this point in the history
…rency
  • Loading branch information
blimpich authored Jul 1, 2024
2 parents f5d424c + 1640355 commit e9f73c9
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 38 deletions.
4 changes: 0 additions & 4 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4917,10 +4917,6 @@ const CONST = {
},

SUBSCRIPTION_PRICE_FACTOR: 2,
SUBSCRIPTION_POSSIBLE_COST_SAVINGS: {
COLLECT_PLAN: 10,
CONTROL_PLAN: 18,
},
FEEDBACK_SURVEY_OPTIONS: {
TOO_LIMITED: {
ID: 'tooLimited',
Expand Down
14 changes: 7 additions & 7 deletions src/components/AddPaymentCard/PaymentCardChangeCurrencyForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import PaymentCardCurrencyHeader from './PaymentCardCurrencyHeader';
import PaymentCardCurrencyModal from './PaymentCardCurrencyModal';

type PaymentCardFormProps = {
initialCurrency?: ValueOf<typeof CONST.CURRENCY>;
initialCurrency?: ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>;
isSecurityCodeRequired?: boolean;
changeBillingCurrency: (currency?: ValueOf<typeof CONST.CURRENCY>, values?: FormOnyxValues<typeof ONYXKEYS.FORMS.CHANGE_BILLING_CURRENCY_FORM>) => void;
changeBillingCurrency: (currency?: ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>, values?: FormOnyxValues<typeof ONYXKEYS.FORMS.CHANGE_BILLING_CURRENCY_FORM>) => void;
};

const REQUIRED_FIELDS = [INPUT_IDS.SECURITY_CODE];
Expand All @@ -30,7 +30,7 @@ function PaymentCardChangeCurrencyForm({changeBillingCurrency, isSecurityCodeReq
const {translate} = useLocalize();

const [isCurrencyModalVisible, setIsCurrencyModalVisible] = useState(false);
const [currency, setCurrency] = useState<ValueOf<typeof CONST.CURRENCY>>(initialCurrency ?? CONST.CURRENCY.USD);
const [currency, setCurrency] = useState<ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>>(initialCurrency ?? CONST.PAYMENT_CARD_CURRENCY.USD);

const validate = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.CHANGE_BILLING_CURRENCY_FORM>): FormInputErrors<typeof ONYXKEYS.FORMS.CHANGE_BILLING_CURRENCY_FORM> => {
const errors = ValidationUtils.getFieldRequiredErrors(values, REQUIRED_FIELDS);
Expand All @@ -46,7 +46,7 @@ function PaymentCardChangeCurrencyForm({changeBillingCurrency, isSecurityCodeReq
() => ({
sections: [
{
data: (Object.keys(CONST.CURRENCY) as Array<ValueOf<typeof CONST.CURRENCY>>).map((currencyItem) => ({
data: (Object.keys(CONST.PAYMENT_CARD_CURRENCY) as Array<ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>>).map((currencyItem) => ({
text: currencyItem,
value: currencyItem,
keyForList: currencyItem,
Expand All @@ -62,13 +62,13 @@ function PaymentCardChangeCurrencyForm({changeBillingCurrency, isSecurityCodeReq
setIsCurrencyModalVisible(true);
}, []);

const changeCurrency = useCallback((selectedCurrency: ValueOf<typeof CONST.CURRENCY>) => {
const changeCurrency = useCallback((selectedCurrency: ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>) => {
setCurrency(selectedCurrency);
setIsCurrencyModalVisible(false);
}, []);

const selectCurrency = useCallback(
(selectedCurrency: ValueOf<typeof CONST.CURRENCY>) => {
(selectedCurrency: ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>) => {
setCurrency(selectedCurrency);
changeBillingCurrency(selectedCurrency);
},
Expand Down Expand Up @@ -109,7 +109,7 @@ function PaymentCardChangeCurrencyForm({changeBillingCurrency, isSecurityCodeReq
</>
<PaymentCardCurrencyModal
isVisible={isCurrencyModalVisible}
currencies={Object.keys(CONST.CURRENCY) as Array<ValueOf<typeof CONST.CURRENCY>>}
currencies={Object.keys(CONST.PAYMENT_CARD_CURRENCY) as Array<ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>>}
currentCurrency={currency}
onCurrencyChange={changeCurrency}
onClose={() => setIsCurrencyModalVisible(false)}
Expand Down
8 changes: 4 additions & 4 deletions src/components/AddPaymentCard/PaymentCardCurrencyModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ type PaymentCardCurrencyModalProps = {
isVisible: boolean;

/** The list of currencies to render */
currencies: Array<ValueOf<typeof CONST.CURRENCY>>;
currencies: Array<ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>>;

/** Currently selected currency */
currentCurrency: ValueOf<typeof CONST.CURRENCY>;
currentCurrency: ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>;

/** Function to call when the user selects a currency */
onCurrencyChange?: (currency: ValueOf<typeof CONST.CURRENCY>) => void;
onCurrencyChange?: (currency: ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>) => void;

/** Function to call when the user closes the currency picker */
onClose?: () => void;
};

function PaymentCardCurrencyModal({isVisible, currencies, currentCurrency = CONST.CURRENCY.USD, onCurrencyChange, onClose}: PaymentCardCurrencyModalProps) {
function PaymentCardCurrencyModal({isVisible, currencies, currentCurrency = CONST.PAYMENT_CARD_CURRENCY.USD, onCurrencyChange, onClose}: PaymentCardCurrencyModalProps) {
const {isSmallScreenWidth} = useWindowDimensions();
const styles = useThemeStyles();
const {translate} = useLocalize();
Expand Down
4 changes: 2 additions & 2 deletions src/components/AddPaymentCard/PaymentCardForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type PaymentCardFormProps = {
showCurrencyField?: boolean;
showStateSelector?: boolean;
isDebitCard?: boolean;
addPaymentCard: (values: FormOnyxValues<typeof ONYXKEYS.FORMS.ADD_PAYMENT_CARD_FORM>, currency?: ValueOf<typeof CONST.CURRENCY>) => void;
addPaymentCard: (values: FormOnyxValues<typeof ONYXKEYS.FORMS.ADD_PAYMENT_CARD_FORM>, currency?: ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>) => void;
submitButtonText: string;
/** Custom content to display in the footer after card form */
footerContent?: ReactNode;
Expand Down Expand Up @@ -295,7 +295,7 @@ function PaymentCardForm({
<View style={[styles.mt4, styles.mhn5]}>
<InputWrapper
currencySelectorRoute={currencySelectorRoute}
value={data?.currency ?? CONST.CURRENCY.USD}
value={data?.currency ?? CONST.PAYMENT_CARD_CURRENCY.USD}
InputComponent={CurrencySelector}
inputID={INPUT_IDS.CURRENCY}
/>
Expand Down
38 changes: 38 additions & 0 deletions src/hooks/useSubscriptionPossibleCostSavings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 POSSIBLE_COST_SAVINGS = {
[CONST.PAYMENT_CARD_CURRENCY.USD]: {
[CONST.POLICY.TYPE.TEAM]: 1000,
[CONST.POLICY.TYPE.CORPORATE]: 1800,
},
[CONST.PAYMENT_CARD_CURRENCY.AUD]: {
[CONST.POLICY.TYPE.TEAM]: 1400,
[CONST.POLICY.TYPE.CORPORATE]: 3000,
},
[CONST.PAYMENT_CARD_CURRENCY.GBP]: {
[CONST.POLICY.TYPE.TEAM]: 800,
[CONST.POLICY.TYPE.CORPORATE]: 1400,
},
[CONST.PAYMENT_CARD_CURRENCY.NZD]: {
[CONST.POLICY.TYPE.TEAM]: 1600,
[CONST.POLICY.TYPE.CORPORATE]: 3200,
},
} as const;

function useSubscriptionPossibleCostSavings(): number {
const preferredCurrency = usePreferredCurrency();
const subscriptionPlan = useSubscriptionPlan();
const [privateSubscription] = useOnyx(ONYXKEYS.NVP_PRIVATE_SUBSCRIPTION);

if (!subscriptionPlan || !privateSubscription?.type) {
return 0;
}

return POSSIBLE_COST_SAVINGS[preferredCurrency][subscriptionPlan];
}

export default useSubscriptionPossibleCostSavings;
2 changes: 1 addition & 1 deletion src/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3518,7 +3518,7 @@ export default {
title: 'Subscription settings',
autoRenew: 'Auto-renew',
autoIncrease: 'Auto-increase annual seats',
saveUpTo: ({amountSaved}) => `Save up to $${amountSaved}/month per active member`,
saveUpTo: ({amountWithCurrency}) => `Save up to ${amountWithCurrency}/month per active member`,
automaticallyIncrease:
'Automatically increase your annual seats to accommodate for active members that exceed your subscription size. Note: This will extend your annual subscription end date.',
disableAutoRenew: 'Disable auto-renew',
Expand Down
2 changes: 1 addition & 1 deletion src/languages/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4020,7 +4020,7 @@ export default {
title: 'Configuración de suscripción',
autoRenew: 'Auto-renovación',
autoIncrease: 'Auto-incremento',
saveUpTo: ({amountSaved}) => `Ahorre hasta $${amountSaved} al mes por miembro activo`,
saveUpTo: ({amountWithCurrency}) => `Ahorre hasta ${amountWithCurrency} al mes por miembro activo`,
automaticallyIncrease:
'Aumenta automáticamente tus plazas anuales para dar lugar a los miembros activos que superen el tamaño de tu suscripción. Nota: Esto ampliará la fecha de finalización de tu suscripción anual.',
disableAutoRenew: 'Desactivar auto-renovación',
Expand Down
2 changes: 1 addition & 1 deletion src/libs/API/parameters/AddPaymentCardParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type AddPaymentCardParams = {
cardCVV: string;
addressName: string;
addressZip: string;
currency: ValueOf<typeof CONST.CURRENCY>;
currency: ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>;
isP2PDebitCard: boolean;
};
export default AddPaymentCardParams;
2 changes: 1 addition & 1 deletion src/libs/API/parameters/UpdateBillingCurrencyParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {ValueOf} from 'type-fest';
import type CONST from '@src/CONST';

type UpdateBillingCurrencyParams = {
currency: ValueOf<typeof CONST.CURRENCY>;
currency: ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>;
cardCVV: string;
};

Expand Down
12 changes: 6 additions & 6 deletions src/libs/actions/PaymentMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function addPaymentCard(params: PaymentCardParams) {
cardCVV: params.securityCode,
addressName: params.nameOnCard,
addressZip: params.addressZipCode,
currency: CONST.CURRENCY.USD,
currency: CONST.PAYMENT_CARD_CURRENCY.USD,
isP2PDebitCard: true,
};

Expand Down Expand Up @@ -214,7 +214,7 @@ function addSubscriptionPaymentCard(cardData: {
cardCVV: string;
addressName: string;
addressZip: string;
currency: ValueOf<typeof CONST.CURRENCY>;
currency: ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>;
}) {
const {cardNumber, cardYear, cardMonth, cardCVV, addressName, addressZip, currency} = cardData;

Expand Down Expand Up @@ -253,7 +253,7 @@ function addSubscriptionPaymentCard(cardData: {
},
];

if (currency === CONST.CURRENCY.GBP) {
if (currency === CONST.PAYMENT_CARD_CURRENCY.GBP) {
// eslint-disable-next-line rulesdir/no-api-side-effects-method
API.makeRequestWithSideEffects(SIDE_EFFECT_REQUEST_COMMANDS.ADD_PAYMENT_CARD_GBR, parameters, {optimisticData, successData, failureData}).then((response) => {
if (response?.jsonCode !== CONST.JSON_CODE.SUCCESS) {
Expand Down Expand Up @@ -290,7 +290,7 @@ function clearPaymentCardFormErrorAndSubmit() {
[INPUT_IDS.ADDRESS_ZIP_CODE]: '',
[INPUT_IDS.ADDRESS_STATE]: '',
[INPUT_IDS.ACCEPT_TERMS]: '',
[INPUT_IDS.CURRENCY]: CONST.CURRENCY.USD,
[INPUT_IDS.CURRENCY]: CONST.PAYMENT_CARD_CURRENCY.USD,
});
}

Expand All @@ -306,7 +306,7 @@ function clearPaymentCard3dsVerification() {
* Set currency for payments
*
*/
function setPaymentMethodCurrency(currency: ValueOf<typeof CONST.CURRENCY>) {
function setPaymentMethodCurrency(currency: ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>) {
Onyx.merge(ONYXKEYS.FORMS.ADD_PAYMENT_CARD_FORM, {
[INPUT_IDS.CURRENCY]: currency,
});
Expand Down Expand Up @@ -465,7 +465,7 @@ function deletePaymentCard(fundID: number) {
* Call the API to change billing currency.
*
*/
function updateBillingCurrency(currency: ValueOf<typeof CONST.CURRENCY>, cardCVV: string) {
function updateBillingCurrency(currency: ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>, cardCVV: string) {
const parameters: UpdateBillingCurrencyParams = {
cardCVV,
currency,
Expand Down
2 changes: 1 addition & 1 deletion src/pages/settings/PaymentCard/ChangeCurrency/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function ChangeCurrency() {
const {translate} = useLocalize();
const [debitCardForm] = useOnyx(ONYXKEYS.FORMS.ADD_PAYMENT_CARD_FORM);

const changeCurrency = useCallback((currency?: ValueOf<typeof CONST.CURRENCY>) => {
const changeCurrency = useCallback((currency?: ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>) => {
if (currency) {
PaymentMethods.setPaymentMethodCurrency(currency);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ function ChangeBillingCurrency() {
const [fundList] = useOnyx(ONYXKEYS.FUND_LIST);
const defaultCard = useMemo(() => Object.values(fundList ?? {}).find((card) => card.accountData?.additionalData?.isBillingCard), [fundList]);

const changeBillingCurrency = useCallback((currency?: ValueOf<typeof CONST.CURRENCY>, values?: FormOnyxValues<typeof ONYXKEYS.FORMS.CHANGE_BILLING_CURRENCY_FORM>) => {
const changeBillingCurrency = useCallback((currency?: ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>, values?: FormOnyxValues<typeof ONYXKEYS.FORMS.CHANGE_BILLING_CURRENCY_FORM>) => {
if (values?.securityCode) {
PaymentMethods.updateBillingCurrency(currency ?? CONST.CURRENCY.USD, values.securityCode);
PaymentMethods.updateBillingCurrency(currency ?? CONST.PAYMENT_CARD_CURRENCY.USD, values.securityCode);
}
Navigation.goBack();
}, []);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/settings/Subscription/PaymentCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function AddPaymentCard() {
cardCVV: values.securityCode,
addressName: values.nameOnCard,
addressZip: values.addressZipCode,
currency: values.currency ?? CONST.CURRENCY.USD,
currency: values.currency ?? CONST.PAYMENT_CARD_CURRENCY.USD,
};
PaymentMethods.addSubscriptionPaymentCard(cardData);
}, []);
Expand Down
11 changes: 6 additions & 5 deletions src/pages/settings/Subscription/SubscriptionSettings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import OfflineWithFeedback from '@components/OfflineWithFeedback';
import Section from '@components/Section';
import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import useSubscriptionPlan from '@hooks/useSubscriptionPlan';
import usePreferredCurrency from '@hooks/usePreferredCurrency';
import useSubscriptionPossibleCostSavings from '@hooks/useSubscriptionPossibleCostSavings';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import {convertToShortDisplayString} from '@libs/CurrencyUtils';
import Navigation from '@navigation/Navigation';
import {formatSubscriptionEndDate} from '@pages/settings/Subscription/utils';
import ToggleSettingOptionRow from '@pages/workspace/workflows/ToggleSettingsOptionRow';
Expand All @@ -21,10 +23,9 @@ function SubscriptionSettings() {
const {translate} = useLocalize();
const styles = useThemeStyles();
const theme = useTheme();
const subscriptionPlan = useSubscriptionPlan();
const [privateSubscription] = useOnyx(ONYXKEYS.NVP_PRIVATE_SUBSCRIPTION);

const isCollect = subscriptionPlan === CONST.POLICY.TYPE.TEAM;
const preferredCurrency = usePreferredCurrency();
const possibleCostSavings = useSubscriptionPossibleCostSavings();

const autoRenewalDate = formatSubscriptionEndDate(privateSubscription?.endDate);

Expand All @@ -50,7 +51,7 @@ function SubscriptionSettings() {
<Text style={[styles.mr1, styles.textNormalThemeText]}>{translate('subscription.subscriptionSettings.autoIncrease')}</Text>
<Text style={customTitleSecondSentenceStyles}>
{translate('subscription.subscriptionSettings.saveUpTo', {
amountSaved: isCollect ? CONST.SUBSCRIPTION_POSSIBLE_COST_SAVINGS.COLLECT_PLAN : CONST.SUBSCRIPTION_POSSIBLE_COST_SAVINGS.CONTROL_PLAN,
amountWithCurrency: convertToShortDisplayString(possibleCostSavings, preferredCurrency),
})}
</Text>
</View>
Expand Down
2 changes: 1 addition & 1 deletion src/types/form/AddPaymentCardForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type AddPaymentCardForm = Form<
[INPUT_IDS.ADDRESS_ZIP_CODE]: string;
[INPUT_IDS.ADDRESS_STATE]: string;
[INPUT_IDS.ACCEPT_TERMS]: string;
[INPUT_IDS.CURRENCY]: ValueOf<typeof CONST.CURRENCY>;
[INPUT_IDS.CURRENCY]: ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>;
}
>;

Expand Down
2 changes: 1 addition & 1 deletion src/types/form/ChangeBillingCurrencyForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type InputID = ValueOf<typeof INPUT_IDS>;
type ChangeBillingCurrencyForm = Form<
InputID,
{
[INPUT_IDS.CURRENCY]: ValueOf<typeof CONST.CURRENCY>;
[INPUT_IDS.CURRENCY]: ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>;
[INPUT_IDS.SECURITY_CODE]: string;
}
>;
Expand Down

0 comments on commit e9f73c9

Please sign in to comment.