diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index e4a6b801db57..8201e5c225b3 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1752,6 +1752,14 @@ function buildOptimisticIOUReportAction(type, amount, currency, comment, partici }; } +/** + * Builds an optimistic report preview action with a randomly generated reportActionID. + * + * @param {Object} chatReport + * @param {Object} iouReport + * + * @returns {Object} + */ function buildOptimisticReportPreview(chatReport, iouReport) { const message = getReportPreviewMessage(iouReport); return { @@ -1776,6 +1784,30 @@ function buildOptimisticReportPreview(chatReport, iouReport) { }; } +/** + * Updates a report preview action that exists for an IOU report. + * + * @param {Object} iouReport + * @param {Object} reportPreviewAction + * + * @returns {Object} + */ +function updateReportPreview(iouReport, reportPreviewAction) { + const message = getReportPreviewMessage(iouReport, reportPreviewAction); + return { + ...reportPreviewAction, + created: DateUtils.getDBTime(), + message: [ + { + html: message, + text: message, + isEdited: false, + type: CONST.REPORT.MESSAGE.TYPE.COMMENT, + }, + ], + }; +} + function buildOptimisticTaskReportAction(taskReportID, actionName, message = '') { const originalMessage = { taskReportID, @@ -2788,6 +2820,7 @@ export { buildOptimisticExpenseReport, buildOptimisticIOUReportAction, buildOptimisticReportPreview, + updateReportPreview, buildOptimisticTaskReportAction, buildOptimisticAddCommentReportAction, buildOptimisticTaskCommentReportAction, diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index dbea53c54c3a..57287d3c5394 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -384,10 +384,7 @@ function requestMoney(report, amount, currency, payeeEmail, payeeAccountID, part let reportPreviewAction = isNewIOUReport ? null : ReportActionsUtils.getReportPreviewAction(chatReport.reportID, iouReport.reportID); if (reportPreviewAction) { - reportPreviewAction.created = DateUtils.getDBTime(); - const message = ReportUtils.getReportPreviewMessage(iouReport, reportPreviewAction); - reportPreviewAction.message[0].html = message; - reportPreviewAction.message[0].text = message; + reportPreviewAction = ReportUtils.updateReportPreview(iouReport, reportPreviewAction); } else { reportPreviewAction = ReportUtils.buildOptimisticReportPreview(chatReport, iouReport); } @@ -657,10 +654,7 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco let oneOnOneReportPreviewAction = ReportActionsUtils.getReportPreviewAction(oneOnOneChatReport.reportID, oneOnOneIOUReport.reportID); if (oneOnOneReportPreviewAction) { - oneOnOneReportPreviewAction.created = DateUtils.getDBTime(); - const message = ReportUtils.getReportPreviewMessage(oneOnOneIOUReport, oneOnOneReportPreviewAction); - oneOnOneReportPreviewAction.message[0].html = message; - oneOnOneReportPreviewAction.message[0].text = message; + oneOnOneReportPreviewAction = ReportUtils.updateReportPreview(oneOnOneIOUReport, oneOnOneReportPreviewAction); } else { oneOnOneReportPreviewAction = ReportUtils.buildOptimisticReportPreview(oneOnOneChatReport, oneOnOneIOUReport); } @@ -1141,11 +1135,7 @@ function getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentMetho login: recipient.login, }; - const optimisticReportPreviewAction = ReportActionsUtils.getReportPreviewAction(chatReport.reportID, iouReport.reportID); - optimisticReportPreviewAction.created = DateUtils.getDBTime(); - const message = ReportUtils.getReportPreviewMessage(iouReport, optimisticReportPreviewAction); - optimisticReportPreviewAction.message[0].html = message; - optimisticReportPreviewAction.message[0].text = message; + const optimisticReportPreviewAction = ReportUtils.updateReportPreview(iouReport, ReportActionsUtils.getReportPreviewAction(chatReport.reportID, iouReport.reportID)); const optimisticData = [ {