-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Refactor Edit and Delete Report Comment #9532
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
Changes from all commits
827e0cf
47f54ac
82309d7
5cd5c15
3246ff7
8f23138
30991b5
483b78b
0da2900
f8bf959
e4d3b9f
4c8f633
8de9a89
63219b8
52d6960
39c06b6
f42ae84
554e247
818be57
769d43e
1ee29e6
53ae16a
2c808ce
62401ee
afc8fc6
c12fcd1
1888216
22f5e1b
27c5bc9
3b3d43d
6afbf4a
b633c47
bfc8584
2e70d47
fc0803b
c000554
d57dd14
27361a7
f06eac4
7dc9e3f
bb0f552
4bce6bf
5f713be
ee002be
03115f8
b715aed
bcc2b66
69ba9f2
9a5b5bd
70ef475
545977e
dd41dad
ab9772f
e2bbed7
26d2704
b20e330
c9cc3b8
98c3ed3
90ae734
d0784e4
82c150b
6535d05
68a32c7
f8146dd
70b383f
219dae4
493768a
c335b0a
c498a35
e0cc84b
7c5a71a
b6a179e
d2f86c2
a41fa84
2fb375c
765b0d8
0c3e9ce
91841dc
827ad50
0e0376c
e076529
fb063f0
70f6d27
093bc57
c39972a
542d0e1
2f9de3d
764a569
b5b7fb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,7 +24,6 @@ import * as ReportUtils from '../ReportUtils'; | |
| import * as ReportActions from './ReportActions'; | ||
| import Growl from '../Growl'; | ||
| import * as Localize from '../Localize'; | ||
| import PusherUtils from '../PusherUtils'; | ||
| import DateUtils from '../DateUtils'; | ||
| import * as ReportActionsUtils from '../ReportActionsUtils'; | ||
| import * as NumberUtils from '../NumberUtils'; | ||
|
|
@@ -436,48 +435,6 @@ function getReportChannelName(reportID) { | |
| return `${CONST.PUSHER.PRIVATE_REPORT_CHANNEL_PREFIX}${reportID}${CONFIG.PUSHER.SUFFIX}`; | ||
| } | ||
|
|
||
| /** | ||
| * Initialize our pusher subscriptions to listen for new report comments and pin toggles | ||
| */ | ||
| function subscribeToUserEvents() { | ||
| // If we don't have the user's accountID yet we can't subscribe so return early | ||
| if (!currentUserAccountID) { | ||
| return; | ||
| } | ||
|
|
||
| const pusherChannelName = `${CONST.PUSHER.PRIVATE_USER_CHANNEL_PREFIX}${currentUserAccountID}${CONFIG.PUSHER.SUFFIX}`; | ||
| if (Pusher.isSubscribed(pusherChannelName) || Pusher.isAlreadySubscribing(pusherChannelName)) { | ||
| return; | ||
| } | ||
|
|
||
| // Live-update a report's actions when an 'edit comment' event is received. | ||
| PusherUtils.subscribeToPrivateUserChannelEvent(Pusher.TYPE.REPORT_COMMENT_EDIT, | ||
| currentUserAccountID, | ||
| ({reportID, sequenceNumber, message}) => { | ||
| // We only want the active client to process these events once otherwise multiple tabs would decrement the 'unreadActionCount' | ||
| if (!ActiveClientManager.isClientTheLeader()) { | ||
| return; | ||
| } | ||
|
|
||
| const actionsToMerge = {}; | ||
| actionsToMerge[sequenceNumber] = {message: [message]}; | ||
|
|
||
| // If someone besides the current user deleted an action and the sequenceNumber is greater than our last read we will decrement the unread count | ||
| // we skip this for the current user because we should already have decremented the count optimistically when they deleted the comment. | ||
| const isFromCurrentUser = ReportActions.isFromCurrentUser(reportID, sequenceNumber, currentUserAccountID, actionsToMerge); | ||
| if (!message.html && !isFromCurrentUser && sequenceNumber > getLastReadSequenceNumber(reportID)) { | ||
| Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, { | ||
| unreadActionCount: Math.max(getUnreadActionCount(reportID) - 1, 0), | ||
| }); | ||
| } | ||
|
|
||
| Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, { | ||
| lastMessageText: ReportActions.getLastVisibleMessageText(reportID, actionsToMerge), | ||
| }); | ||
| Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, actionsToMerge); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Setup reportComment push notification callbacks. | ||
| */ | ||
|
|
@@ -880,73 +837,6 @@ function addComment(reportID, text) { | |
| addActions(reportID, text); | ||
| } | ||
|
|
||
| /** | ||
| * Deletes a comment from the report, basically sets it as empty string | ||
| * | ||
| * @param {Number} reportID | ||
| * @param {Object} reportAction | ||
| */ | ||
| function deleteReportComment(reportID, reportAction) { | ||
| // Optimistic Response | ||
| const sequenceNumber = reportAction.sequenceNumber; | ||
| const reportActionsToMerge = {}; | ||
| const oldMessage = {...reportAction.message}; | ||
| reportActionsToMerge[sequenceNumber] = { | ||
| ...reportAction, | ||
| message: [ | ||
| { | ||
| type: CONST.REPORT.MESSAGE.TYPE.COMMENT, | ||
| html: '', | ||
| text: '', | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| // If the comment we are deleting is more recent than our last read comment we will update the unread count | ||
| if (sequenceNumber > getLastReadSequenceNumber(reportID)) { | ||
| Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, { | ||
| unreadActionCount: Math.max(getUnreadActionCount(reportID) - 1, 0), | ||
| }); | ||
| } | ||
|
|
||
| // Optimistically update the report and reportActions | ||
| Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, reportActionsToMerge); | ||
| Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, { | ||
| lastMessageText: ReportActions.getLastVisibleMessageText(reportID, reportActionsToMerge), | ||
| }); | ||
|
|
||
| // Try to delete the comment by calling the API | ||
| DeprecatedAPI.Report_EditComment({ | ||
| reportID, | ||
| reportActionID: reportAction.reportActionID, | ||
| reportComment: '', | ||
| sequenceNumber, | ||
| }) | ||
| .then((response) => { | ||
| if (response.jsonCode === 200) { | ||
| return; | ||
| } | ||
|
|
||
| // Reverse Optimistic Response | ||
| reportActionsToMerge[sequenceNumber] = { | ||
| ...reportAction, | ||
| message: oldMessage, | ||
| }; | ||
|
|
||
| if (sequenceNumber > getLastReadSequenceNumber(reportID)) { | ||
| Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, { | ||
| unreadActionCount: getUnreadActionCount(reportID) + 1, | ||
| }); | ||
| } | ||
|
|
||
| Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, { | ||
| lastMessageText: ReportActions.getLastVisibleMessageText(reportID, reportActionsToMerge), | ||
| }); | ||
|
|
||
| Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, reportActionsToMerge); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the latest page of report actions and updates the last read message | ||
| * | ||
|
|
@@ -1216,6 +1106,88 @@ Onyx.connect({ | |
| callback: handleReportChanged, | ||
| }); | ||
|
|
||
| /** | ||
| * Deletes a comment from the report, basically sets it as empty string | ||
| * | ||
| * @param {Number} reportID | ||
| * @param {Object} reportAction | ||
| */ | ||
| function deleteReportComment(reportID, reportAction) { | ||
| const sequenceNumber = reportAction.sequenceNumber; | ||
| const optimisticReportActions = { | ||
| [sequenceNumber]: { | ||
| pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, | ||
| }, | ||
| }; | ||
|
|
||
| // If we are deleting the last visible message, let's find the previous visible one | ||
| // and update the lastMessageText in the chat preview. | ||
| const optimisticReport = { | ||
| lastMessageText: ReportActions.getLastVisibleMessageText(reportID, { | ||
| [sequenceNumber]: { | ||
| message: [{ | ||
| html: '', | ||
| text: '', | ||
| }], | ||
| }, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe could use a comment here to explain why we need to update the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also I think this should be passing in the modified message.. like: const optimisticReport = {
lastMessageText: ReportActions.getLastVisibleMessageText(reportID, {
[sequenceNumber]: {
...reportAction,
message: [{
html: '',
text: '',
}],
},
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not that sure we need all that anymore except for the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nah, I think we do need. Not sure why though, I thought the API would update lastMessageText for the sender and recipient. Need some more time to understand everything |
||
| }), | ||
| }; | ||
|
|
||
| // If the API call fails we must show the original message again, so we revert the message content back to how it was | ||
| // and and remove the pendingAction so the strike-through clears | ||
| const failureData = [ | ||
| { | ||
| onyxMethod: CONST.ONYX.METHOD.MERGE, | ||
| key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, | ||
| value: { | ||
| [sequenceNumber]: { | ||
| message: reportAction.message, | ||
| pendingAction: null, | ||
| }, | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
| const successData = [ | ||
| { | ||
| onyxMethod: CONST.ONYX.METHOD.MERGE, | ||
| key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, | ||
| value: { | ||
| [sequenceNumber]: { | ||
| pendingAction: null, | ||
| }, | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
| // If we are deleting an unread message that is greater than our last read we decrease the unreadActionCount | ||
| // since the message we are deleting is an unread | ||
| if (sequenceNumber > getLastReadSequenceNumber(reportID)) { | ||
| const unreadActionCount = getUnreadActionCount(reportID); | ||
| optimisticReport.unreadActionCount = Math.max(unreadActionCount - 1, 0); | ||
| } | ||
|
|
||
| const optimisticData = [ | ||
| { | ||
| onyxMethod: CONST.ONYX.METHOD.MERGE, | ||
| key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, | ||
| value: optimisticReportActions, | ||
| }, | ||
| { | ||
| onyxMethod: CONST.ONYX.METHOD.MERGE, | ||
| key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, | ||
| value: optimisticReport, | ||
| }, | ||
| ]; | ||
|
|
||
| const parameters = { | ||
| reportID, | ||
| sequenceNumber, | ||
| reportActionID: reportAction.reportActionID, | ||
| }; | ||
| API.write('DeleteComment', parameters, {optimisticData, successData, failureData}); | ||
| } | ||
|
|
||
| /** | ||
| * Saves a new message for a comment. Marks the comment as edited, which will be reflected in the UI. | ||
| * | ||
|
|
@@ -1241,32 +1213,59 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { | |
| return; | ||
| } | ||
|
|
||
| // Optimistically update the report action with the new message | ||
| // Optimistically update the reportAction with the new message | ||
| const sequenceNumber = originalReportAction.sequenceNumber; | ||
| const newReportAction = {...originalReportAction}; | ||
| const actionToMerge = {}; | ||
| newReportAction.message[0].isEdited = true; | ||
| newReportAction.message[0].html = htmlForNewComment; | ||
| newReportAction.message[0].text = parser.htmlToText(htmlForNewComment); | ||
| actionToMerge[sequenceNumber] = newReportAction; | ||
| Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, actionToMerge); | ||
|
|
||
| // Persist the updated report comment | ||
| DeprecatedAPI.Report_EditComment({ | ||
| const optimisticReportActions = { | ||
| [sequenceNumber]: { | ||
| pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, | ||
| message: [{ | ||
| isEdited: true, | ||
| html: htmlForNewComment, | ||
| text: textForNewComment, | ||
| }], | ||
| }, | ||
| }; | ||
|
|
||
| const optimisticData = [ | ||
| { | ||
| onyxMethod: CONST.ONYX.METHOD.MERGE, | ||
| key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, | ||
| value: optimisticReportActions, | ||
| }, | ||
| ]; | ||
|
|
||
| const failureData = [ | ||
| { | ||
| onyxMethod: CONST.ONYX.METHOD.MERGE, | ||
| key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, | ||
| value: { | ||
| [sequenceNumber]: { | ||
| ...originalReportAction, | ||
| pendingAction: null, | ||
| }, | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
| const successData = [ | ||
| { | ||
| onyxMethod: CONST.ONYX.METHOD.MERGE, | ||
| key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, | ||
| value: { | ||
| [sequenceNumber]: { | ||
| pendingAction: null, | ||
| }, | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
| const parameters = { | ||
| reportID, | ||
| reportActionID: originalReportAction.reportActionID, | ||
| reportComment: htmlForNewComment, | ||
| sequenceNumber, | ||
| }) | ||
| .then((response) => { | ||
| if (response.jsonCode === 200) { | ||
| return; | ||
| } | ||
|
|
||
| // If it fails, reset Onyx | ||
| actionToMerge[sequenceNumber] = originalReportAction; | ||
| Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, actionToMerge); | ||
| }); | ||
| reportComment: htmlForNewComment, | ||
| reportActionID: originalReportAction.reportActionID, | ||
| }; | ||
| API.write('UpdateComment', parameters, {optimisticData, successData, failureData}); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1582,7 +1581,6 @@ export { | |
| updateNotificationPreference, | ||
| setNewMarkerPosition, | ||
| subscribeToReportTypingEvents, | ||
| subscribeToUserEvents, | ||
| subscribeToReportCommentPushNotifications, | ||
| unsubscribeFromReportChannel, | ||
| saveReportComment, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.