diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 93b9af771a32..d258b5419103 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -1183,6 +1183,21 @@ function updateMoneyRequestTag(transactionID, transactionThreadReportID, tag) { API.write('UpdateMoneyRequestTag', params, onyxData); } +/** + * Updates the category of a money request + * + * @param {String} transactionID + * @param {Number} transactionThreadReportID + * @param {String} category + */ +function updateMoneyRequestCategory(transactionID, transactionThreadReportID, category) { + const transactionChanges = { + category, + }; + const {params, onyxData} = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, true); + API.write('UpdateMoneyRequestCategory', params, onyxData); +} + /** * Updates the description of a money request * @@ -3719,6 +3734,7 @@ export { updateMoneyRequestBillable, updateMoneyRequestMerchant, updateMoneyRequestTag, + updateMoneyRequestCategory, updateMoneyRequestAmountAndCurrency, updateMoneyRequestDescription, replaceReceipt, diff --git a/src/pages/EditRequestPage.js b/src/pages/EditRequestPage.js index 606d3da1ddb9..3eb9d88f1120 100644 --- a/src/pages/EditRequestPage.js +++ b/src/pages/EditRequestPage.js @@ -110,12 +110,6 @@ function EditRequestPage({report, route, policyCategories, policyTags, parentRep }); }, [parentReportAction, fieldToEdit]); - // Update the transaction object and close the modal - function editMoneyRequest(transactionChanges) { - IOU.editMoneyRequest(transaction, report.reportID, transactionChanges); - Navigation.dismissModal(report.reportID); - } - const saveAmountAndCurrency = useCallback( ({amount, currency: newCurrency}) => { const newAmount = CurrencyUtils.convertToBackendAmount(Number.parseFloat(amount)); @@ -176,6 +170,16 @@ function EditRequestPage({report, route, policyCategories, policyTags, parentRep [transactionTag, transaction.transactionID, report.reportID], ); + const saveCategory = useCallback( + ({category: newCategory}) => { + // In case the same category has been selected, reset the category. + const updatedCategory = newCategory === transactionCategory ? '' : newCategory; + IOU.updateMoneyRequestCategory(transaction.transactionID, report.reportID, updatedCategory); + Navigation.dismissModal(); + }, + [transactionCategory, transaction.transactionID, report.reportID], + ); + const saveComment = useCallback( ({comment: newComment}) => { // Only update comment if it has changed @@ -235,14 +239,7 @@ function EditRequestPage({report, route, policyCategories, policyTags, parentRep { - let updatedCategory = transactionChanges.category; - // In case the same category has been selected, do reset of the category. - if (transactionCategory === updatedCategory) { - updatedCategory = ''; - } - editMoneyRequest({category: updatedCategory}); - }} + onSubmit={saveCategory} /> ); }