diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 0bb3b1101b7c..ce4264b32141 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -3047,34 +3047,6 @@ function updateOptimisticParentReportAction(parentReportAction: OnyxEntry<Report }; } -/** - * Get optimistic data of parent report action - * @param reportID The reportID of the report that is updated - * @param lastVisibleActionCreated Last visible action created of the child report - * @param type The type of action in the child report - * @param parentReportID Custom reportID to be updated - * @param parentReportActionID Custom reportActionID to be updated - */ -function getOptimisticDataForParentReportAction(reportID: string, lastVisibleActionCreated: string, type: string, parentReportID = '', parentReportActionID = ''): OnyxUpdate | EmptyObject { - const report = getReport(reportID); - if (!report || isEmptyObject(report)) { - return {}; - } - const parentReportAction = ReportActionsUtils.getParentReportAction(report); - if (!parentReportAction || isEmptyObject(parentReportAction)) { - return {}; - } - - const optimisticParentReportAction = updateOptimisticParentReportAction(parentReportAction, lastVisibleActionCreated, type); - return { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID || report?.parentReportID}`, - value: { - [parentReportActionID || (report?.parentReportActionID ?? '')]: optimisticParentReportAction, - }, - }; -} - /** * Builds an optimistic reportAction for the parent report when a task is created * @param taskReportID - Report ID of the task @@ -5409,7 +5381,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 = false): AncestorIDs { if (!report) { return { reportIDs: [], @@ -5428,7 +5400,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; } @@ -5442,6 +5414,45 @@ function getAllAncestorReportActionIDs(report: Report | null | undefined): Ances return allAncestorIDs; } +/** + * Get optimistic data of parent report action + * @param reportID The reportID of the report that is updated + * @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): Array<OnyxUpdate | EmptyObject> { + const report = getReport(reportID); + + if (!report || isEmptyObject(report)) { + return []; + } + + const ancestors = getAllAncestorReportActionIDs(report, true); + 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}`, + value: { + [ancestorReportAction?.reportActionID ?? '']: updateOptimisticParentReportAction(ancestorReportAction, lastVisibleActionCreated, type), + }, + }; + }); +} + function canBeAutoReimbursed(report: OnyxEntry<Report>, policy: OnyxEntry<Policy> | EmptyObject): boolean { if (isEmptyObject(policy)) { return false; diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 93154bfff16b..8cea9cbd0dae 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -490,9 +490,12 @@ 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) => { + 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 if (DateUtils.canUpdateTimezone() && currentUserAccountID) { @@ -1227,9 +1230,12 @@ function deleteReportComment(reportID: string, reportAction: ReportAction) { optimisticReport?.lastVisibleActionCreated ?? '', CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, ); - if (!isEmptyObject(optimisticParentReportData)) { - optimisticData.push(optimisticParentReportData); - } + optimisticParentReportData.forEach((parentReportData) => { + if (isEmptyObject(parentReportData)) { + return; + } + optimisticData.push(parentReportData); + }); } const parameters: DeleteCommentParams = { diff --git a/src/libs/actions/Task.ts b/src/libs/actions/Task.ts index 3a7e29be09a3..690d62f02f48 100644 --- a/src/libs/actions/Task.ts +++ b/src/libs/actions/Task.ts @@ -249,9 +249,12 @@ 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) => { + if (isEmptyObject(parentReportData)) { + return; + } + optimisticData.push(parentReportData); + }); // FOR PARENT REPORT (SHARE DESTINATION) successData.push({ @@ -865,9 +868,12 @@ function deleteTask(report: OnyxEntry<OnyxTypes.Report>) { parentReport?.lastVisibleActionCreated ?? '', CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, ); - if (!isEmptyObject(optimisticParentReportData)) { - optimisticData.push(optimisticParentReportData); - } + optimisticParentReportData.forEach((parentReportData) => { + if (isEmptyObject(parentReportData)) { + return; + } + optimisticData.push(parentReportData); + }); } const successData: OnyxUpdate[] = [