From 605b92b129d46e9268a1b9be95babb2ff6316bdb Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Thu, 15 Jun 2023 00:21:32 +0700 Subject: [PATCH 01/10] update optimistic data for parent report action when add comment in thread --- src/libs/actions/Report.js | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index cb4376593526..39b2c1016fb6 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -274,6 +274,45 @@ function addActions(reportID, text = '', file) { }, ]; + // Optimistically update the parent report action if the report is a thread + const report = ReportUtils.getReport(reportID); + if (ReportUtils.isThread(report)) { + const parentReportAction = ReportActionsUtils.getParentReportAction(report); + const childVisibleActionCount = parentReportAction.childVisibleActionCount ? parentReportAction.childVisibleActionCount + 1 : 1; + const childCommenterCount = parentReportAction.childCommenterCount ? parentReportAction.childCommenterCount : 1; + const childLastVisibleActionCreated = currentTime; + let childOldestFourEmails = parentReportAction.childOldestFourEmails; + if (!childOldestFourEmails) { + childOldestFourEmails = currentUserEmail; + } else { + const oldestFourEmails = childOldestFourEmails.split(','); + const index = _.findIndex(oldestFourEmails, (email) => email === currentUserEmail); + if (index !== -1) { + oldestFourEmails.splice(index, 1); + } + oldestFourEmails.push(currentUserEmail); + if (oldestFourEmails.length > 4) { + oldestFourEmails.splice(0, 1); + } + childOldestFourEmails = oldestFourEmails.join(','); + } + + const optimisticParentReportAction = { + childVisibleActionCount, + childCommenterCount, + childLastVisibleActionCreated, + childOldestFourEmails, + }; + + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`, + value: { + [parentReportAction.reportActionID]: optimisticParentReportAction, + }, + }); + } + // 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(); From 7a76df9fdd7b87d50b80fd4a1960743f4d49f6a6 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Tue, 20 Jun 2023 14:17:26 +0700 Subject: [PATCH 02/10] migrate child oldest four accountIDs --- src/libs/actions/Report.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 33c320f7f5b8..d9211cb6c213 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -296,27 +296,27 @@ function addActions(reportID, text = '', file) { const childVisibleActionCount = parentReportAction.childVisibleActionCount ? parentReportAction.childVisibleActionCount + 1 : 1; const childCommenterCount = parentReportAction.childCommenterCount ? parentReportAction.childCommenterCount : 1; const childLastVisibleActionCreated = currentTime; - let childOldestFourEmails = parentReportAction.childOldestFourEmails; - if (!childOldestFourEmails) { - childOldestFourEmails = currentUserEmail; + let childOldestFourAccountIDs = parentReportAction.childOldestFourAccountIDs; + if (!childOldestFourAccountIDs) { + childOldestFourAccountIDs = currentUserAccountID; } else { - const oldestFourEmails = childOldestFourEmails.split(','); - const index = _.findIndex(oldestFourEmails, (email) => email === currentUserEmail); + const oldestFourAccountIDs = childOldestFourAccountIDs.split(','); + const index = _.findIndex(oldestFourAccountIDs, (accountID) => accountID === currentUserAccountID); if (index !== -1) { - oldestFourEmails.splice(index, 1); + oldestFourAccountIDs.splice(index, 1); } - oldestFourEmails.push(currentUserEmail); - if (oldestFourEmails.length > 4) { - oldestFourEmails.splice(0, 1); + oldestFourAccountIDs.push(currentUserAccountID); + if (oldestFourAccountIDs.length > 4) { + oldestFourAccountIDs.splice(0, 1); } - childOldestFourEmails = oldestFourEmails.join(','); + childOldestFourAccountIDs = oldestFourAccountIDs.join(','); } const optimisticParentReportAction = { childVisibleActionCount, childCommenterCount, childLastVisibleActionCreated, - childOldestFourEmails, + childOldestFourAccountIDs, }; optimisticData.push({ From 65c5bf8554bd88892f48d893cb3e5ffce238741a Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Mon, 3 Jul 2023 11:13:32 +0700 Subject: [PATCH 03/10] update parent report action for deleting action --- src/libs/ReportUtils.js | 48 ++++++++++++++++++++++++++++++++++++++ src/libs/actions/Report.js | 44 ++++++++++++++-------------------- 2 files changed, 65 insertions(+), 27 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 4ddbf032867c..a752e40e5f9b 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1236,6 +1236,53 @@ function buildOptimisticAddCommentReportAction(text, file) { }; } +/** + * update optimistic reportAction for the parent report when a comment is added or remove in thread + * @param {String} parentReportAction - Parent report action of thread + * @param {String} lastVisibleActionCreated - Last visible action created of thread + * @param {String} type - The type of action in thread + * @returns {Object} + */ + +function updateOptimisticParentReportAction(parentReportAction, lastVisibleActionCreated, type) { + let childVisibleActionCount = parentReportAction.childVisibleActionCount ? parentReportAction.childVisibleActionCount : 1; + let childCommenterCount = parentReportAction.childCommenterCount ? parentReportAction.childCommenterCount : 1; + const childLastVisibleActionCreated = lastVisibleActionCreated; + let childOldestFourAccountIDs = parentReportAction.childOldestFourAccountIDs; + + if (type === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) { + childVisibleActionCount += 1; + if (!childOldestFourAccountIDs) { + childOldestFourAccountIDs = currentUserAccountID.toString(); + } else { + const oldestFourAccountIDs = childOldestFourAccountIDs.split(','); + const index = _.findIndex(oldestFourAccountIDs, (accountID) => accountID === currentUserAccountID.toString()); + if (index !== -1) { + oldestFourAccountIDs.splice(index, 1); + } + + oldestFourAccountIDs.push(currentUserAccountID); + if (oldestFourAccountIDs.length > 4) { + oldestFourAccountIDs.splice(0, 1); + } + childOldestFourAccountIDs = oldestFourAccountIDs.join(','); + } + } else { + childVisibleActionCount -= 1; + if (childVisibleActionCount === 0) { + childCommenterCount = 0; + childOldestFourAccountIDs = ''; + } + } + + return { + childVisibleActionCount, + childCommenterCount, + childLastVisibleActionCreated, + childOldestFourAccountIDs, + }; +} + /** * Builds an optimistic reportAction for the parent report when a task is created * @param {String} taskReportID - Report ID of the task @@ -2415,6 +2462,7 @@ export { buildOptimisticTaskReportAction, buildOptimisticAddCommentReportAction, buildOptimisticTaskCommentReportAction, + updateOptimisticParentReportAction, shouldReportBeInOptionList, getChatByParticipants, getChatByParticipantsByLoginList, diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 86530070b6e7..639ee1bcd86b 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -309,37 +309,11 @@ function addActions(reportID, text = '', file) { const report = ReportUtils.getReport(reportID); if (ReportUtils.isThread(report)) { const parentReportAction = ReportActionsUtils.getParentReportAction(report); - const childVisibleActionCount = parentReportAction.childVisibleActionCount ? parentReportAction.childVisibleActionCount + 1 : 1; - const childCommenterCount = parentReportAction.childCommenterCount ? parentReportAction.childCommenterCount : 1; - const childLastVisibleActionCreated = currentTime; - let childOldestFourAccountIDs = parentReportAction.childOldestFourAccountIDs; - if (!childOldestFourAccountIDs) { - childOldestFourAccountIDs = currentUserAccountID; - } else { - const oldestFourAccountIDs = childOldestFourAccountIDs.split(','); - const index = _.findIndex(oldestFourAccountIDs, (accountID) => accountID === currentUserAccountID); - if (index !== -1) { - oldestFourAccountIDs.splice(index, 1); - } - oldestFourAccountIDs.push(currentUserAccountID); - if (oldestFourAccountIDs.length > 4) { - oldestFourAccountIDs.splice(0, 1); - } - childOldestFourAccountIDs = oldestFourAccountIDs.join(','); - } - - const optimisticParentReportAction = { - childVisibleActionCount, - childCommenterCount, - childLastVisibleActionCreated, - childOldestFourAccountIDs, - }; - optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`, value: { - [parentReportAction.reportActionID]: optimisticParentReportAction, + [parentReportAction.reportActionID]: ReportUtils.updateOptimisticParentReportAction(parentReportAction, currentTime, CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD), }, }); } @@ -947,6 +921,22 @@ function deleteReportComment(reportID, reportAction) { }, ]; + const report = ReportUtils.getReport(reportID); + if (ReportUtils.isThread(report)) { + const parentReportAction = ReportActionsUtils.getParentReportAction(report); + 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, From bea0b7860820cfafa6da24e72cb5e13b84082a26 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Wed, 5 Jul 2023 12:03:51 +0700 Subject: [PATCH 04/10] fix childVisibleActionCount --- 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 a752e40e5f9b..b660707b4f40 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1245,7 +1245,7 @@ function buildOptimisticAddCommentReportAction(text, file) { */ function updateOptimisticParentReportAction(parentReportAction, lastVisibleActionCreated, type) { - let childVisibleActionCount = parentReportAction.childVisibleActionCount ? parentReportAction.childVisibleActionCount : 1; + let childVisibleActionCount = parentReportAction.childVisibleActionCount ? parentReportAction.childVisibleActionCount : 0; let childCommenterCount = parentReportAction.childCommenterCount ? parentReportAction.childCommenterCount : 1; const childLastVisibleActionCreated = lastVisibleActionCreated; let childOldestFourAccountIDs = parentReportAction.childOldestFourAccountIDs; From a4d911117e379e8ba6e44e00f3ae5e7789d3b07f Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Wed, 5 Jul 2023 12:18:51 +0700 Subject: [PATCH 05/10] update parent report action for all case --- src/libs/actions/Report.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 706afbd551f8..950b5af85df9 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -307,7 +307,7 @@ function addActions(reportID, text = '', file) { // Optimistically update the parent report action if the report is a thread const report = ReportUtils.getReport(reportID); - if (ReportUtils.isThread(report)) { + if (report && report.parentReportActionID) { const parentReportAction = ReportActionsUtils.getParentReportAction(report); optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, @@ -922,7 +922,7 @@ function deleteReportComment(reportID, reportAction) { ]; const report = ReportUtils.getReport(reportID); - if (ReportUtils.isThread(report)) { + if (report && report.parentReportActionID) { const parentReportAction = ReportActionsUtils.getParentReportAction(report); optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, From 3b49b05c49adfe350b0bf29615dd8358fe681be8 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Thu, 6 Jul 2023 15:56:25 +0700 Subject: [PATCH 06/10] refactor code of function --- src/libs/ReportUtils.js | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index a0b8897d3de7..00dde3c2acc2 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1254,38 +1254,35 @@ function buildOptimisticAddCommentReportAction(text, file) { } /** - * update optimistic reportAction for the parent report when a comment is added or remove in thread - * @param {String} parentReportAction - Parent report action of thread - * @param {String} lastVisibleActionCreated - Last visible action created of thread - * @param {String} type - The type of action in thread + * 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 ? parentReportAction.childVisibleActionCount : 0; - let childCommenterCount = parentReportAction.childCommenterCount ? parentReportAction.childCommenterCount : 1; - const childLastVisibleActionCreated = lastVisibleActionCreated; + 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; - if (!childOldestFourAccountIDs) { - childOldestFourAccountIDs = currentUserAccountID.toString(); - } else { - const oldestFourAccountIDs = childOldestFourAccountIDs.split(','); + const oldestFourAccountIDs = childOldestFourAccountIDs ? childOldestFourAccountIDs.split(',') : []; + if (oldestFourAccountIDs.length < 4) { const index = _.findIndex(oldestFourAccountIDs, (accountID) => accountID === currentUserAccountID.toString()); - if (index !== -1) { - oldestFourAccountIDs.splice(index, 1); - } - - oldestFourAccountIDs.push(currentUserAccountID); - if (oldestFourAccountIDs.length > 4) { - oldestFourAccountIDs.splice(0, 1); + if (index === -1) { + childCommenterCount += 1; + oldestFourAccountIDs.push(currentUserAccountID); } - childOldestFourAccountIDs = oldestFourAccountIDs.join(','); } + + if (!childCommenterCount) childCommenterCount = 1; + if (!oldestFourAccountIDs.length) oldestFourAccountIDs.push(currentUserAccountID); + + childOldestFourAccountIDs = oldestFourAccountIDs.join(','); } else { - childVisibleActionCount -= 1; + if (childVisibleActionCount > 0) childVisibleActionCount -= 1; if (childVisibleActionCount === 0) { childCommenterCount = 0; childOldestFourAccountIDs = ''; @@ -1295,7 +1292,7 @@ function updateOptimisticParentReportAction(parentReportAction, lastVisibleActio return { childVisibleActionCount, childCommenterCount, - childLastVisibleActionCreated, + childLastVisibleActionCreated: lastVisibleActionCreated, childOldestFourAccountIDs, }; } From 0b0f688fa5faba492120f5614b41db87216eab59 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Thu, 6 Jul 2023 17:46:47 +0700 Subject: [PATCH 07/10] check parentReportAction carefully --- src/libs/actions/Report.js | 40 +++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 6d1a3688be0d..2c8bf3373b25 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -309,13 +309,15 @@ function addActions(reportID, text = '', file) { const report = ReportUtils.getReport(reportID); if (report && report.parentReportActionID) { const parentReportAction = ReportActionsUtils.getParentReportAction(report); - 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), - }, - }); + 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 @@ -924,17 +926,19 @@ function deleteReportComment(reportID, reportAction) { const report = ReportUtils.getReport(reportID); if (report && report.parentReportActionID) { const parentReportAction = ReportActionsUtils.getParentReportAction(report); - 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, - ), - }, - }); + 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 = { From c34ed38f94dd8455260400b85ffc402b350d9b12 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Sat, 8 Jul 2023 10:45:59 +0700 Subject: [PATCH 08/10] clear condition --- src/libs/ReportUtils.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 7e53d7703191..b1679bfd42da 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1361,13 +1361,10 @@ function updateOptimisticParentReportAction(parentReportAction, lastVisibleActio oldestFourAccountIDs.push(currentUserAccountID); } } - - if (!childCommenterCount) childCommenterCount = 1; - if (!oldestFourAccountIDs.length) oldestFourAccountIDs.push(currentUserAccountID); - childOldestFourAccountIDs = oldestFourAccountIDs.join(','); - } else { + } else if (type === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) { if (childVisibleActionCount > 0) childVisibleActionCount -= 1; + if (childVisibleActionCount === 0) { childCommenterCount = 0; childOldestFourAccountIDs = ''; From e7a6d7edb0d7a8bca77b246b72dc5e947c8518ce Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Wed, 12 Jul 2023 06:32:18 +0700 Subject: [PATCH 09/10] change style in line --- src/libs/ReportUtils.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index b1679bfd42da..908208dba667 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1363,7 +1363,9 @@ function updateOptimisticParentReportAction(parentReportAction, lastVisibleActio } childOldestFourAccountIDs = oldestFourAccountIDs.join(','); } else if (type === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) { - if (childVisibleActionCount > 0) childVisibleActionCount -= 1; + if (childVisibleActionCount > 0) { + childVisibleActionCount -= 1; + } if (childVisibleActionCount === 0) { childCommenterCount = 0; From 7263151c9483d31fb785fa2382bca1bac12e7c3e Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Wed, 12 Jul 2023 06:49:57 +0700 Subject: [PATCH 10/10] fix bug --- src/libs/actions/Report.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index c632944681ad..2cd5cb846628 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -243,7 +243,7 @@ function addActions(reportID, text = '', file) { const currentTime = DateUtils.getDBTime(); - const prevVisibleMessageText = ReportActionsUtils.getLastVisibleMessageText(reportID); + const prevVisibleMessageText = ReportActionsUtils.getLastVisibleMessage(reportID); const lastCommentText = ReportUtils.formatReportLastMessageText(lastAction.message[0].text); const optimisticReport = {