Skip to content

Commit

Permalink
simplify the code and update useEffect deps
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardoj committed Sep 13, 2024
1 parent 327da6f commit eb00272
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions src/components/SettlementButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import * as IOU from '@userActions/IOU';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {LastPaymentMethod} from '@src/types/onyx';
import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import type SettlementButtonProps from './types';
Expand Down Expand Up @@ -47,7 +46,7 @@ function SettlementButton({
formattedAmount = '',
onPress,
pressOnEnter = false,
policyID = '',
policyID = '-1',
shouldHidePaymentOptions = false,
shouldShowApproveButton = false,
shouldDisableApproveButton = false,
Expand All @@ -65,9 +64,7 @@ function SettlementButton({
const session = useSession();
// The app would crash due to subscribing to the entire report collection if chatReportID is an empty string. So we should have a fallback ID here.
const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${chatReportID || -1}`);
// The "nvpLastPaymentMethod" object needs to be stable to prevent the "useMemo"
// hook from being recreated unnecessarily, hence the use of CONST.EMPTY_OBJECT
const [nvpLastPaymentMethod = CONST.EMPTY_OBJECT as LastPaymentMethod] = useOnyx(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, {selector: (paymentMethod) => paymentMethod ?? {}});
const [lastPaymentMethod = '-1'] = useOnyx(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, {selector: (paymentMethod) => paymentMethod?.[policyID]});
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`);
const isInvoiceReport = (!isEmptyObject(iouReport) && ReportUtils.isInvoiceReport(iouReport)) || false;
const isPaidGroupPolicy = ReportUtils.isPaidGroupPolicyExpenseChat(chatReport);
Expand Down Expand Up @@ -109,9 +106,6 @@ function SettlementButton({
}

// To achieve the one tap pay experience we need to choose the correct payment type as default.
// If the user has previously chosen a specific payment option or paid for some expense,
// let's use the last payment method or use default.
const paymentMethod = nvpLastPaymentMethod?.[policyID] ?? '-1';
if (canUseWallet) {
buttonOptions.push(paymentMethods[CONST.IOU.PAYMENT_TYPE.EXPENSIFY]);
}
Expand Down Expand Up @@ -160,14 +154,27 @@ function SettlementButton({
buttonOptions.push(approveButtonOption);
}

// Put the preferred payment method to the front of the array, so it's shown as default
if (paymentMethod) {
return buttonOptions.sort((method) => (method.value === paymentMethod ? -1 : 0));
// Put the preferred payment method to the front of the array, so it's shown as default. We assume their last payment method is their preferred.
if (lastPaymentMethod) {
return buttonOptions.sort((method) => (method.value === lastPaymentMethod ? -1 : 0));
}
return buttonOptions;
// We don't want to reorder the options when the preferred payment method changes while the button is still visible
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, [currency, formattedAmount, iouReport, chatReport, policyID, translate, shouldHidePaymentOptions, shouldShowApproveButton, shouldDisableApproveButton]);
}, [
iouReport,
translate,
formattedAmount,
shouldDisableApproveButton,
isInvoiceReport,
currency,
shouldHidePaymentOptions,
shouldShowApproveButton,
shouldShowPaywithExpensifyOption,
shouldShowPayElsewhereOption,
chatReport,
onPress,
]);

const selectPaymentType = (event: KYCFlowEvent, iouPaymentType: PaymentMethodType, triggerKYCFlow: TriggerKYCFlow) => {
if (policy && SubscriptionUtils.shouldRestrictUserBillableActions(policy.id)) {
Expand Down Expand Up @@ -226,7 +233,12 @@ function SettlementButton({
onPress={(event, iouPaymentType) => selectPaymentType(event, iouPaymentType, triggerKYCFlow)}
pressOnEnter={pressOnEnter}
options={paymentButtonOptions}
onOptionSelected={(option) => savePreferredPaymentMethod(policyID, option.value)}
onOptionSelected={(option) => {
if (policyID === '-1') {
return;
}
savePreferredPaymentMethod(policyID, option.value);
}}
style={style}
disabledStyle={disabledStyle}
buttonSize={buttonSize}
Expand Down

0 comments on commit eb00272

Please sign in to comment.