diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index eee3d7558666..2b7612d9aaba 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1389,6 +1389,30 @@ function updateOptimisticParentReportAction(parentReportAction, lastVisibleActio }; } +/** + * Get optimistic data of parent report action + * @param {String} reportID The reportID of the report that is updated + * @param {String} lastVisibleActionCreated Last visible action created of the child report + * @param {String} type The type of action in the child report + * @returns {Object} + */ +const getOptimisticDataForParentReportAction = (reportID, lastVisibleActionCreated, type) => { + const report = getReport(reportID); + if (report && report.parentReportActionID) { + const parentReportAction = ReportActionsUtils.getParentReportAction(report); + if (parentReportAction && parentReportAction.reportActionID) { + return { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`, + value: { + [parentReportAction.reportActionID]: updateOptimisticParentReportAction(parentReportAction, lastVisibleActionCreated, type), + }, + }; + } + } + return {}; +}; + /** * Builds an optimistic reportAction for the parent report when a task is created * @param {String} taskReportID - Report ID of the task @@ -2591,6 +2615,7 @@ export { buildOptimisticAddCommentReportAction, buildOptimisticTaskCommentReportAction, updateOptimisticParentReportAction, + getOptimisticDataForParentReportAction, shouldReportBeInOptionList, getChatByParticipants, getChatByParticipantsByLoginList, diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 3c2a7be271cd..fa6f249ba7d0 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -321,19 +321,10 @@ 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 optimistic data for parent report action if the report is a child report + const optimisticParentReportData = ReportUtils.getOptimisticDataForParentReportAction(reportID, currentTime, CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD); + if (!_.isEmpty(optimisticParentReportData)) { + optimisticData.push(optimisticParentReportData); } // Update the timezone if it's been 5 minutes from the last time the user added a comment @@ -959,22 +950,10 @@ 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, - ), - }, - }); - } + // Update optimistic data for parent report action if the report is a child report + const optimisticParentReportData = ReportUtils.getOptimisticDataForParentReportAction(reportID, optimisticReport.lastVisibleActionCreated, CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE); + if (!_.isEmpty(optimisticParentReportData)) { + optimisticData.push(optimisticParentReportData); } const parameters = { diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index ee33161558f6..8126d4b12dad 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -242,6 +242,12 @@ function completeTask(taskReportID, taskTitle) { }, ]; + // Update optimistic data for parent report action + const optimisticParentReportData = ReportUtils.getOptimisticDataForParentReportAction(taskReportID, completedTaskReportAction.created, CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD); + if (!_.isEmpty(optimisticParentReportData)) { + optimisticData.push(optimisticParentReportData); + } + API.write( 'CompleteTask', { @@ -312,6 +318,12 @@ function reopenTask(taskReportID, taskTitle) { }, ]; + // Update optimistic data for parent report action + const optimisticParentReportData = ReportUtils.getOptimisticDataForParentReportAction(taskReportID, reopenedTaskReportAction.created, CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD); + if (!_.isEmpty(optimisticParentReportData)) { + optimisticData.push(optimisticParentReportData); + } + API.write( 'ReopenTask', {