diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index a9d04654f8bd..edee278b0eff 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -837,7 +837,7 @@ function createDistanceRequest(report, participant, comment, created, category, /** * @param {String} transactionID - * @param {Number} transactionThreadReportID + * @param {String} transactionThreadReportID * @param {Object} transactionChanges * @param {String} [transactionChanges.created] Present when updated the date field * @param {Boolean} onlyIncludeChangedFields @@ -1017,7 +1017,7 @@ function getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, t * Updates the created date of a money request * * @param {String} transactionID - * @param {Number} transactionThreadReportID + * @param {String} transactionThreadReportID * @param {String} val */ function updateMoneyRequestDate(transactionID, transactionThreadReportID, val) { @@ -1032,7 +1032,7 @@ function updateMoneyRequestDate(transactionID, transactionThreadReportID, val) { * Edits an existing distance request * * @param {String} transactionID - * @param {Number} transactionThreadReportID + * @param {String} transactionThreadReportID * @param {Object} transactionChanges * @param {String} [transactionChanges.created] * @param {Number} [transactionChanges.amount] @@ -2242,7 +2242,7 @@ function editRegularMoneyRequest(transactionID, transactionThreadReportID, trans /** * @param {object} transaction - * @param {Number} transactionThreadReportID + * @param {String} transactionThreadReportID * @param {Object} transactionChanges */ function editMoneyRequest(transaction, transactionThreadReportID, transactionChanges) { @@ -2253,6 +2253,23 @@ function editMoneyRequest(transaction, transactionThreadReportID, transactionCha } } +/** + * Updates the amount and currency fields of a money request + * + * @param {String} transactionID + * @param {String} transactionThreadReportID + * @param {String} currency + * @param {Number} amount + */ +function updateMoneyRequestAmountAndCurrency(transactionID, transactionThreadReportID, currency, amount) { + const transactionChanges = { + amount, + currency, + }; + const {params, onyxData} = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, true); + API.write('UpdateMoneyRequestAmountAndCurrency', params, onyxData); +} + /** * @param {String} transactionID * @param {Object} reportAction - the money request reportAction we are deleting @@ -3505,6 +3522,7 @@ export { setUpDistanceTransaction, navigateToNextPage, updateMoneyRequestDate, + updateMoneyRequestAmountAndCurrency, replaceReceipt, detachReceipt, getIOUReportID, diff --git a/src/pages/EditRequestPage.js b/src/pages/EditRequestPage.js index 2bdf3d19f16a..4a0dce41265c 100644 --- a/src/pages/EditRequestPage.js +++ b/src/pages/EditRequestPage.js @@ -120,6 +120,22 @@ function EditRequestPage({report, route, parentReport, policyCategories, policyT Navigation.dismissModal(report.reportID); } + const saveAmountAndCurrency = useCallback( + ({amount, currency: newCurrency}) => { + const newAmount = CurrencyUtils.convertToBackendAmount(Number.parseFloat(amount)); + + // If the value hasn't changed, don't request to save changes on the server and just close the modal + if (newAmount === TransactionUtils.getAmount(transaction) && newCurrency === TransactionUtils.getCurrency(transaction)) { + Navigation.dismissModal(); + return; + } + + IOU.updateMoneyRequestAmountAndCurrency(transaction.transactionID, report.reportID, newCurrency, newAmount); + Navigation.dismissModal(); + }, + [transaction, report], + ); + const saveCreated = useCallback( ({created: newCreated}) => { // If the value hasn't changed, don't request to save changes on the server and just close the modal @@ -164,19 +180,7 @@ function EditRequestPage({report, route, parentReport, policyCategories, policyT defaultAmount={transactionAmount} defaultCurrency={defaultCurrency} reportID={report.reportID} - onSubmit={(transactionChanges) => { - const amount = CurrencyUtils.convertToBackendAmount(Number.parseFloat(transactionChanges)); - // In case the amount hasn't been changed, do not make the API request. - if (amount === transactionAmount && transactionCurrency === defaultCurrency) { - Navigation.dismissModal(); - return; - } - // Temporarily disabling currency editing and it will be enabled as a quick follow up - editMoneyRequest({ - amount, - currency: defaultCurrency, - }); - }} + onSubmit={saveAmountAndCurrency} onNavigateToCurrency={() => { const activeRoute = encodeURIComponent(Navigation.getActiveRouteWithoutParams()); Navigation.navigate(ROUTES.EDIT_CURRENCY_REQUEST.getRoute(report.reportID, defaultCurrency, activeRoute)); diff --git a/src/pages/iou/request/step/IOURequestStepAmount.js b/src/pages/iou/request/step/IOURequestStepAmount.js index 8f89703e6efd..b0812271c647 100644 --- a/src/pages/iou/request/step/IOURequestStepAmount.js +++ b/src/pages/iou/request/step/IOURequestStepAmount.js @@ -68,10 +68,10 @@ function IOURequestStepAmount({ }; /** - * @param {Number} currentAmount + * @param {Number} amount */ - const navigateToNextPage = (currentAmount) => { - const amountInSmallestCurrencyUnits = CurrencyUtils.convertToBackendAmount(Number.parseFloat(currentAmount)); + const navigateToNextPage = ({amount}) => { + const amountInSmallestCurrencyUnits = CurrencyUtils.convertToBackendAmount(Number.parseFloat(amount)); IOU.setMoneyRequestAmount_temporaryForRefactor(transactionID, amountInSmallestCurrencyUnits, currency || CONST.CURRENCY.USD); if (backTo) { diff --git a/src/pages/iou/steps/MoneyRequestAmountForm.js b/src/pages/iou/steps/MoneyRequestAmountForm.js index 652e07674ae0..2fe033cacca3 100644 --- a/src/pages/iou/steps/MoneyRequestAmountForm.js +++ b/src/pages/iou/steps/MoneyRequestAmountForm.js @@ -228,8 +228,8 @@ function MoneyRequestAmountForm({amount, currency, isEditing, forwardedRef, onCu const backendAmount = CurrencyUtils.convertToBackendAmount(Number.parseFloat(currentAmount)); initializeAmount(backendAmount); - onSubmitButtonPress(currentAmount); - }, [onSubmitButtonPress, currentAmount, initializeAmount]); + onSubmitButtonPress({amount: currentAmount, currency}); + }, [onSubmitButtonPress, currentAmount, currency, initializeAmount]); /** * Input handler to check for a forward-delete key (or keyboard shortcut) press. diff --git a/src/pages/iou/steps/NewRequestAmountPage.js b/src/pages/iou/steps/NewRequestAmountPage.js index 569e2ce693ee..1df74569e4c3 100644 --- a/src/pages/iou/steps/NewRequestAmountPage.js +++ b/src/pages/iou/steps/NewRequestAmountPage.js @@ -128,8 +128,8 @@ function NewRequestAmountPage({route, iou, report, selectedTab}) { Navigation.navigate(ROUTES.MONEY_REQUEST_CURRENCY.getRoute(iouType, reportID, currency, activeRoute)); }; - const navigateToNextPage = (currentAmount) => { - const amountInSmallestCurrencyUnits = CurrencyUtils.convertToBackendAmount(Number.parseFloat(currentAmount)); + const navigateToNextPage = ({amount}) => { + const amountInSmallestCurrencyUnits = CurrencyUtils.convertToBackendAmount(Number.parseFloat(amount)); IOU.setMoneyRequestAmount(amountInSmallestCurrencyUnits); IOU.setMoneyRequestCurrency(currency);