Skip to content

Commit

Permalink
Merge pull request #14343 from Expensify/vit-updateArchiveMessageForA…
Browse files Browse the repository at this point in the history
…dmin

Add closed report action optimistically when workspace is closed
  • Loading branch information
mountiny authored Jan 24, 2023
2 parents c2ff5f8 + 93199c4 commit 4ddb47e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
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(),
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) || {};
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

0 comments on commit 4ddb47e

Please sign in to comment.