Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly update thread ancestors reply count #38658

Merged
merged 5 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 41 additions & 30 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2957,34 +2957,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 @@ -5247,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 = false): AncestorIDs {
if (!report) {
puneetlath marked this conversation as resolved.
Show resolved Hide resolved
return {
reportIDs: [],
Expand All @@ -5266,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;
}

Expand All @@ -5280,6 +5252,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 @@ -504,9 +504,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 @@ -1228,9 +1231,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 @@ -861,9 +864,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
Loading