Skip to content

Commit

Permalink
Build method to create closed optimistic actions
Browse files Browse the repository at this point in the history
  • Loading branch information
mountiny committed Jan 16, 2023
1 parent a59825c commit 55cbf0f
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
72 changes: 72 additions & 0 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,77 @@ function buildOptimisticCreatedReportAction(ownerEmail) {
};
}

/**
* Returns the necessary reportAction onyx data to indicate that a chat has been
* @param {Number} sequenceNumber
* @param {String} ownerEmail
* @param {String} policyName
* @param {String} reason - 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,
};
}

// actionName: "CLOSED"
// actorAccountID: 32
// actorEmail: "test3@test.com"
// automatic: false
// avatar: "https://d2g02b6ed2w9fz.cloudfront.net/dea8ae7de1308911b35e0b1189ca85d379dbdb83_128.jpeg"
// clientID: ""
// created: "2023-01-16 19:18:52.966"
// message: Array(2)
// 0:
// {type: 'TEXT', style: 'strong', text: 'You'}
// 1:
// {type: 'TEXT', style: 'normal', text: ' closed this report'}

// originalMessage:
// policyName: "Test's Workspace"
// reason: "policyDeleted"
// person: Array(1)
// 0: {type: 'TEXT', style: 'strong', text: 'Test User'}
// length: 1
// reportActionID: "8939705822345818810"
// reportActionTimestamp: 1673896732966
// sequenceNumber: 1
// shouldShow: true
// timestamp: 1673896732

/**
* @param {String} policyID
* @param {String} policyName
Expand Down Expand Up @@ -1250,6 +1321,7 @@ export {
isUnread,
buildOptimisticWorkspaceChats,
buildOptimisticChatReport,
buildOptimisticClosedReportAction,
buildOptimisticCreatedReportAction,
buildOptimisticIOUReport,
buildOptimisticIOUReportAction,
Expand Down
20 changes: 19 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 {getMaxSequenceNumber} from './Report';
import Log from '../Log';

const allPolicies = {};
Expand Down Expand Up @@ -59,8 +60,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 @@ -80,6 +82,22 @@ function deleteWorkspace(policyID, reports) {
oldPolicyName: allPolicies[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`].name,
},
})),
..._.map(reports, ({reportID, ownerEmail}) => {
const highestSequenceNumber = 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/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 55cbf0f

Please sign in to comment.