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

Add closed report action optimistically when workspace is closed #14343

Merged
merged 8 commits into from
Jan 24, 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
48 changes: 48 additions & 0 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,53 @@ function buildOptimisticCreatedReportAction(ownerEmail) {
};
}

/**
* Returns the necessary reportAction onyx data to indicate that a chat has been archived
*
* @param {Number} sequenceNumber
* @param {String} ownerEmail
* @param {String} policyName
* @param {String} reason - A reason why the chat has been archived
* @returns {Object}
*/
function buildOptimisticClosedReportAction(sequenceNumber, ownerEmail, policyName, reason = CONST.REPORT.ARCHIVE_REASON.DEFAULT) {
return {
actionName: CONST.REPORT.ACTIONS.TYPE.CLOSED,
actorAccountID: currentUserAccountID,
automatic: false,
avatar: lodashGet(allPersonalDetails, [currentUserEmail, 'avatar'], getDefaultAvatar(currentUserEmail)),
clientID: NumberUtils.generateReportActionClientID(),
created: DateUtils.getDBTime(),
Copy link
Contributor

Choose a reason for hiding this comment

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

@mountiny Can you explain why we need to use created here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

as any report action, this indicated when the action took place, ie when the chat was archived

message: [
{
type: CONST.REPORT.MESSAGE.TYPE.TEXT,
style: 'strong',
text: ownerEmail === currentUserEmail ? 'You' : ownerEmail,
},
{
type: CONST.REPORT.MESSAGE.TYPE.TEXT,
style: 'normal',
text: ' closed this report',
},
],
originalMessage: {
policyName,
reason,
},
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
person: [
{
type: CONST.REPORT.MESSAGE.TYPE.TEXT,
style: 'strong',
text: lodashGet(allPersonalDetails, [currentUserEmail, 'displayName'], currentUserEmail),
},
],
reportActionID: NumberUtils.rand64(),
sequenceNumber,
shouldShow: true,
};
}

/**
* @param {String} policyID
* @param {String} policyName
Expand Down Expand Up @@ -1258,6 +1305,7 @@ export {
isUnread,
buildOptimisticWorkspaceChats,
buildOptimisticChatReport,
buildOptimisticClosedReportAction,
buildOptimisticCreatedReportAction,
buildOptimisticIOUReport,
buildOptimisticIOUReportAction,
Expand Down
22 changes: 21 additions & 1 deletion src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import ROUTES from '../../ROUTES';
import * as OptionsListUtils from '../OptionsListUtils';
import DateUtils from '../DateUtils';
import * as ReportUtils from '../ReportUtils';
import * as Report from './Report';
import Log from '../Log';

const allPolicies = {};
Expand Down Expand Up @@ -71,8 +72,9 @@ function updateLastAccessedWorkspace(policyID) {
*
* @param {String} policyID
* @param {Array<Object>} reports
* @param {String} policyName
*/
function deleteWorkspace(policyID, reports) {
function deleteWorkspace(policyID, reports, policyName) {
const optimisticData = [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
Expand All @@ -92,6 +94,24 @@ function deleteWorkspace(policyID, reports) {
oldPolicyName: allPolicies[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`].name,
},
})),

// Add closed actions to all chat reports linked to this policy
..._.map(reports, ({reportID, ownerEmail}) => {
const highestSequenceNumber = Report.getMaxSequenceNumber(reportID);
const optimisticClosedReportAction = ReportUtils.buildOptimisticClosedReportAction(
highestSequenceNumber + 1,
ownerEmail,
policyName,
CONST.REPORT.ARCHIVE_REASON.POLICY_DELETED,
);
const optimisticReportActions = {};
optimisticReportActions[optimisticClosedReportAction.clientID] = optimisticClosedReportAction;
return {
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
value: optimisticReportActions,
};
}),
];

// Restore the old report stateNum and statusNum
Expand Down
2 changes: 1 addition & 1 deletion src/pages/home/report/ReportActionsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class ReportActionsList extends React.Component {
// Make sure the oldest report action loaded is not the first. This is so we do not show the
// skeleton view above the created action in a newly generated optimistic chat or one with not
// that many comments.
const lastReportAction = _.last(this.props.sortedReportActions);
const lastReportAction = _.last(this.props.sortedReportActions) || {};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change is fixing a different bug I discovered when working on this PR. When the workspace has been deleted, navigating to the archived chats threw error, because the this.props.sortedReportActions was an empty string resulting in lastReportAction being undefined. Hence next line threw error.

if (this.props.report.isLoadingReportActions && lastReportAction.sequenceNumber > 0) {
return (
<ReportActionsSkeletonView
Expand Down
2 changes: 1 addition & 1 deletion src/pages/workspace/WorkspaceInitialPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class WorkspaceInitialPage extends React.Component {
*/
confirmDeleteAndHideModal() {
const policyReports = _.filter(this.props.reports, report => report && report.policyID === this.props.policy.id);
Policy.deleteWorkspace(this.props.policy.id, policyReports);
Policy.deleteWorkspace(this.props.policy.id, policyReports, this.props.policy.name);
this.toggleDeleteModal(false);
Navigation.navigate(ROUTES.SETTINGS_WORKSPACES);
}
Expand Down
Loading