-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Followup #23961: update IOU report total in optimistic data on edit money request #25498
Changes from 5 commits
f0ff0a7
72784d4
b7dc724
7c9706e
c7a2d06
4cbd6d5
db3cf8a
ea2926f
4399aa1
3e98419
e89644c
529180d
2590bdc
58346d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -949,12 +949,64 @@ function editMoneyRequest(transactionID, transactionThreadReportID, transactionC | |
const transactionThread = allReports[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`]; | ||
const transaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]; | ||
const iouReport = allReports[`${ONYXKEYS.COLLECTION.REPORT}${transactionThread.parentReportID}`]; | ||
const chatReport = allReports[`${ONYXKEYS.COLLECTION.REPORT}${iouReport.chatReportID}`]; | ||
const isFromExpenseReport = ReportUtils.isExpenseReport(iouReport); | ||
|
||
// STEP 2: Build new modified expense report action. | ||
const updatedReportAction = ReportUtils.buildOptimisticModifiedExpenseReportAction(transactionThread, transaction, transactionChanges, isFromExpenseReport); | ||
const updatedTransaction = TransactionUtils.getUpdatedTransaction(transaction, transactionChanges, isFromExpenseReport); | ||
|
||
// STEP 3: Compute the IOU total and update the report preview message so LHN amount owed is correct | ||
// Should only update if the transaction matches the currency of the report, else we wait for the update | ||
// from the server with the currency conversion | ||
let updatedIouReport = null; | ||
BeeMargarida marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const updatedChatReport = {...chatReport}; | ||
mountiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (updatedTransaction.currency === iouReport.currency && updatedTransaction.modifiedAmount) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @BeeMargarida @rezkiy37 run into an issue because the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I'll make a PR with that fix There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The regression fix PR: #27451 (comment) |
||
if (ReportUtils.isExpenseReport(iouReport)) { | ||
updatedIouReport = {...iouReport}; | ||
updatedIouReport.total -= TransactionUtils.getAmount(transaction, true); | ||
updatedIouReport.total += TransactionUtils.getAmount(updatedTransaction, true); | ||
} else { | ||
updatedIouReport = IOUUtils.updateIOUOwnerAndTotal( | ||
iouReport, | ||
updatedReportAction.actorAccountID, | ||
TransactionUtils.getAmount(transaction, false), | ||
TransactionUtils.getCurrency(transaction), | ||
true, | ||
); | ||
updatedIouReport = IOUUtils.updateIOUOwnerAndTotal( | ||
updatedIouReport, | ||
updatedReportAction.actorAccountID, | ||
TransactionUtils.getAmount(updatedTransaction, false), | ||
TransactionUtils.getCurrency(updatedTransaction), | ||
false, | ||
); | ||
BeeMargarida marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
updatedIouReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedIouReport.total, updatedTransaction.currency); | ||
|
||
// Update the last message of the IOU report | ||
const lastMessage = ReportUtils.getIOUReportActionMessage( | ||
iouReport.reportID, | ||
CONST.IOU.REPORT_ACTION_TYPE.CREATE, | ||
BeeMargarida marked this conversation as resolved.
Show resolved
Hide resolved
|
||
updatedIouReport.total, | ||
'', | ||
updatedTransaction.currency, | ||
'', | ||
false, | ||
); | ||
updatedIouReport.lastMessageText = lastMessage[0].text; | ||
updatedIouReport.lastMessageHtml = lastMessage[0].html; | ||
|
||
// Update the last message of the IOU chat report | ||
BeeMargarida marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const messageText = Localize.translateLocal('iou.payerOwesAmount', { | ||
payer: updatedIouReport.managerEmail, | ||
amount: CurrencyUtils.convertToDisplayString(updatedIouReport.total, updatedIouReport.currency), | ||
BeeMargarida marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
updatedChatReport.lastMessageText = messageText; | ||
updatedChatReport.lastMessageHtml = messageText; | ||
} | ||
|
||
// STEP 4: Compose the optimistic data | ||
const optimisticData = [ | ||
{ | ||
|
@@ -969,6 +1021,16 @@ function editMoneyRequest(transactionID, transactionThreadReportID, transactionC | |
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, | ||
value: updatedTransaction, | ||
}, | ||
{ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, | ||
value: updatedIouReport, | ||
}, | ||
{ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.chatReportID}`, | ||
value: updatedChatReport, | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have set |
||
]; | ||
|
||
const successData = [ | ||
|
@@ -991,6 +1053,11 @@ function editMoneyRequest(transactionID, transactionThreadReportID, transactionC | |
}, | ||
}, | ||
}, | ||
{ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, | ||
value: {pendingAction: null}, | ||
}, | ||
]; | ||
|
||
const failureData = [ | ||
|
@@ -1008,7 +1075,7 @@ function editMoneyRequest(transactionID, transactionThreadReportID, transactionC | |
}, | ||
{ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.report}`, | ||
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, | ||
value: iouReport, | ||
}, | ||
mountiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
]; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also just load this single transaction by composing multiple
withOnyx
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will cause a lot of re-renders since we're including all the transactions. Similar optimisation can also be done for the
parentReportAction
aboveThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean having several
withOnyx
that depend on one another? Won't those also be injected as props? Or we ignore thetransactions
prop and only use the one transaction?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is possible, because there's no way to access the
parentReportAction
by key. And thisparentReportAction
is necessary to access the transaction. So we can't do this inwithOnyx
.Or am I missing something? 🤔
The only thing that I think would work is make a
withReportActions
where we could pass transformValue that selected the report action taking into account the fullreport.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can use
selector
function from onyx. No?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so, because in the selector we don't have access to the props, so we couldn't use the
fullReport
to access the correct report actionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm...
Can't you add the following here:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yap, I think I can do that, but I can't make the same refactor to
parentReportActions
.Well, I can, but I need to make a
withReportActions
HOC (allows to use the props intransformValue
so we can only get the report action that we want). I've developed this and it seems to work fine. Do you think this optimization is worth it?Note: the optimization could also be applied to the policies prop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're the owner of this code, and you think its not worth it, then I'm totally fine with this as well!
@mountiny Do you agree?