Skip to content

Commit ba6c19a

Browse files
authored
Merge pull request #35327 from paultsimura/fix/34853-settlement-button
feat: Persist the latest selected payment option
2 parents dc3c396 + 3d850af commit ba6c19a

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

src/components/ButtonWithDropdownMenu.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ type ButtonWithDropdownMenuProps = {
3232
/** Callback to execute when the main button is pressed */
3333
onPress: (event: GestureResponderEvent | KeyboardEvent | undefined, value: string) => void;
3434

35+
/** Callback to execute when a dropdown option is selected */
36+
onOptionSelected?: (option: DropdownOption) => void;
37+
3538
/** Call the onPress function on main button when Enter key is pressed */
3639
pressOnEnter?: boolean;
3740

@@ -72,6 +75,7 @@ function ButtonWithDropdownMenu({
7275
buttonRef,
7376
onPress,
7477
options,
78+
onOptionSelected,
7579
}: ButtonWithDropdownMenuProps) {
7680
const theme = useTheme();
7781
const styles = useThemeStyles();
@@ -174,6 +178,7 @@ function ButtonWithDropdownMenu({
174178
menuItems={options.map((item, index) => ({
175179
...item,
176180
onSelected: () => {
181+
onOptionSelected?.(item);
177182
setSelectedItemIndex(index);
178183
},
179184
}))}

src/components/SettlementButton.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,9 @@ function SettlementButton({
177177
return [approveButtonOption];
178178
}
179179

180-
// To achieve the one tap pay experience we need to choose the correct payment type as default,
181-
// if user already paid for some request or expense, let's use the last payment method or use default.
180+
// To achieve the one tap pay experience we need to choose the correct payment type as default.
181+
// If the user has previously chosen a specific payment option or paid for some request or expense,
182+
// let's use the last payment method or use default.
182183
const paymentMethod = nvp_lastPaymentMethod[policyID] || '';
183184
if (canUseWallet) {
184185
buttonOptions.push(paymentMethods[CONST.IOU.PAYMENT_TYPE.EXPENSIFY]);
@@ -192,12 +193,14 @@ function SettlementButton({
192193
buttonOptions.push(approveButtonOption);
193194
}
194195

195-
// Put the preferred payment method to the front of the array so its shown as default
196+
// Put the preferred payment method to the front of the array, so it's shown as default
196197
if (paymentMethod) {
197198
return _.sortBy(buttonOptions, (method) => (method.value === paymentMethod ? 0 : 1));
198199
}
199200
return buttonOptions;
200-
}, [currency, formattedAmount, iouReport, nvp_lastPaymentMethod, policyID, translate, shouldHidePaymentOptions, shouldShowApproveButton]);
201+
// We don't want to reorder the options when the preferred payment method changes while the button is still visible
202+
// eslint-disable-next-line react-hooks/exhaustive-deps
203+
}, [currency, formattedAmount, iouReport, policyID, translate, shouldHidePaymentOptions, shouldShowApproveButton]);
201204

202205
const selectPaymentType = (event, iouPaymentType, triggerKYCFlow) => {
203206
if (iouPaymentType === CONST.IOU.PAYMENT_TYPE.EXPENSIFY || iouPaymentType === CONST.IOU.PAYMENT_TYPE.VBBA) {
@@ -235,6 +238,7 @@ function SettlementButton({
235238
onPress={(event, iouPaymentType) => selectPaymentType(event, iouPaymentType, triggerKYCFlow)}
236239
pressOnEnter={pressOnEnter}
237240
options={paymentButtonOptions}
241+
onOptionSelected={(option) => IOU.savePreferredPaymentMethod(policyID, option.value)}
238242
style={style}
239243
buttonSize={buttonSize}
240244
anchorAlignment={paymentMethodDropdownAnchorAlignment}

src/libs/actions/IOU.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3775,6 +3775,15 @@ function navigateToStartStepIfScanFileCannotBeRead(receiptFilename, receiptPath,
37753775
FileUtils.readFileAsync(receiptPath, receiptFilename, onSuccess, onFailure);
37763776
}
37773777

3778+
/**
3779+
* Save the preferred payment method for a policy
3780+
* @param {String} policyID
3781+
* @param {String} paymentMethod
3782+
*/
3783+
function savePreferredPaymentMethod(policyID, paymentMethod) {
3784+
Onyx.merge(`${ONYXKEYS.NVP_LAST_PAYMENT_METHOD}`, {[policyID]: paymentMethod});
3785+
}
3786+
37783787
export {
37793788
setMoneyRequestParticipants,
37803789
createDistanceRequest,
@@ -3835,4 +3844,5 @@ export {
38353844
getIOUReportID,
38363845
editMoneyRequest,
38373846
navigateToStartStepIfScanFileCannotBeRead,
3847+
savePreferredPaymentMethod,
38383848
};

0 commit comments

Comments
 (0)