Skip to content

Commit

Permalink
Merge pull request #38658 from bernhardoj/fix/38002-thread-replies-count
Browse files Browse the repository at this point in the history
Correctly update thread ancestors reply count
  • Loading branch information
puneetlath authored Mar 27, 2024
2 parents 9dbec73 + f5afda1 commit 6c204ec
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 42 deletions.
71 changes: 41 additions & 30 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: [],
Expand All @@ -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;
}

Expand All @@ -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;
Expand Down
18 changes: 12 additions & 6 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = {
Expand Down
18 changes: 12 additions & 6 deletions src/libs/actions/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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[] = [
Expand Down

0 comments on commit 6c204ec

Please sign in to comment.