diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 54c69c630714..82d3f3217dff 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1339,6 +1339,49 @@ function buildOptimisticAddCommentReportAction(text, file) { }; } +/** + * update optimistic parent reportAction when a comment is added or remove in the child report + * @param {String} parentReportAction - Parent report action of the child report + * @param {String} lastVisibleActionCreated - Last visible action created of the child report + * @param {String} type - The type of action in the child report + * @returns {Object} + */ + +function updateOptimisticParentReportAction(parentReportAction, lastVisibleActionCreated, type) { + let childVisibleActionCount = parentReportAction.childVisibleActionCount || 0; + let childCommenterCount = parentReportAction.childCommenterCount || 0; + let childOldestFourAccountIDs = parentReportAction.childOldestFourAccountIDs; + + if (type === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) { + childVisibleActionCount += 1; + const oldestFourAccountIDs = childOldestFourAccountIDs ? childOldestFourAccountIDs.split(',') : []; + if (oldestFourAccountIDs.length < 4) { + const index = _.findIndex(oldestFourAccountIDs, (accountID) => accountID === currentUserAccountID.toString()); + if (index === -1) { + childCommenterCount += 1; + oldestFourAccountIDs.push(currentUserAccountID); + } + } + childOldestFourAccountIDs = oldestFourAccountIDs.join(','); + } else if (type === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) { + if (childVisibleActionCount > 0) { + childVisibleActionCount -= 1; + } + + if (childVisibleActionCount === 0) { + childCommenterCount = 0; + childOldestFourAccountIDs = ''; + } + } + + return { + childVisibleActionCount, + childCommenterCount, + childLastVisibleActionCreated: lastVisibleActionCreated, + childOldestFourAccountIDs, + }; +} + /** * Builds an optimistic reportAction for the parent report when a task is created * @param {String} taskReportID - Report ID of the task @@ -2549,6 +2592,7 @@ export { buildOptimisticTaskReportAction, buildOptimisticAddCommentReportAction, buildOptimisticTaskCommentReportAction, + updateOptimisticParentReportAction, shouldReportBeInOptionList, getChatByParticipants, getChatByParticipantsByLoginList, diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 8c072d8c7bc8..4d4c9290d87c 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -320,6 +320,21 @@ function addActions(reportID, text = '', file) { }, ]; + // Optimistically update the parent report action if the report is a thread + const report = ReportUtils.getReport(reportID); + if (report && report.parentReportActionID) { + const parentReportAction = ReportActionsUtils.getParentReportAction(report); + if (parentReportAction && parentReportAction.reportActionID) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`, + value: { + [parentReportAction.reportActionID]: ReportUtils.updateOptimisticParentReportAction(parentReportAction, currentTime, CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD), + }, + }); + } + } + // Update the timezone if it's been 5 minutes from the last time the user added a comment if (DateUtils.canUpdateTimezone()) { const timezone = DateUtils.getCurrentTimezone(); @@ -926,6 +941,24 @@ function deleteReportComment(reportID, reportAction) { }, ]; + const report = ReportUtils.getReport(reportID); + if (report && report.parentReportActionID) { + const parentReportAction = ReportActionsUtils.getParentReportAction(report); + if (parentReportAction && parentReportAction.reportActionID) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`, + value: { + [parentReportAction.reportActionID]: ReportUtils.updateOptimisticParentReportAction( + parentReportAction, + optimisticReport.lastVisibleActionCreated, + CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, + ), + }, + }); + } + } + const parameters = { reportID: originalReportID, reportActionID,