From 5a71949f4836a4cb227c70e30024430103c964f2 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 20 Mar 2024 14:21:30 +0800 Subject: [PATCH 1/5] update all parent/ancestor thread data --- src/libs/ReportUtils.ts | 52 ++++++++++++++++++-------------------- src/libs/actions/Report.ts | 8 ++---- src/libs/actions/Task.ts | 8 ++---- 3 files changed, 28 insertions(+), 40 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 826d58ee6ecc..4e4a1f0446f1 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -2957,34 +2957,6 @@ function updateOptimisticParentReportAction(parentReportAction: OnyxEntry { + const ancestorReport = getReport(ancestors.reportIDs[index]); + const ancestorReportAction = ReportActionsUtils.getReportAction(ancestorReport.reportID, ancestors.reportActionsIDs[index]); + return { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${ancestorReport.reportID}`, + value: { + [ancestorReportAction?.reportActionID ?? '']: updateOptimisticParentReportAction(ancestorReportAction, lastVisibleActionCreated, type), + }, + }; + }); +} + function canBeAutoReimbursed(report: OnyxEntry, policy: OnyxEntry | EmptyObject): boolean { if (isEmptyObject(policy)) { return false; diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 49ecfce36cf0..4251fdfbcb76 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -504,9 +504,7 @@ function addActions(reportID: string, text = '', file?: FileObject) { // 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 (!isEmptyObject(optimisticParentReportData)) { - optimisticData.push(optimisticParentReportData); - } + optimisticParentReportData.forEach((parentReportData) => optimisticData.push(parentReportData)); // Update the timezone if it's been 5 minutes from the last time the user added a comment if (DateUtils.canUpdateTimezone() && currentUserAccountID) { @@ -1228,9 +1226,7 @@ function deleteReportComment(reportID: string, reportAction: ReportAction) { optimisticReport?.lastVisibleActionCreated ?? '', CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, ); - if (!isEmptyObject(optimisticParentReportData)) { - optimisticData.push(optimisticParentReportData); - } + optimisticParentReportData.forEach((parentReportData) => optimisticData.push(parentReportData)); } const parameters: DeleteCommentParams = { diff --git a/src/libs/actions/Task.ts b/src/libs/actions/Task.ts index 681ed0ec383f..e3a9eab8ebe8 100644 --- a/src/libs/actions/Task.ts +++ b/src/libs/actions/Task.ts @@ -249,9 +249,7 @@ function createTaskAndNavigate( // If needed, update optimistic data for parent report action of the parent report. const optimisticParentReportData = ReportUtils.getOptimisticDataForParentReportAction(parentReportID, currentTime, CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD); - if (!isEmptyObject(optimisticParentReportData)) { - optimisticData.push(optimisticParentReportData); - } + optimisticParentReportData.forEach((parentReportData) => optimisticData.push(parentReportData)); // FOR PARENT REPORT (SHARE DESTINATION) successData.push({ @@ -861,9 +859,7 @@ function deleteTask(report: OnyxEntry) { parentReport?.lastVisibleActionCreated ?? '', CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, ); - if (!isEmptyObject(optimisticParentReportData)) { - optimisticData.push(optimisticParentReportData); - } + optimisticParentReportData.forEach((parentReportData) => optimisticData.push(parentReportData)); } const successData: OnyxUpdate[] = [ From cbdba9e41b763dbdc906647a077b6d732dcf1e3e Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 20 Mar 2024 14:44:46 +0800 Subject: [PATCH 2/5] fix type --- src/libs/ReportUtils.ts | 17 ++++++++++++++++- src/libs/actions/Report.ts | 12 ++++++++++-- src/libs/actions/Task.ts | 12 ++++++++++-- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 4e4a1f0446f1..db6587b47ca8 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -5258,14 +5258,29 @@ function getAllAncestorReportActionIDs(report: Report | null | undefined): Ances * @param lastVisibleActionCreated Last visible action created of the child report * @param type The type of action in the child report */ -function getOptimisticDataForParentReportAction(reportID: string, lastVisibleActionCreated: string, type: string): OnyxUpdate[] { +function getOptimisticDataForParentReportAction(reportID: string, lastVisibleActionCreated: string, type: string): (OnyxUpdate | EmptyObject)[] { const report = getReport(reportID); + + if (!report || isEmptyObject(report)) { + return []; + } + const ancestors = getAllAncestorReportActionIDs(report); const totalAncestor = ancestors.reportIDs.length; return Array.from(Array(totalAncestor), (_, index) => { const ancestorReport = getReport(ancestors.reportIDs[index]); + + if (!ancestorReport || isEmptyObject(ancestorReport)) { + return {} as EmptyObject; + } + const ancestorReportAction = ReportActionsUtils.getReportAction(ancestorReport.reportID, ancestors.reportActionsIDs[index]); + + if (!ancestorReportAction || isEmptyObject(ancestorReportAction)) { + return {} as EmptyObject; + } + return { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${ancestorReport.reportID}`, diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 4251fdfbcb76..f35c89120a66 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -504,7 +504,11 @@ function addActions(reportID: string, text = '', file?: FileObject) { // 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); - optimisticParentReportData.forEach((parentReportData) => optimisticData.push(parentReportData)); + optimisticParentReportData.forEach((parentReportData) => { + if (!isEmptyObject(parentReportData)) { + optimisticData.push(parentReportData); + } + }); // Update the timezone if it's been 5 minutes from the last time the user added a comment if (DateUtils.canUpdateTimezone() && currentUserAccountID) { @@ -1226,7 +1230,11 @@ function deleteReportComment(reportID: string, reportAction: ReportAction) { optimisticReport?.lastVisibleActionCreated ?? '', CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, ); - optimisticParentReportData.forEach((parentReportData) => optimisticData.push(parentReportData)); + optimisticParentReportData.forEach((parentReportData) => { + if (!isEmptyObject(parentReportData)) { + optimisticData.push(parentReportData); + } + }); } const parameters: DeleteCommentParams = { diff --git a/src/libs/actions/Task.ts b/src/libs/actions/Task.ts index e3a9eab8ebe8..9de13f5e4138 100644 --- a/src/libs/actions/Task.ts +++ b/src/libs/actions/Task.ts @@ -249,7 +249,11 @@ function createTaskAndNavigate( // If needed, update optimistic data for parent report action of the parent report. const optimisticParentReportData = ReportUtils.getOptimisticDataForParentReportAction(parentReportID, currentTime, CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD); - optimisticParentReportData.forEach((parentReportData) => optimisticData.push(parentReportData)); + optimisticParentReportData.forEach((parentReportData) => { + if (!isEmptyObject(parentReportData)) { + optimisticData.push(parentReportData); + } + }); // FOR PARENT REPORT (SHARE DESTINATION) successData.push({ @@ -859,7 +863,11 @@ function deleteTask(report: OnyxEntry) { parentReport?.lastVisibleActionCreated ?? '', CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, ); - optimisticParentReportData.forEach((parentReportData) => optimisticData.push(parentReportData)); + optimisticParentReportData.forEach((parentReportData) => { + if (!isEmptyObject(parentReportData)) { + optimisticData.push(parentReportData); + } + }); } const successData: OnyxUpdate[] = [ From 4abbbd4b01c81e2db3f23305b9bf23ca71602540 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 20 Mar 2024 14:52:11 +0800 Subject: [PATCH 3/5] fix lint --- src/libs/ReportUtils.ts | 2 +- src/libs/actions/Report.ts | 10 ++++++---- src/libs/actions/Task.ts | 10 ++++++---- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index db6587b47ca8..4463d484e79c 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -5258,7 +5258,7 @@ function getAllAncestorReportActionIDs(report: Report | null | undefined): Ances * @param lastVisibleActionCreated Last visible action created of the child report * @param type The type of action in the child report */ -function getOptimisticDataForParentReportAction(reportID: string, lastVisibleActionCreated: string, type: string): (OnyxUpdate | EmptyObject)[] { +function getOptimisticDataForParentReportAction(reportID: string, lastVisibleActionCreated: string, type: string): Array { const report = getReport(reportID); if (!report || isEmptyObject(report)) { diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index f35c89120a66..f39acba8268c 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -505,9 +505,10 @@ function addActions(reportID: string, text = '', file?: FileObject) { // 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); optimisticParentReportData.forEach((parentReportData) => { - if (!isEmptyObject(parentReportData)) { - optimisticData.push(parentReportData); + if (isEmptyObject(parentReportData)) { + return; } + optimisticData.push(parentReportData); }); // Update the timezone if it's been 5 minutes from the last time the user added a comment @@ -1231,9 +1232,10 @@ function deleteReportComment(reportID: string, reportAction: ReportAction) { CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, ); optimisticParentReportData.forEach((parentReportData) => { - if (!isEmptyObject(parentReportData)) { - optimisticData.push(parentReportData); + if (isEmptyObject(parentReportData)) { + return; } + optimisticData.push(parentReportData); }); } diff --git a/src/libs/actions/Task.ts b/src/libs/actions/Task.ts index 9de13f5e4138..e98acda277b0 100644 --- a/src/libs/actions/Task.ts +++ b/src/libs/actions/Task.ts @@ -250,9 +250,10 @@ function createTaskAndNavigate( // If needed, update optimistic data for parent report action of the parent report. const optimisticParentReportData = ReportUtils.getOptimisticDataForParentReportAction(parentReportID, currentTime, CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD); optimisticParentReportData.forEach((parentReportData) => { - if (!isEmptyObject(parentReportData)) { - optimisticData.push(parentReportData); + if (isEmptyObject(parentReportData)) { + return; } + optimisticData.push(parentReportData); }); // FOR PARENT REPORT (SHARE DESTINATION) @@ -864,9 +865,10 @@ function deleteTask(report: OnyxEntry) { CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, ); optimisticParentReportData.forEach((parentReportData) => { - if (!isEmptyObject(parentReportData)) { - optimisticData.push(parentReportData); + if (isEmptyObject(parentReportData)) { + return; } + optimisticData.push(parentReportData); }); } From 7e945138e77394834e5e03662864e8a8eae7b351 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 20 Mar 2024 16:02:21 +0800 Subject: [PATCH 4/5] allow to include transaction thread --- src/libs/ReportUtils.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 4463d484e79c..30ea323f4d0f 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -5219,7 +5219,7 @@ function getAllAncestorReportActions(report: Report | null | undefined, shouldHi return allAncestors.reverse(); } -function getAllAncestorReportActionIDs(report: Report | null | undefined): AncestorIDs { +function getAllAncestorReportActionIDs(report: Report | null | undefined, includeTransactionThread: boolean = false): AncestorIDs { if (!report) { return { reportIDs: [], @@ -5238,7 +5238,7 @@ function getAllAncestorReportActionIDs(report: Report | null | undefined): Ances const parentReport = getReport(parentReportID); const parentReportAction = ReportActionsUtils.getReportAction(parentReportID, parentReportActionID ?? '0'); - if (!parentReportAction || ReportActionsUtils.isTransactionThread(parentReportAction) || !parentReport) { + if (!parentReportAction || (!includeTransactionThread && ReportActionsUtils.isTransactionThread(parentReportAction)) || !parentReport) { break; } @@ -5265,9 +5265,11 @@ function getOptimisticDataForParentReportAction(reportID: string, lastVisibleAct return []; } - const ancestors = getAllAncestorReportActionIDs(report); + const ancestors = getAllAncestorReportActionIDs(report, true); const totalAncestor = ancestors.reportIDs.length; + console.log('total ancestor', totalAncestor) + return Array.from(Array(totalAncestor), (_, index) => { const ancestorReport = getReport(ancestors.reportIDs[index]); From f5afda1ee8707a0719593bd1965ccf28239b3f6f Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 20 Mar 2024 16:08:09 +0800 Subject: [PATCH 5/5] fix lint --- src/libs/ReportUtils.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 30ea323f4d0f..21b2071e0504 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -5219,7 +5219,7 @@ function getAllAncestorReportActions(report: Report | null | undefined, shouldHi return allAncestors.reverse(); } -function getAllAncestorReportActionIDs(report: Report | null | undefined, includeTransactionThread: boolean = false): AncestorIDs { +function getAllAncestorReportActionIDs(report: Report | null | undefined, includeTransactionThread = false): AncestorIDs { if (!report) { return { reportIDs: [], @@ -5268,8 +5268,6 @@ function getOptimisticDataForParentReportAction(reportID: string, lastVisibleAct const ancestors = getAllAncestorReportActionIDs(report, true); const totalAncestor = ancestors.reportIDs.length; - console.log('total ancestor', totalAncestor) - return Array.from(Array(totalAncestor), (_, index) => { const ancestorReport = getReport(ancestors.reportIDs[index]);