Skip to content

Commit

Permalink
Merge pull request #22990 from dukenv0307/fix/20570
Browse files Browse the repository at this point in the history
Update optimistic data for parent report action when completing or reopening task
  • Loading branch information
MariaHCD authored Jul 19, 2023
2 parents 772b469 + bcd496a commit 40f354b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 29 deletions.
25 changes: 25 additions & 0 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,30 @@ function updateOptimisticParentReportAction(parentReportAction, lastVisibleActio
};
}

/**
* Get optimistic data of parent report action
* @param {String} reportID The reportID of the report that is updated
* @param {String} lastVisibleActionCreated Last visible action created of the child report
* @param {String} type The type of action in the child report
* @returns {Object}
*/
const getOptimisticDataForParentReportAction = (reportID, lastVisibleActionCreated, type) => {
const report = getReport(reportID);
if (report && report.parentReportActionID) {
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
if (parentReportAction && parentReportAction.reportActionID) {
return {
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`,
value: {
[parentReportAction.reportActionID]: updateOptimisticParentReportAction(parentReportAction, lastVisibleActionCreated, type),
},
};
}
}
return {};
};

/**
* Builds an optimistic reportAction for the parent report when a task is created
* @param {String} taskReportID - Report ID of the task
Expand Down Expand Up @@ -2618,6 +2642,7 @@ export {
buildOptimisticAddCommentReportAction,
buildOptimisticTaskCommentReportAction,
updateOptimisticParentReportAction,
getOptimisticDataForParentReportAction,
shouldReportBeInOptionList,
getChatByParticipants,
getChatByParticipantsByLoginList,
Expand Down
37 changes: 8 additions & 29 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,19 +321,10 @@ function addActions(reportID, text = '', file) {
},
];

// Optimistically update the parent report action if the report is a thread
const report = ReportUtils.getReport(reportID);
if (report && report.parentReportActionID) {
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
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 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 (!_.isEmpty(optimisticParentReportData)) {
optimisticData.push(optimisticParentReportData);
}

// Update the timezone if it's been 5 minutes from the last time the user added a comment
Expand Down Expand Up @@ -959,22 +950,10 @@ function deleteReportComment(reportID, reportAction) {
},
];

const report = ReportUtils.getReport(reportID);
if (report && report.parentReportActionID) {
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
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,
),
},
});
}
// Update optimistic data for parent report action if the report is a child report
const optimisticParentReportData = ReportUtils.getOptimisticDataForParentReportAction(reportID, optimisticReport.lastVisibleActionCreated, CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE);
if (!_.isEmpty(optimisticParentReportData)) {
optimisticData.push(optimisticParentReportData);
}

const parameters = {
Expand Down
12 changes: 12 additions & 0 deletions src/libs/actions/Task.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ function completeTask(taskReportID, taskTitle) {
},
];

// Update optimistic data for parent report action
const optimisticParentReportData = ReportUtils.getOptimisticDataForParentReportAction(taskReportID, completedTaskReportAction.created, CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
if (!_.isEmpty(optimisticParentReportData)) {
optimisticData.push(optimisticParentReportData);
}

API.write(
'CompleteTask',
{
Expand Down Expand Up @@ -312,6 +318,12 @@ function reopenTask(taskReportID, taskTitle) {
},
];

// Update optimistic data for parent report action
const optimisticParentReportData = ReportUtils.getOptimisticDataForParentReportAction(taskReportID, reopenedTaskReportAction.created, CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
if (!_.isEmpty(optimisticParentReportData)) {
optimisticData.push(optimisticParentReportData);
}

API.write(
'ReopenTask',
{
Expand Down

0 comments on commit 40f354b

Please sign in to comment.