From 03f4a3e555ed04f748ad34f85086031752f70ff4 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Mon, 17 Jul 2023 17:37:08 +0700 Subject: [PATCH 1/5] update optimistic data for parent report action when completing or reopening task --- src/libs/actions/Task.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 818e653445d6..568448a14cb5 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -244,6 +244,25 @@ function completeTask(taskReportID, taskTitle) { }, ]; + // Optimistically update the parent report action + const report = ReportUtils.getReport(taskReportID); + 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, + completedTaskReportAction.created, + CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + ), + }, + }); + } + } + API.write( 'CompleteTask', { @@ -314,6 +333,25 @@ function reopenTask(taskReportID, taskTitle) { }, ]; + // Optimistically update the parent report action + const report = ReportUtils.getReport(taskReportID); + 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, + reopenedTaskReportAction.created, + CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + ), + }, + }); + } + } + API.write( 'ReopenTask', { From d73809d739d42666531657f058cb00888e29ee8d Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Wed, 19 Jul 2023 20:21:09 +0700 Subject: [PATCH 2/5] create common to get optimistic data for parent report action --- src/libs/ReportUtils.js | 25 +++++++++++++++++++++++ src/libs/actions/Report.js | 37 ++++++--------------------------- src/libs/actions/Task.js | 42 ++++++-------------------------------- 3 files changed, 37 insertions(+), 67 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index eee3d7558666..9db7ae07b1a9 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..d6b5bc098daf 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -321,20 +321,9 @@ 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 if (DateUtils.canUpdateTimezone()) { @@ -959,23 +948,9 @@ 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 = { reportID: originalReportID, diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 9a97cd120c94..2d37e3cbd617 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -242,24 +242,9 @@ function completeTask(taskReportID, taskTitle) { }, ]; - // Optimistically update the parent report action - const report = ReportUtils.getReport(taskReportID); - 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, - completedTaskReportAction.created, - CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, - ), - }, - }); - } - } + // 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', @@ -331,24 +316,9 @@ function reopenTask(taskReportID, taskTitle) { }, ]; - // Optimistically update the parent report action - const report = ReportUtils.getReport(taskReportID); - 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, - reopenedTaskReportAction.created, - CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, - ), - }, - }); - } - } + // 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', From 174e335b83bcc41700aa9615c07803362227dac5 Mon Sep 17 00:00:00 2001 From: dukenv0307 <129500732+dukenv0307@users.noreply.github.com> Date: Wed, 19 Jul 2023 20:36:09 +0700 Subject: [PATCH 3/5] Update src/libs/ReportUtils.js Co-authored-by: Aimane Chnaif <96077027+aimane-chnaif@users.noreply.github.com> --- src/libs/ReportUtils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 9db7ae07b1a9..2b7612d9aaba 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1390,7 +1390,7 @@ function updateOptimisticParentReportAction(parentReportAction, lastVisibleActio } /** - * get optimistic data of parent report action + * 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 From bb613da1378d153ccfd4b053eed011c36dc40960 Mon Sep 17 00:00:00 2001 From: dukenv0307 <129500732+dukenv0307@users.noreply.github.com> Date: Wed, 19 Jul 2023 23:01:47 +0700 Subject: [PATCH 4/5] Update src/libs/actions/Report.js Co-authored-by: Maria D'Costa --- src/libs/actions/Report.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index d6b5bc098daf..871c81ac481c 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -323,7 +323,9 @@ function addActions(reportID, text = '', file) { // 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); + if (!_.isEmpty(optimisticParentReportData)) { + optimisticData.push(optimisticParentReportData); + } // Update the timezone if it's been 5 minutes from the last time the user added a comment if (DateUtils.canUpdateTimezone()) { From bcd496ac38c876b8bfc2f1918d742667a3a2f698 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Wed, 19 Jul 2023 23:07:28 +0700 Subject: [PATCH 5/5] format if statements --- src/libs/actions/Report.js | 6 ++++-- src/libs/actions/Task.js | 8 ++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 871c81ac481c..fa6f249ba7d0 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -324,7 +324,7 @@ function addActions(reportID, text = '', file) { // 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); + optimisticData.push(optimisticParentReportData); } // Update the timezone if it's been 5 minutes from the last time the user added a comment @@ -952,7 +952,9 @@ function deleteReportComment(reportID, reportAction) { // 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); + if (!_.isEmpty(optimisticParentReportData)) { + optimisticData.push(optimisticParentReportData); + } const parameters = { reportID: originalReportID, diff --git a/src/libs/actions/Task.js b/src/libs/actions/Task.js index 2d37e3cbd617..8126d4b12dad 100644 --- a/src/libs/actions/Task.js +++ b/src/libs/actions/Task.js @@ -244,7 +244,9 @@ 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); + if (!_.isEmpty(optimisticParentReportData)) { + optimisticData.push(optimisticParentReportData); + } API.write( 'CompleteTask', @@ -318,7 +320,9 @@ 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); + if (!_.isEmpty(optimisticParentReportData)) { + optimisticData.push(optimisticParentReportData); + } API.write( 'ReopenTask',