From 55cbf0f6d21e4502e1d54bcb623a372af210bb6a Mon Sep 17 00:00:00 2001 From: Vit Horacek Date: Mon, 16 Jan 2023 22:23:54 +0100 Subject: [PATCH 1/7] Build method to create closed optimistic actions --- src/libs/ReportUtils.js | 72 +++++++++++++++++++++ src/libs/actions/Policy.js | 20 +++++- src/pages/workspace/WorkspaceInitialPage.js | 2 +- 3 files changed, 92 insertions(+), 2 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 45b9319a430a..68a9714a80e6 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -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 @@ -1250,6 +1321,7 @@ export { isUnread, buildOptimisticWorkspaceChats, buildOptimisticChatReport, + buildOptimisticClosedReportAction, buildOptimisticCreatedReportAction, buildOptimisticIOUReport, buildOptimisticIOUReportAction, diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 5a2de0e7e1f6..f6081486cda1 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -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 = {}; @@ -59,8 +60,9 @@ function updateLastAccessedWorkspace(policyID) { * * @param {String} policyID * @param {Array} reports + * @param {String} policyName */ -function deleteWorkspace(policyID, reports) { +function deleteWorkspace(policyID, reports, policyName) { const optimisticData = [ { onyxMethod: CONST.ONYX.METHOD.MERGE, @@ -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 diff --git a/src/pages/workspace/WorkspaceInitialPage.js b/src/pages/workspace/WorkspaceInitialPage.js index 9ee7b141c5bc..74d3706e31e5 100644 --- a/src/pages/workspace/WorkspaceInitialPage.js +++ b/src/pages/workspace/WorkspaceInitialPage.js @@ -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); } From a84aab4322c6d6d0bd3e333e96b0f47b94a340e0 Mon Sep 17 00:00:00 2001 From: Vit Horacek Date: Tue, 17 Jan 2023 00:33:54 +0100 Subject: [PATCH 2/7] Add a comment --- src/libs/actions/Policy.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index f6081486cda1..a8ba6e618217 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -82,6 +82,8 @@ function deleteWorkspace(policyID, reports, policyName) { oldPolicyName: allPolicies[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`].name, }, })), + + // Add closed actions to all chat reports linked to this policy ..._.map(reports, ({reportID, ownerEmail}) => { const highestSequenceNumber = getMaxSequenceNumber(reportID); const optimisticClosedReportAction = ReportUtils.buildOptimisticClosedReportAction( From 742e4e7ba1b85df06d6aff463fea0d436bffca66 Mon Sep 17 00:00:00 2001 From: Vit Horacek Date: Tue, 17 Jan 2023 00:36:09 +0100 Subject: [PATCH 3/7] Clean up --- src/libs/ReportUtils.js | 27 +-------------------------- 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 68a9714a80e6..3779eae9f267 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -951,7 +951,7 @@ function buildOptimisticCreatedReportAction(ownerEmail) { } /** - * Returns the necessary reportAction onyx data to indicate that a chat has been + * Returns the necessary reportAction onyx data to indicate that a chat has been archived * @param {Number} sequenceNumber * @param {String} ownerEmail * @param {String} policyName @@ -996,31 +996,6 @@ function buildOptimisticClosedReportAction(sequenceNumber, ownerEmail, policyNam }; } -// 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 From a0bc6049db1f7162781c1d126c93c7222a181723 Mon Sep 17 00:00:00 2001 From: Vit Horacek Date: Fri, 20 Jan 2023 22:50:24 +0000 Subject: [PATCH 4/7] Default to empty object in case report actions are missing --- src/pages/home/report/ReportActionsList.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/home/report/ReportActionsList.js b/src/pages/home/report/ReportActionsList.js index 36638f521dc7..5e61a042193a 100644 --- a/src/pages/home/report/ReportActionsList.js +++ b/src/pages/home/report/ReportActionsList.js @@ -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 ( Date: Fri, 20 Jan 2023 22:57:18 +0000 Subject: [PATCH 5/7] Fix the import --- src/libs/actions/Policy.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 97819c60487d..2cbacf64061f 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -13,7 +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 * as Report from './Report'; import Log from '../Log'; const allPolicies = {}; @@ -97,7 +97,7 @@ function deleteWorkspace(policyID, reports, policyName) { // Add closed actions to all chat reports linked to this policy ..._.map(reports, ({reportID, ownerEmail}) => { - const highestSequenceNumber = getMaxSequenceNumber(reportID); + const highestSequenceNumber = Report.getMaxSequenceNumber(reportID); const optimisticClosedReportAction = ReportUtils.buildOptimisticClosedReportAction( highestSequenceNumber + 1, ownerEmail, From 3574093c46e12e9da2ed1119369e165a471ab319 Mon Sep 17 00:00:00 2001 From: Vit Horacek Date: Fri, 20 Jan 2023 22:57:50 +0000 Subject: [PATCH 6/7] Fix style --- src/libs/actions/Policy.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 2cbacf64061f..7716c36c7f21 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -102,7 +102,7 @@ function deleteWorkspace(policyID, reports, policyName) { highestSequenceNumber + 1, ownerEmail, policyName, - CONST.REPORT.ARCHIVE_REASON.POLICY_DELETED + CONST.REPORT.ARCHIVE_REASON.POLICY_DELETED, ); const optimisticReportActions = {}; optimisticReportActions[optimisticClosedReportAction.clientID] = optimisticClosedReportAction; @@ -110,7 +110,7 @@ function deleteWorkspace(policyID, reports, policyName) { onyxMethod: CONST.ONYX.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, value: optimisticReportActions, - } + }; }), ]; From 93199c41a543a867d6ab54aadbd8cf61ff9e1451 Mon Sep 17 00:00:00 2001 From: Vit Horacek Date: Fri, 20 Jan 2023 22:59:15 +0000 Subject: [PATCH 7/7] Update JSDoc of the build closed report action method --- src/libs/ReportUtils.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 066802b3705f..70dadec83653 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -952,10 +952,11 @@ 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 - Reason why the chat has been archived + * @param {String} reason - A reason why the chat has been archived * @returns {Object} */ function buildOptimisticClosedReportAction(sequenceNumber, ownerEmail, policyName, reason = CONST.REPORT.ARCHIVE_REASON.DEFAULT) {