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

Update optimistic data for parent report when adding comment #20762

Merged
merged 17 commits into from
Jul 13, 2023
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
44 changes: 44 additions & 0 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,49 @@ function buildOptimisticAddCommentReportAction(text, file) {
};
}

/**
* update optimistic parent reportAction when a comment is added or remove in the child report
* @param {String} parentReportAction - Parent report action of the child report
* @param {String} lastVisibleActionCreated - Last visible action created of the child report
* @param {String} type - The type of action in the child report
* @returns {Object}
*/

function updateOptimisticParentReportAction(parentReportAction, lastVisibleActionCreated, type) {
let childVisibleActionCount = parentReportAction.childVisibleActionCount || 0;
let childCommenterCount = parentReportAction.childCommenterCount || 0;
let childOldestFourAccountIDs = parentReportAction.childOldestFourAccountIDs;

if (type === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) {
childVisibleActionCount += 1;
const oldestFourAccountIDs = childOldestFourAccountIDs ? childOldestFourAccountIDs.split(',') : [];
if (oldestFourAccountIDs.length < 4) {
const index = _.findIndex(oldestFourAccountIDs, (accountID) => accountID === currentUserAccountID.toString());
if (index === -1) {
childCommenterCount += 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused why we are adding a commenter based on the oldestFourAccountIDs. We want to check that they've never commented in the thread before changing this right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this case, oldestFourAccountIDs.length < 4 and the user isn't already in list that mean this is the first time the user comment, so I think increase childCommenterCount for this case makes sense.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should still be extracted outside of the <4 section actually. With the code you have here, we will only increase the childCommenterCount if there are less than 4. We need to increase that regardless of how many people have commented, if it is this user's first time commenting. Right?

Copy link
Contributor Author

@dukenv0307 dukenv0307 Jul 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should still be extracted outside of the <4 section actually. With the code you have here, we will only increase the childCommenterCount if there are less than 4. We need to increase that regardless of how many people have commented, if it is this user's first time commenting. Right?

@stitesExpensify We update childCommenterCount for this case because we know for sure that it's the user's first comment. For the case, oldestFourAccountIDs.length === 4, in FE we don't know the user had commented before or not. So we dont't update childCommenterCount.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

childCommenterCount value isn't that important in frontend as long as it meets > 0.
Only used here:

const shouldDisplayThreadReplies = hasReplies && props.action.childCommenterCount && !ReportUtils.isThreadFirstChat(props.action, props.report.reportID);

oldestFourAccountIDs.push(currentUserAccountID);
}
}
childOldestFourAccountIDs = oldestFourAccountIDs.join(',');
} else if (type === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
if (childVisibleActionCount > 0) {
childVisibleActionCount -= 1;
}

if (childVisibleActionCount === 0) {
childCommenterCount = 0;
childOldestFourAccountIDs = '';
}
}

return {
childVisibleActionCount,
childCommenterCount,
childLastVisibleActionCreated: lastVisibleActionCreated,
childOldestFourAccountIDs,
};
}

/**
* 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 @@ -2549,6 +2592,7 @@ export {
buildOptimisticTaskReportAction,
buildOptimisticAddCommentReportAction,
buildOptimisticTaskCommentReportAction,
updateOptimisticParentReportAction,
shouldReportBeInOptionList,
getChatByParticipants,
getChatByParticipantsByLoginList,
Expand Down
33 changes: 33 additions & 0 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,21 @@ 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 the timezone if it's been 5 minutes from the last time the user added a comment
if (DateUtils.canUpdateTimezone()) {
const timezone = DateUtils.getCurrentTimezone();
Expand Down Expand Up @@ -926,6 +941,24 @@ 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,
),
},
});
}
}

const parameters = {
reportID: originalReportID,
reportActionID,
Expand Down