From 50fd370576100cf63741e1e2c68a7eb519a80332 Mon Sep 17 00:00:00 2001 From: Cristi Paval Date: Tue, 11 Jul 2023 19:36:12 +0300 Subject: [PATCH 1/2] Reusable function instead of repetitive statements --- src/libs/ReportUtils.js | 17 +++++++++++++++++ src/libs/actions/IOU.js | 16 +++------------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index a4b2e4796859..96bbb830cac1 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1585,6 +1585,22 @@ function buildOptimisticReportPreview(chatReport, iouReport) { }; } +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, @@ -2544,6 +2560,7 @@ export { buildOptimisticExpenseReport, buildOptimisticIOUReportAction, buildOptimisticReportPreview, + updateReportPreview, buildOptimisticTaskReportAction, buildOptimisticAddCommentReportAction, buildOptimisticTaskCommentReportAction, diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index fb5dc33755ec..e359fda973ad 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 = [ { From bfdf2410259d306148aac895831fb1aa7e171cf8 Mon Sep 17 00:00:00 2001 From: Cristi Paval Date: Tue, 11 Jul 2023 20:08:18 +0300 Subject: [PATCH 2/2] Add JSDocs --- src/libs/ReportUtils.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 96bbb830cac1..ac7449864f9f 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1561,6 +1561,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 { @@ -1585,6 +1593,14 @@ 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 {