Skip to content

Commit

Permalink
Merge pull request #32533 from Expensify/cmartins-createUpdateMoneyRe…
Browse files Browse the repository at this point in the history
…questAmount

Create updateMoneyRequestAmountOrCurrency
  • Loading branch information
luacmartins authored Dec 25, 2023
2 parents 7b836cf + 78dae42 commit 595bf40
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
26 changes: 22 additions & 4 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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]
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -3505,6 +3522,7 @@ export {
setUpDistanceTransaction,
navigateToNextPage,
updateMoneyRequestDate,
updateMoneyRequestAmountAndCurrency,
replaceReceipt,
detachReceipt,
getIOUReportID,
Expand Down
30 changes: 17 additions & 13 deletions src/pages/EditRequestPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down
6 changes: 3 additions & 3 deletions src/pages/iou/request/step/IOURequestStepAmount.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/pages/iou/steps/MoneyRequestAmountForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/pages/iou/steps/NewRequestAmountPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 595bf40

Please sign in to comment.