From 827e0cf055a0d645deceff6d23161b6704f063a2 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Wed, 22 Jun 2022 11:54:29 -0300 Subject: [PATCH 01/46] Add new API call to edit and delete comment endpoints --- src/libs/Pusher/EventType.js | 1 - src/libs/actions/Report.js | 136 ++++++++++++++++++++++------------- src/libs/deprecatedAPI.js | 13 ---- 3 files changed, 85 insertions(+), 65 deletions(-) diff --git a/src/libs/Pusher/EventType.js b/src/libs/Pusher/EventType.js index 97fa8cc9246b..79e7fb0c5137 100644 --- a/src/libs/Pusher/EventType.js +++ b/src/libs/Pusher/EventType.js @@ -4,7 +4,6 @@ */ export default { REPORT_COMMENT: 'reportComment', - REPORT_COMMENT_EDIT: 'reportCommentEdit', PREFERRED_LOCALE: 'preferredLocale', EXPENSIFY_CARD_UPDATE: 'expensifyCardUpdate', SCREEN_SHARE_REQUEST: 'screenshareRequest', diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index f529931b5c80..81578081887e 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -519,20 +519,21 @@ function setNewMarkerPosition(reportID, sequenceNumber) { * @param {Number} sequenceNumber * @param {Object} message */ -function updateReportActionMessage(reportID, sequenceNumber, message) { - const actionToMerge = {}; - actionToMerge[sequenceNumber] = {message: [message]}; - Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, actionToMerge).then(() => { - // If the message is deleted, update the last read message and the unread counter - if (!message.html) { - setLocalLastRead(reportID, lastReadSequenceNumbers[reportID]); - } - - Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, { - lastMessageText: ReportActions.getLastVisibleMessageText(reportID), - }); - }); -} +// function updateReportActionMessage(reportID, sequenceNumber, message) { +// +// const actionToMerge = {}; +// actionToMerge[sequenceNumber] = {message: [message]}; +// Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, actionToMerge).then(() => { +// // If the message is deleted, update the last read message and the unread counter +// if (!message.html) { +// setLocalLastRead(reportID, lastReadSequenceNumbers[reportID]); +// } + +// Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, { +// lastMessageText: ReportActions.getLastVisibleMessageText(reportID), +// }); +// }); +// } /** * Updates a report in the store with a new report action @@ -614,9 +615,9 @@ function subscribeToUserEvents() { ); // Live-update a report's actions when an 'edit comment' event is received. - PusherUtils.subscribeToPrivateUserChannelEvent(Pusher.TYPE.REPORT_COMMENT_EDIT, - currentUserAccountID, - pushJSON => updateReportActionMessage(pushJSON.reportID, pushJSON.sequenceNumber, pushJSON.message)); + // PusherUtils.subscribeToPrivateUserChannelEvent(Pusher.TYPE.REPORT_COMMENT_EDIT, + // currentUserAccountID, + // pushJSON => updateReportActionMessage(pushJSON.reportID, pushJSON.sequenceNumber, pushJSON.message)); } /** @@ -1032,7 +1033,7 @@ function deleteReportComment(reportID, reportAction) { // Optimistic Response const sequenceNumber = reportAction.sequenceNumber; const reportActionsToMerge = {}; - const oldMessage = {...reportAction.message}; + // const oldMessage = {...reportAction.message}; reportActionsToMerge[sequenceNumber] = { ...reportAction, message: [ @@ -1044,31 +1045,48 @@ function deleteReportComment(reportID, reportAction) { ], }; - Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, reportActionsToMerge).then(() => { - setLocalLastRead(reportID, getLastReadSequenceNumber(reportID)); - }); + // Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, reportActionsToMerge).then(() => { + // setLocalLastRead(reportID, getLastReadSequenceNumber(reportID)); + // }); - // Try to delete the comment by calling the API - DeprecatedAPI.Report_EditComment({ + // Optimistically update the report action with the new message + const optimisticData = [ + { + onyxMethod: 'merge', + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, + value: {reportComment: htmlForNewComment, sequenceNumber}, + }, + ]; + + // Persist the updated report comment + API.write('UpdateReportComment', { reportID, - reportActionID: reportAction.reportActionID, + reportActionID: originalReportAction.reportActionID, reportComment: '', - sequenceNumber, - }) - .then((response) => { - if (response.jsonCode === 200) { - return; - } + sequenceNumber + }, {optimisticData}); - // Reverse Optimistic Response - reportActionsToMerge[sequenceNumber] = { - ...reportAction, - message: oldMessage, - }; - Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, reportActionsToMerge).then(() => { - setLocalLastRead(reportID, getLastReadSequenceNumber(reportID)); - }); - }); + // 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, + // }; + // Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, reportActionsToMerge).then(() => { + // setLocalLastRead(reportID, getLastReadSequenceNumber(reportID)); + // }); + // }); } /** @@ -1234,7 +1252,6 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { return; } - // Optimistically update the report action with the new message const sequenceNumber = originalReportAction.sequenceNumber; const newReportAction = {...originalReportAction}; const actionToMerge = {}; @@ -1244,22 +1261,38 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { actionToMerge[sequenceNumber] = newReportAction; Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, actionToMerge); + // Optimistically update the report action with the new message + const optimisticData = [ + { + onyxMethod: 'merge', + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, + value: {reportComment: htmlForNewComment, sequenceNumber}, + }, + ]; + // Persist the updated report comment - DeprecatedAPI.Report_EditComment({ + API.write('UpdateReportComment', { reportID, reportActionID: originalReportAction.reportActionID, reportComment: htmlForNewComment, - sequenceNumber, - }) - .then((response) => { - if (response.jsonCode === 200) { - return; - } + sequenceNumber + }, {optimisticData}); - // If it fails, reset Onyx - actionToMerge[sequenceNumber] = originalReportAction; - Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, actionToMerge); - }); + // DeprecatedAPI.Report_EditComment({ + // 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); + // }); } /** @@ -1493,6 +1526,7 @@ Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, initWithStoredValues: false, callback: (actions, key) => { + // reportID can be derived from the Onyx key const reportID = parseInt(key.split('_')[1], 10); if (!reportID) { diff --git a/src/libs/deprecatedAPI.js b/src/libs/deprecatedAPI.js index 67333741f0a7..db6bf38e870a 100644 --- a/src/libs/deprecatedAPI.js +++ b/src/libs/deprecatedAPI.js @@ -343,19 +343,6 @@ function Report_TogglePinned(parameters) { return Network.post(commandName, parameters); } -/** - * @param {Object} parameters - * @param {Number} parameters.reportID - * @param {Number} parameters.reportActionID - * @param {String} parameters.reportComment - * @returns {Promise} - */ -function Report_EditComment(parameters) { - const commandName = 'Report_EditComment'; - requireParameters(['reportID', 'reportActionID', 'reportComment'], parameters, commandName); - return Network.post(commandName, parameters); -} - /** * @param {Object} parameters * @param {Number} parameters.reportID From 82309d7245a1eb6a59e45a2208034f2e276d8a38 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Thu, 23 Jun 2022 19:33:48 -0300 Subject: [PATCH 02/46] Use optimisc data on comment edit and delete methods --- src/libs/actions/Report.js | 159 ++++++++++++++++++++----------------- src/libs/deprecatedAPI.js | 13 +++ 2 files changed, 100 insertions(+), 72 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 81578081887e..c71764fc1d46 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -520,7 +520,7 @@ function setNewMarkerPosition(reportID, sequenceNumber) { * @param {Object} message */ // function updateReportActionMessage(reportID, sequenceNumber, message) { -// +// // const actionToMerge = {}; // actionToMerge[sequenceNumber] = {message: [message]}; // Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, actionToMerge).then(() => { @@ -1023,72 +1023,6 @@ function getLastReadSequenceNumber(reportID) { return lastReadSequenceNumbers[reportID]; } -/** - * 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: '', - }, - ], - }; - - // Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, reportActionsToMerge).then(() => { - // setLocalLastRead(reportID, getLastReadSequenceNumber(reportID)); - // }); - - // Optimistically update the report action with the new message - const optimisticData = [ - { - onyxMethod: 'merge', - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, - value: {reportComment: htmlForNewComment, sequenceNumber}, - }, - ]; - - // Persist the updated report comment - API.write('UpdateReportComment', { - reportID, - reportActionID: originalReportAction.reportActionID, - reportComment: '', - sequenceNumber - }, {optimisticData}); - - // 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, - // }; - // Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, reportActionsToMerge).then(() => { - // setLocalLastRead(reportID, getLastReadSequenceNumber(reportID)); - // }); - // }); -} - /** * Updates the last read action ID on the report. It optimistically makes the change to the store, and then let's the * network layer handle the delayed write. @@ -1230,6 +1164,83 @@ Onyx.connect({ callback: handleReportChanged, }); +// Onyx.connect({ +// key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, +// callback: (report) => { +// console.log(report); +// }, +// }); + +/** + * 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: '', + }, + ], + }; + + // Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, reportActionsToMerge).then(() => { + // setLocalLastRead(reportID, getLastReadSequenceNumber(reportID)); + // }); + + // Optimistically update the report action with the new message + const optimisticData = [ + { + onyxMethod: 'merge', + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, + value: { + reportID, + reportActionID: reportAction.reportActionID, + sequenceNumber, + }, + }, + ]; + + // Persist the updated report comment + API.write('DeleteComment', { + reportID, + reportActionID: reportAction.reportActionID, + sequenceNumber, + }, {optimisticData}); + + // 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, + // }; + // Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, reportActionsToMerge).then(() => { + // setLocalLastRead(reportID, getLastReadSequenceNumber(reportID)); + // }); + // }); +} + /** * Saves a new message for a comment. Marks the comment as edited, which will be reflected in the UI. * @@ -1266,16 +1277,21 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { { onyxMethod: 'merge', key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, - value: {reportComment: htmlForNewComment, sequenceNumber}, + value: { + reportID, + sequenceNumber, + reportActionID, + message: htmlForNewComment, + }, }, ]; // Persist the updated report comment - API.write('UpdateReportComment', { + API.write('UpdateComment', { reportID, - reportActionID: originalReportAction.reportActionID, + sequenceNumber, reportComment: htmlForNewComment, - sequenceNumber + reportActionID: originalReportAction.reportActionID, }, {optimisticData}); // DeprecatedAPI.Report_EditComment({ @@ -1526,7 +1542,6 @@ Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, initWithStoredValues: false, callback: (actions, key) => { - // reportID can be derived from the Onyx key const reportID = parseInt(key.split('_')[1], 10); if (!reportID) { diff --git a/src/libs/deprecatedAPI.js b/src/libs/deprecatedAPI.js index db6bf38e870a..67333741f0a7 100644 --- a/src/libs/deprecatedAPI.js +++ b/src/libs/deprecatedAPI.js @@ -343,6 +343,19 @@ function Report_TogglePinned(parameters) { return Network.post(commandName, parameters); } +/** + * @param {Object} parameters + * @param {Number} parameters.reportID + * @param {Number} parameters.reportActionID + * @param {String} parameters.reportComment + * @returns {Promise} + */ +function Report_EditComment(parameters) { + const commandName = 'Report_EditComment'; + requireParameters(['reportID', 'reportActionID', 'reportComment'], parameters, commandName); + return Network.post(commandName, parameters); +} + /** * @param {Object} parameters * @param {Number} parameters.reportID From 3246ff7ee217d1b58e500bd2d8908d3de45179d9 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Mon, 27 Jun 2022 16:27:34 -0300 Subject: [PATCH 03/46] Fix reportActionID error for deleteComment --- src/libs/actions/Report.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 49e269af2aed..074215d32511 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1281,7 +1281,7 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { value: { reportID, sequenceNumber, - reportActionID, + reportActionID: originalReportAction.reportActionID, message: htmlForNewComment, }, }, From 8f231389567600dcf0430629b53fda62469aea9f Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Tue, 28 Jun 2022 17:17:05 -0300 Subject: [PATCH 04/46] Send reportAction optimistic data on edit --- src/libs/actions/Report.js | 42 ++++++------------------------- src/libs/actions/ReportActions.js | 23 +++++++++++++++++ 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 074215d32511..c682de40864c 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -510,29 +510,6 @@ function setNewMarkerPosition(reportID, sequenceNumber) { }); } -/** - * Updates a report action's message to be a new value. - * - * @param {Number} reportID - * @param {Number} sequenceNumber - * @param {Object} message - */ -// function updateReportActionMessage(reportID, sequenceNumber, message) { -// -// const actionToMerge = {}; -// actionToMerge[sequenceNumber] = {message: [message]}; -// Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, actionToMerge).then(() => { -// // If the message is deleted, update the last read message and the unread counter -// if (!message.html) { -// setLocalLastRead(reportID, lastReadSequenceNumbers[reportID]); -// } - -// Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, { -// lastMessageText: ReportActions.getLastVisibleMessageText(reportID), -// }); -// }); -// } - /** * Updates a report in the store with a new report action * @@ -1265,13 +1242,6 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { } 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); // Optimistically update the report action with the new message const optimisticData = [ @@ -1279,10 +1249,14 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { onyxMethod: 'merge', key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, value: { - reportID, - sequenceNumber, - reportActionID: originalReportAction.reportActionID, - message: htmlForNewComment, + [sequenceNumber]: { + isPending: true, + message: [{ + isEdited: true, + html: htmlForNewComment, + text: parser.htmlToText(htmlForNewComment) + }] + } }, }, ]; diff --git a/src/libs/actions/ReportActions.js b/src/libs/actions/ReportActions.js index d613b4045e56..751be72fedac 100644 --- a/src/libs/actions/ReportActions.js +++ b/src/libs/actions/ReportActions.js @@ -114,9 +114,32 @@ function getLastVisibleMessageText(reportID) { return ReportUtils.formatReportLastMessageText(messageText); } +/** + * Updates a report action's message to be a new value. + * + * @param {Number} reportID + * @param {Number} sequenceNumber + * @param {Object} message + */ +function updateReportActionMessage(reportID, sequenceNumber, message) { + const actionToMerge = {}; + actionToMerge[sequenceNumber] = {message: [message]}; + Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, actionToMerge).then(() => { + // If the message is deleted, update the last read message and the unread counter + // if (!message.html) { + // setLocalLastRead(reportID, lastReadSequenceNumbers[reportID]); + // } + + Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, { + lastMessageText: getLastVisibleMessageText(reportID), + }); + }); +} + export { isReportMissingActions, dangerouslyGetReportActionsMaxSequenceNumber, getDeletedCommentsCount, getLastVisibleMessageText, + updateReportActionMessage, }; From 30991b526961924b4181c495fd4c5ae19b428637 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Tue, 28 Jun 2022 17:27:30 -0300 Subject: [PATCH 05/46] Update report last message if user edits it --- src/libs/actions/Report.js | 56 +++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index c682de40864c..9f16d4577442 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1228,46 +1228,64 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { // Do not autolink if someone explicitly tries to remove a link from message. // https://github.com/Expensify/App/issues/9090 - const htmlForNewComment = parser.replace(textForNewComment, {filterRules: _.filter(_.pluck(parser.rules, 'name'), name => name !== 'autolink')}); + const htmlForEditedComment = parser.replace(textForNewComment, {filterRules: _.filter(_.pluck(parser.rules, 'name'), name => name !== 'autolink')}); + const textForEditedComment = parser.htmlToText(htmlForEditedComment); // Delete the comment if it's empty - if (_.isEmpty(htmlForNewComment)) { + if (_.isEmpty(htmlForEditedComment)) { deleteReportComment(reportID, originalReportAction); return; } // Skip the Edit if message is not changed - if (originalReportAction.message[0].html === htmlForNewComment.trim()) { + if (originalReportAction.message[0].html === htmlForEditedComment.trim()) { return; } const sequenceNumber = originalReportAction.sequenceNumber; + const highestSequenceNumber = reportMaxSequenceNumbers[reportID] || 0; // Optimistically update the report action with the new message + const optimisticReportActions = { + [sequenceNumber]: { + isPending: true, + message: [{ + isEdited: true, + html: htmlForEditedComment, + text: textForEditedComment, + }] + } + }; + + // Optimistically update the report itself if the user edited the lastest message + const optimisticReport = {}; + if (sequenceNumber === highestSequenceNumber) { + optimisticReport.lastMessageText = ReportUtils.formatReportLastMessageText(textForEditedComment); + }; + + // Group all Optmistic data changes const optimisticData = [ { - onyxMethod: 'merge', + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, + value: optimisticReport, + }, + { + onyxMethod: CONST.ONYX.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, - value: { - [sequenceNumber]: { - isPending: true, - message: [{ - isEdited: true, - html: htmlForNewComment, - text: parser.htmlToText(htmlForNewComment) - }] - } - }, + value: optimisticReportActions, }, ]; - // Persist the updated report comment - API.write('UpdateComment', { + const parameters = { reportID, sequenceNumber, - reportComment: htmlForNewComment, - reportActionID: originalReportAction.reportActionID, - }, {optimisticData}); + reportComment: htmlForEditedComment, + reportActionID: originalReportAction.reportActionID + }; + + // Persist the updated report comment + API.write('UpdateComment', parameters, {optimisticData}); // DeprecatedAPI.Report_EditComment({ // reportID, From 483b78b9f1dc7d4673b92aa5fbbc7bfea89ca54f Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Tue, 28 Jun 2022 17:28:45 -0300 Subject: [PATCH 06/46] Lint and add missing commas and semi-colons --- src/libs/actions/Report.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 9f16d4577442..ca6ac28472d6 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1253,15 +1253,15 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { isEdited: true, html: htmlForEditedComment, text: textForEditedComment, - }] - } + }], + }, }; // Optimistically update the report itself if the user edited the lastest message const optimisticReport = {}; if (sequenceNumber === highestSequenceNumber) { optimisticReport.lastMessageText = ReportUtils.formatReportLastMessageText(textForEditedComment); - }; + } // Group all Optmistic data changes const optimisticData = [ @@ -1281,7 +1281,7 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { reportID, sequenceNumber, reportComment: htmlForEditedComment, - reportActionID: originalReportAction.reportActionID + reportActionID: originalReportAction.reportActionID, }; // Persist the updated report comment From 0da29002555edc4d940c0e7665dc27a6e1116241 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Tue, 28 Jun 2022 17:31:39 -0300 Subject: [PATCH 07/46] Add ONYX MERGE to const file --- src/CONST.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/CONST.js b/src/CONST.js index 4e209315779d..a903c0b64539 100755 --- a/src/CONST.js +++ b/src/CONST.js @@ -715,6 +715,12 @@ const CONST = { // There's a limit of 60k characters in Auth - https://github.com/Expensify/Auth/blob/198d59547f71fdee8121325e8bc9241fc9c3236a/auth/lib/Request.h#L28 MAX_COMMENT_LENGTH: 60000, + + ONYX: { + METHOD: { + MERGE: 'merge', + } + } }; export default CONST; From 63219b8f9db62a1bdf73fae99c2cf53423ed0167 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Thu, 14 Jul 2022 18:01:01 -0300 Subject: [PATCH 08/46] Refactor update and delete with main changes --- src/libs/actions/Report.js | 197 ++++++++++--------------------------- 1 file changed, 51 insertions(+), 146 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 0a62bbb27892..4698b20f9487 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1040,73 +1040,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 * @@ -1283,13 +1216,6 @@ Onyx.connect({ callback: handleReportChanged, }); -// Onyx.connect({ -// key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, -// callback: (report) => { -// console.log(report); -// }, -// }); - /** * Deletes a comment from the report, basically sets it as empty string * @@ -1297,67 +1223,65 @@ Onyx.connect({ * @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, + const oldMessage = {...reportAction.message}; + + const optimisticReportActions = { + [sequenceNumber]: { + isPending: true, + message: [{ html: '', text: '', - }, - ], + }], + }, + }; + const optimisticReport = { + lastMessageText: ReportActions.getLastVisibleMessageText(reportID, reportActionsToMerge), }; - // Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, reportActionsToMerge).then(() => { - // setLocalLastRead(reportID, getLastReadSequenceNumber(reportID)); - // }); + // If the deleted comment is more recent than our last read comment, update the unread count + if (sequenceNumber > getLastReadSequenceNumber(reportID)) { + optimisticReport.unreadActionCount = Math.max(getUnreadActionCount(reportID) - 1, 0); + } - // Optimistically update the report action with the new message + // List of optmistic data to change const optimisticData = [ { - onyxMethod: 'merge', + onyxMethod: CONST.ONYX.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, - value: { - reportID, - reportActionID: reportAction.reportActionID, - sequenceNumber, - }, + value: optimisticReportActions, + }, + { + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, + value: optimisticReport, }, ]; - // Persist the updated report comment - API.write('DeleteComment', { + const parameters = { reportID, - reportActionID: reportAction.reportActionID, sequenceNumber, - }, {optimisticData}); + reportActionID: originalReportAction.reportActionID, + }; + API.write('DeleteComment', parameters, {optimisticData}); - // 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, - // }; - // Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, reportActionsToMerge).then(() => { - // setLocalLastRead(reportID, getLastReadSequenceNumber(reportID)); - // }); + // If all fails 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); } /** @@ -1386,10 +1310,8 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { return; } + // Optimistically update the reportAction with the new message const sequenceNumber = originalReportAction.sequenceNumber; - const highestSequenceNumber = reportMaxSequenceNumbers[reportID] || 0; - - // Optimistically update the report action with the new message const optimisticReportActions = { [sequenceNumber]: { isPending: true, @@ -1401,23 +1323,24 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { }, }; - // Optimistically update the report itself if the user edited the lastest message + // Optimistically update the report's last message if the user edited it + const highestSequenceNumber = getMaxSequenceNumber(reportID); const optimisticReport = {}; if (sequenceNumber === highestSequenceNumber) { optimisticReport.lastMessageText = ReportUtils.formatReportLastMessageText(textForEditedComment); } - // Group all Optmistic data changes + // List of optmistic data to change const optimisticData = [ { onyxMethod: CONST.ONYX.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, - value: optimisticReport, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, + value: optimisticReportActions, }, { onyxMethod: CONST.ONYX.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, - value: optimisticReportActions, + key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, + value: optimisticReport, }, ]; @@ -1427,25 +1350,7 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { reportComment: htmlForEditedComment, reportActionID: originalReportAction.reportActionID, }; - - // Persist the updated report comment API.write('UpdateComment', parameters, {optimisticData}); - - // DeprecatedAPI.Report_EditComment({ - // 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); - // }); } /** From 39c06b6c26db109cbea6c9046098d0470e87b6cd Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Fri, 15 Jul 2022 16:03:17 -0300 Subject: [PATCH 09/46] Fix optimistic report data last message --- src/libs/actions/Report.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 4698b20f9487..192daa7a8f13 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1224,7 +1224,6 @@ Onyx.connect({ */ function deleteReportComment(reportID, reportAction) { const sequenceNumber = reportAction.sequenceNumber; - const oldMessage = {...reportAction.message}; const optimisticReportActions = { [sequenceNumber]: { @@ -1235,13 +1234,18 @@ function deleteReportComment(reportID, reportAction) { }], }, }; + const optimisticReport = { - lastMessageText: ReportActions.getLastVisibleMessageText(reportID, reportActionsToMerge), + lastMessageText: ReportActions.getLastVisibleMessageTextz(reportID, { + [sequenceNumber]: { + ...reportAction, + }, + }), }; // If the deleted comment is more recent than our last read comment, update the unread count if (sequenceNumber > getLastReadSequenceNumber(reportID)) { - optimisticReport.unreadActionCount = Math.max(getUnreadActionCount(reportID) - 1, 0); + optimisticReport.unreadActionCount = Math.max(getUnreadActionCount(reportID) - 1, 0); } // List of optmistic data to change @@ -1261,7 +1265,7 @@ function deleteReportComment(reportID, reportAction) { const parameters = { reportID, sequenceNumber, - reportActionID: originalReportAction.reportActionID, + reportActionID: reportAction.reportActionID, }; API.write('DeleteComment', parameters, {optimisticData}); From 62401ee939a632361dac1c2d88f9b648ad8db979 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Tue, 9 Aug 2022 19:37:44 -0300 Subject: [PATCH 10/46] Display offline pending actions to update/delete comment --- src/libs/actions/Report.js | 4 ++-- src/pages/home/report/ReportActionsList.js | 25 ++++++++++++++-------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index ee7c03105e25..375967ae817f 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1208,7 +1208,7 @@ function deleteReportComment(reportID, reportAction) { const optimisticReportActions = { [sequenceNumber]: { - isPending: true, + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, message: [{ html: '', text: '', @@ -1299,7 +1299,7 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { const sequenceNumber = originalReportAction.sequenceNumber; const optimisticReportActions = { [sequenceNumber]: { - isPending: true, + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, message: [{ isEdited: true, html: htmlForEditedComment, diff --git a/src/pages/home/report/ReportActionsList.js b/src/pages/home/report/ReportActionsList.js index 5ef4b3603808..169c935c97a8 100644 --- a/src/pages/home/report/ReportActionsList.js +++ b/src/pages/home/report/ReportActionsList.js @@ -17,6 +17,7 @@ import * as ReportActionsUtils from '../../../libs/ReportActionsUtils'; import reportActionPropTypes from './reportActionPropTypes'; import CONST from '../../../CONST'; import * as StyleUtils from '../../../styles/StyleUtils'; +import OfflineWithFeedback from '../../../components/OfflineWithFeedback'; const propTypes = { /** Personal details of all the users */ @@ -137,15 +138,21 @@ class ReportActionsList extends React.Component { const shouldDisplayNewIndicator = this.props.report.newMarkerSequenceNumber > 0 && item.action.sequenceNumber === this.props.report.newMarkerSequenceNumber; return ( - + {}} + pendingAction={item.action.pendingAction} + errors={item.action.errors} + > + + ); } From 188821671abd4039dfd25f60cfdc9b4b7dfdeed0 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Wed, 10 Aug 2022 16:52:25 -0300 Subject: [PATCH 11/46] Remove OfflineWithFeedback from ReportActionList --- src/pages/home/report/ReportActionsList.js | 24 ++++++++-------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/pages/home/report/ReportActionsList.js b/src/pages/home/report/ReportActionsList.js index 169c935c97a8..e202a283f631 100644 --- a/src/pages/home/report/ReportActionsList.js +++ b/src/pages/home/report/ReportActionsList.js @@ -138,21 +138,15 @@ class ReportActionsList extends React.Component { const shouldDisplayNewIndicator = this.props.report.newMarkerSequenceNumber > 0 && item.action.sequenceNumber === this.props.report.newMarkerSequenceNumber; return ( - {}} - pendingAction={item.action.pendingAction} - errors={item.action.errors} - > - - + ); } From 22f5e1bbc755b0ef2ad1daec332ce7612ff26e08 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Wed, 10 Aug 2022 17:50:47 -0300 Subject: [PATCH 12/46] Remove unused reportID from reportActions method --- src/libs/actions/Report.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index cbca453cac80..d2ca9b526a6e 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -733,12 +733,11 @@ function createOptimisticReport(participantList) { } /** - * @param {Number} reportID * @param {String} [text] * @param {File} [file] * @returns {Object} */ -function buildOptimisticReportAction(reportID, text, file) { +function buildOptimisticReportAction(text, file) { // For comments shorter than 10k chars, convert the comment from MD into HTML because that's how it is stored in the database // For longer comments, skip parsing and display plaintext for performance reasons. It takes over 40s to parse a 100k long string!! const parser = new ExpensiMark(); @@ -816,7 +815,7 @@ function addActions(reportID, text = '', file) { let commandName = 'AddComment'; if (text) { - const reportComment = buildOptimisticReportAction(reportID, text); + const reportComment = buildOptimisticReportAction(text); reportCommentAction = reportComment.reportAction; reportCommentText = reportComment.commentText; } @@ -825,7 +824,7 @@ function addActions(reportID, text = '', file) { // When we are adding an attachment we will call AddAttachment. // It supports sending an attachment with an optional comment and AddComment supports adding a single text comment only. commandName = 'AddAttachment'; - const attachment = buildOptimisticReportAction(reportID, '', file); + const attachment = buildOptimisticReportAction('', file); attachmentAction = attachment.reportAction; } From 3b3d43d152371cc96d94eb99d573d82d53c1311f Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Thu, 11 Aug 2022 14:46:22 -0300 Subject: [PATCH 13/46] Update edit message with success and fail data --- src/libs/actions/Report.js | 29 +++++++++++++++++++++- src/pages/home/report/ReportActionsList.js | 1 - 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index d2ca9b526a6e..acfa62b83ea4 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1265,13 +1265,40 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { }, ]; + // On Success clear the pendingAction state + const successData = [ + { + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, + value: { + [sequenceNumber]: { + pendingAction: null, + }, + }, + }, + ]; + + // On Error clear the pendingAction state and revert the message + const failureData = [ + { + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, + value: { + [sequenceNumber]: { + ...originalReportAction, + pendingAction: null, + }, + }, + }, + ]; + const parameters = { reportID, sequenceNumber, reportComment: htmlForEditedComment, reportActionID: originalReportAction.reportActionID, }; - API.write('UpdateComment', parameters, {optimisticData}); + API.write('UpdateComment', parameters, {optimisticData, successData, failureData}); } /** diff --git a/src/pages/home/report/ReportActionsList.js b/src/pages/home/report/ReportActionsList.js index e202a283f631..5ef4b3603808 100644 --- a/src/pages/home/report/ReportActionsList.js +++ b/src/pages/home/report/ReportActionsList.js @@ -17,7 +17,6 @@ import * as ReportActionsUtils from '../../../libs/ReportActionsUtils'; import reportActionPropTypes from './reportActionPropTypes'; import CONST from '../../../CONST'; import * as StyleUtils from '../../../styles/StyleUtils'; -import OfflineWithFeedback from '../../../components/OfflineWithFeedback'; const propTypes = { /** Personal details of all the users */ From 6afbf4a3338e31e3336f832b6462ade0c94e7969 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Thu, 11 Aug 2022 15:50:54 -0300 Subject: [PATCH 14/46] Send clientID on message update --- src/libs/actions/Report.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index acfa62b83ea4..c961168925ae 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1151,9 +1151,8 @@ function deleteReportComment(reportID, reportAction) { }], }, }; - const optimisticReport = { - lastMessageText: ReportActions.getLastVisibleMessageTextz(reportID, { + lastMessageText: ReportActions.getLastVisibleMessageText(reportID, { [sequenceNumber]: { ...reportAction, }, @@ -1178,6 +1177,19 @@ function deleteReportComment(reportID, reportAction) { value: optimisticReport, }, ]; + // On Error clear the pendingAction state and revert the message + // const failureData = [ + // { + // onyxMethod: CONST.ONYX.METHOD.MERGE, + // key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, + // value: { + // [sequenceNumber]: { + // ...originalReportAction, + // pendingAction: null, + // }, + // }, + // }, + // ]; const parameters = { reportID, @@ -1295,6 +1307,7 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { const parameters = { reportID, sequenceNumber, + clientID: originalReportAction.clientID, reportComment: htmlForEditedComment, reportActionID: originalReportAction.reportActionID, }; From bfc8584e855e0e51fe7a6704a155b8d11f6ed686 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Thu, 11 Aug 2022 16:03:48 -0300 Subject: [PATCH 15/46] Remove deprecatedAPI editComment method --- src/libs/actions/Report.js | 1 + src/libs/deprecatedAPI.js | 14 -------------- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index c961168925ae..95388a8ce402 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1177,6 +1177,7 @@ function deleteReportComment(reportID, reportAction) { value: optimisticReport, }, ]; + // On Error clear the pendingAction state and revert the message // const failureData = [ // { diff --git a/src/libs/deprecatedAPI.js b/src/libs/deprecatedAPI.js index eff8088a6744..aedbbfaa61da 100644 --- a/src/libs/deprecatedAPI.js +++ b/src/libs/deprecatedAPI.js @@ -260,19 +260,6 @@ function Report_GetHistory(parameters) { return Network.post(commandName, parameters); } -/** - * @param {Object} parameters - * @param {Number} parameters.reportID - * @param {Number} parameters.reportActionID - * @param {String} parameters.reportComment - * @returns {Promise} - */ -function Report_EditComment(parameters) { - const commandName = 'Report_EditComment'; - requireParameters(['reportID', 'reportActionID', 'reportComment'], parameters, commandName); - return Network.post(commandName, parameters); -} - /** * @param {Object} parameters * @param {String} parameters.email @@ -687,7 +674,6 @@ export { Policy_Employees_Merge, RejectTransaction, Report_GetHistory, - Report_EditComment, ResendValidateCode, SetNameValuePair, SetPassword, From 2e70d477a70790aa32a9481c08f48f8e543271bb Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Thu, 11 Aug 2022 17:21:56 -0300 Subject: [PATCH 16/46] Send clientID on message delete --- src/libs/actions/Report.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 95388a8ce402..5ae342e14e46 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1194,6 +1194,7 @@ function deleteReportComment(reportID, reportAction) { const parameters = { reportID, + clientID: reportAction.clientID, sequenceNumber, reportActionID: reportAction.reportActionID, }; From c0005545fae6d3699843c08508c3194a7b430ff1 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Fri, 12 Aug 2022 11:33:34 -0300 Subject: [PATCH 17/46] Keep message content on deletion until success --- src/libs/actions/Report.js | 49 +++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 5ae342e14e46..d3852a3591f3 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1145,10 +1145,6 @@ function deleteReportComment(reportID, reportAction) { const optimisticReportActions = { [sequenceNumber]: { pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, - message: [{ - html: '', - text: '', - }], }, }; const optimisticReport = { @@ -1178,19 +1174,38 @@ function deleteReportComment(reportID, reportAction) { }, ]; + // On Success remove the message content and pendingAction state + const successData = { + [sequenceNumber]: { + pendingAction: null, + message: [{ + html: '', + text: '', + }], + }, + }; + // On Error clear the pendingAction state and revert the message - // const failureData = [ - // { - // onyxMethod: CONST.ONYX.METHOD.MERGE, - // key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, - // value: { - // [sequenceNumber]: { - // ...originalReportAction, - // pendingAction: null, - // }, - // }, - // }, - // ]; + const failureData = [ + { + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, + value: { + [sequenceNumber]: { + message: reportAction.message, + pendingAction: null, + }, + }, + }, + // { + // onyxMethod: CONST.ONYX.METHOD.MERGE, + // key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, + // value: { + // lastMessageText: + // unreadActionCount: + // } + // } + ]; const parameters = { reportID, @@ -1198,7 +1213,7 @@ function deleteReportComment(reportID, reportAction) { sequenceNumber, reportActionID: reportAction.reportActionID, }; - API.write('DeleteComment', parameters, {optimisticData}); + API.write('DeleteComment', parameters, {optimisticData, successData, failureData}); // If all fails Reverse Optimistic Response: // reportActionsToMerge[sequenceNumber] = { From d57dd1478c16c87cc003f986f3714d811511bc28 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Fri, 12 Aug 2022 13:49:03 -0300 Subject: [PATCH 18/46] Add strike-trough to message on delete while offline --- src/libs/actions/Report.js | 1 + src/pages/home/report/ReportActionItemMessage.js | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index d3852a3591f3..04f92dc9ed4b 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1197,6 +1197,7 @@ function deleteReportComment(reportID, reportAction) { }, }, }, + // { // onyxMethod: CONST.ONYX.METHOD.MERGE, // key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, diff --git a/src/pages/home/report/ReportActionItemMessage.js b/src/pages/home/report/ReportActionItemMessage.js index 18d4b2306807..5acdb40ea53b 100644 --- a/src/pages/home/report/ReportActionItemMessage.js +++ b/src/pages/home/report/ReportActionItemMessage.js @@ -18,15 +18,25 @@ const propTypes = { /** Information about the network */ network: networkPropTypes.isRequired, + /** Additional styles to add after local styles. */ + style: PropTypes.oneOfType([ + PropTypes.arrayOf(PropTypes.object), + PropTypes.object, + ]), + /** localization props */ ...withLocalizePropTypes, }; +const defaultProps = { + style: [], +}; + const ReportActionItemMessage = (props) => { const isUnsent = props.network.isOffline && props.action.isLoading; return ( - + {_.map(_.compact(props.action.message), (fragment, index) => ( { }; ReportActionItemMessage.propTypes = propTypes; +ReportActionItemMessage.defaultProps = defaultProps; ReportActionItemMessage.displayName = 'ReportActionItemMessage'; export default compose( From bb0f552728eb5512bb1677caf49a457c97003609 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Tue, 16 Aug 2022 15:32:10 -0300 Subject: [PATCH 19/46] Clear sequenceNumber errors on offlineIndicator close btn --- src/libs/actions/ReportActions.js | 3 ++- src/pages/home/report/ReportActionItem.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/ReportActions.js b/src/libs/actions/ReportActions.js index ca108c86a829..c8029d8f611e 100644 --- a/src/libs/actions/ReportActions.js +++ b/src/libs/actions/ReportActions.js @@ -103,9 +103,10 @@ function isFromCurrentUser(reportID, sequenceNumber, currentUserAccountID, actio * @param {Number} reportID * @param {String} clientID */ -function deleteClientAction(reportID, clientID) { +function deleteClientAction(reportID, clientID, sequenceNumber) { Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, { [clientID]: null, + [sequenceNumber]: null, }); } diff --git a/src/pages/home/report/ReportActionItem.js b/src/pages/home/report/ReportActionItem.js index 9d5926987242..78546c71f608 100644 --- a/src/pages/home/report/ReportActionItem.js +++ b/src/pages/home/report/ReportActionItem.js @@ -188,7 +188,7 @@ class ReportActionItem extends Component { )} > ReportActions.deleteClientAction(this.props.report.reportID, this.props.action.clientID)} + onClose={() => ReportActions.deleteClientAction(this.props.report.reportID, this.props.action.clientID, this.props.action.sequenceNumber)} pendingAction={this.props.action.pendingAction} errors={this.props.action.errors} errorRowStyles={[styles.ml10, styles.mr2]} From 4bce6bfdb59aed7eab1ca57325002714b2c7c167 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Tue, 16 Aug 2022 15:35:43 -0300 Subject: [PATCH 20/46] Check if sequenceNumber and clientID are supplied before clearing --- src/libs/actions/ReportActions.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/ReportActions.js b/src/libs/actions/ReportActions.js index c8029d8f611e..ee59fcb8b5ad 100644 --- a/src/libs/actions/ReportActions.js +++ b/src/libs/actions/ReportActions.js @@ -102,12 +102,19 @@ function isFromCurrentUser(reportID, sequenceNumber, currentUserAccountID, actio /** * @param {Number} reportID * @param {String} clientID + * @param {Number} sequenceNumber */ function deleteClientAction(reportID, clientID, sequenceNumber) { - Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, { - [clientID]: null, - [sequenceNumber]: null, - }); + if (clientID) { + Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, { + [clientID]: null, + }); + } + if (sequenceNumber) { + Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, { + [sequenceNumber]: null, + }); + } } export { From bcc2b6600598880b87e6c7f8979f64aab9f79193 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Thu, 18 Aug 2022 15:02:03 -0300 Subject: [PATCH 21/46] Hide message context menu when pending deletion --- src/pages/home/report/ReportActionItem.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pages/home/report/ReportActionItem.js b/src/pages/home/report/ReportActionItem.js index 78546c71f608..16afe53e23a9 100644 --- a/src/pages/home/report/ReportActionItem.js +++ b/src/pages/home/report/ReportActionItem.js @@ -214,6 +214,7 @@ class ReportActionItem extends Component { hovered && !this.state.isContextMenuActive && !this.props.draftMessage + && this.props.action.pendingAction !== 'delete' } draftMessage={this.props.draftMessage} /> From 69ba9f2c71af939b006a9d4be71b5dce9dd1528f Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Thu, 18 Aug 2022 17:24:37 -0300 Subject: [PATCH 22/46] Remove comments from Report.js --- src/libs/actions/Report.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index c6327ed00abf..2b6f392dab6e 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1216,7 +1216,6 @@ function deleteReportComment(reportID, reportAction) { const parameters = { reportID, - clientID: reportAction.clientID, sequenceNumber, reportActionID: reportAction.reportActionID, }; @@ -1287,7 +1286,6 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { optimisticReport.lastMessageText = ReportUtils.formatReportLastMessageText(textForEditedComment); } - // List of optmistic data to change const optimisticData = [ { onyxMethod: CONST.ONYX.METHOD.MERGE, @@ -1300,8 +1298,6 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { value: optimisticReport, }, ]; - - // On Success clear the pendingAction state const successData = [ { onyxMethod: CONST.ONYX.METHOD.MERGE, @@ -1313,8 +1309,6 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { }, }, ]; - - // On Error clear the pendingAction state and revert the message const failureData = [ { onyxMethod: CONST.ONYX.METHOD.MERGE, From 9a5b5bdffd973ee31424a458d8dac7e9f428febc Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Thu, 18 Aug 2022 17:27:36 -0300 Subject: [PATCH 23/46] Remove clientID from update and delete comment --- src/libs/actions/Report.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 2b6f392dab6e..bcff8f8445d8 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1325,7 +1325,6 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { const parameters = { reportID, sequenceNumber, - clientID: originalReportAction.clientID, reportComment: htmlForEditedComment, reportActionID: originalReportAction.reportActionID, }; From 70ef4750c2fc5005ba215991929e2b2cf57d8659 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Thu, 18 Aug 2022 17:32:10 -0300 Subject: [PATCH 24/46] Remove duplicated text message variable --- src/libs/actions/Report.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index bcff8f8445d8..1cf23587bc01 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1252,17 +1252,16 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { // Do not autolink if someone explicitly tries to remove a link from message. // https://github.com/Expensify/App/issues/9090 - const htmlForEditedComment = parser.replace(textForNewComment, {filterRules: _.filter(_.pluck(parser.rules, 'name'), name => name !== 'autolink')}); - const textForEditedComment = parser.htmlToText(htmlForEditedComment); + const htmlForNewComment = parser.replace(textForNewComment, {filterRules: _.filter(_.pluck(parser.rules, 'name'), name => name !== 'autolink')}); // Delete the comment if it's empty - if (_.isEmpty(htmlForEditedComment)) { + if (_.isEmpty(htmlForNewComment)) { deleteReportComment(reportID, originalReportAction); return; } // Skip the Edit if message is not changed - if (originalReportAction.message[0].html === htmlForEditedComment.trim()) { + if (originalReportAction.message[0].html === htmlForNewComment.trim()) { return; } @@ -1273,8 +1272,8 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, message: [{ isEdited: true, - html: htmlForEditedComment, - text: textForEditedComment, + html: htmlForNewComment, + text: textForNewComment, }], }, }; @@ -1283,7 +1282,7 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { const highestSequenceNumber = getMaxSequenceNumber(reportID); const optimisticReport = {}; if (sequenceNumber === highestSequenceNumber) { - optimisticReport.lastMessageText = ReportUtils.formatReportLastMessageText(textForEditedComment); + optimisticReport.lastMessageText = ReportUtils.formatReportLastMessageText(textForNewComment); } const optimisticData = [ @@ -1325,7 +1324,7 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { const parameters = { reportID, sequenceNumber, - reportComment: htmlForEditedComment, + reportComment: htmlForNewComment, reportActionID: originalReportAction.reportActionID, }; API.write('UpdateComment', parameters, {optimisticData, successData, failureData}); From b20e330b7da3e46a456be2b9770bc30ce73700e4 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Thu, 25 Aug 2022 09:58:21 +0200 Subject: [PATCH 25/46] Fix sequenceNumber type for deleteOptimisticReportAction after merge --- src/libs/actions/ReportActions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/ReportActions.js b/src/libs/actions/ReportActions.js index b7755fcfbd3c..4cbb0a4a00c6 100644 --- a/src/libs/actions/ReportActions.js +++ b/src/libs/actions/ReportActions.js @@ -101,7 +101,7 @@ function isFromCurrentUser(reportID, sequenceNumber, currentUserAccountID, actio /** * @param {Number} reportID - * @param {Number} sequenceNumber + * @param {String} sequenceNumber */ function deleteOptimisticReportAction(reportID, sequenceNumber) { Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, { From f8146dd7a3b5655d0edf86654bec569cb37694a5 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Tue, 30 Aug 2022 12:59:01 +0200 Subject: [PATCH 26/46] Fix typos and refactor Report.js --- src/libs/actions/Report.js | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 6780e38284f9..eb526cdb19ce 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -804,8 +804,8 @@ function addActions(reportID, text = '', file) { // We need a newSequenceNumber that is n larger than the current depending on how many actions we are adding. const actionCount = text && file ? 2 : 1; - const highestSequenceNumber = getMaxSequenceNumber(reportID); - const newSequenceNumber = highestSequenceNumber + actionCount; + const maxSequenceNumber = getMaxSequenceNumber(reportID); + const newSequenceNumber = maxSequenceNumber + actionCount; // Update the report in Onyx to have the new sequence number const optimisticReport = { @@ -1175,6 +1175,10 @@ function deleteReportComment(reportID, reportAction) { lastMessageText: ReportActions.getLastVisibleMessageText(reportID, { [sequenceNumber]: { ...reportAction, + message: [{ + html: '', + text: '', + }], }, }), }; @@ -1184,7 +1188,7 @@ function deleteReportComment(reportID, reportAction) { optimisticReport.unreadActionCount = Math.max(getUnreadActionCount(reportID) - 1, 0); } - // List of optmistic data to change + // List of optimistic data to change const optimisticData = [ { onyxMethod: CONST.ONYX.METHOD.MERGE, @@ -1296,25 +1300,26 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { }, }; - // Optimistically update the report's last message if the user edited it - const highestSequenceNumber = getMaxSequenceNumber(reportID); - const optimisticReport = {}; - if (sequenceNumber === highestSequenceNumber) { - optimisticReport.lastMessageText = ReportUtils.formatReportLastMessageText(textForNewComment); - } - const optimisticData = [ { onyxMethod: CONST.ONYX.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, value: optimisticReportActions, }, - { + ]; + + // Optimistically update the report's last message if the user edited it + const maxSequenceNumber = getMaxSequenceNumber(reportID); + if (sequenceNumber === maxSequenceNumber) { + optimisticData.push({ onyxMethod: CONST.ONYX.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, - value: optimisticReport, - }, - ]; + value: { + lastMessageText: ReportUtils.formatReportLastMessageText(textForNewComment), + }, + }); + } + const successData = [ { onyxMethod: CONST.ONYX.METHOD.MERGE, @@ -1349,7 +1354,7 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { } /** - * Saves the draft for a comment report action. This will put the comment into "edit mode" + * Saves the draft for a comment reportAction. This will put the comment into "edit mode" * * @param {Number} reportID * @param {Number} reportActionID From 70b383f249edaabd109151a8d4950dad342c1df3 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Tue, 30 Aug 2022 13:10:09 +0200 Subject: [PATCH 27/46] Clean up deleteReportComment logic --- src/libs/actions/Report.js | 66 ++++++++------------------------------ 1 file changed, 14 insertions(+), 52 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index eb526cdb19ce..5ce31d8c8462 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1165,28 +1165,20 @@ Onyx.connect({ */ function deleteReportComment(reportID, reportAction) { const sequenceNumber = reportAction.sequenceNumber; - const optimisticReportActions = { [sequenceNumber]: { pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, }, }; - const optimisticReport = { - lastMessageText: ReportActions.getLastVisibleMessageText(reportID, { - [sequenceNumber]: { - ...reportAction, - message: [{ - html: '', - text: '', - }], - }, - }), - }; + // TODO: Change the unreadCount here (I'll remove this comment later, I swear :p) + // const optimisticReport = { + // lastMessageText + // }; // If the deleted comment is more recent than our last read comment, update the unread count - if (sequenceNumber > getLastReadSequenceNumber(reportID)) { - optimisticReport.unreadActionCount = Math.max(getUnreadActionCount(reportID) - 1, 0); - } + // if (sequenceNumber > getLastReadSequenceNumber(reportID)) { + // optimisticReport.unreadActionCount = Math.max(getUnreadActionCount(reportID) - 1, 0); + // } // List of optimistic data to change const optimisticData = [ @@ -1195,25 +1187,22 @@ function deleteReportComment(reportID, reportAction) { key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, value: optimisticReportActions, }, - { - onyxMethod: CONST.ONYX.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, - value: optimisticReport, - }, + // { + // onyxMethod: CONST.ONYX.METHOD.MERGE, + // key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, + // value: optimisticReport, + // }, ]; - // On Success remove the message content and pendingAction state + // On Success remove pendingAction state, the message itself will be cleared by Pusher const successData = { [sequenceNumber]: { pendingAction: null, - message: [{ - html: '', - text: '', - }], }, }; // On Error clear the pendingAction state and revert the message + // TODO: Rollback the unreadCount (I'll remove this comment later, I swear :p) const failureData = [ { onyxMethod: CONST.ONYX.METHOD.MERGE, @@ -1225,15 +1214,6 @@ function deleteReportComment(reportID, reportAction) { }, }, }, - - // { - // onyxMethod: CONST.ONYX.METHOD.MERGE, - // key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, - // value: { - // lastMessageText: - // unreadActionCount: - // } - // } ]; const parameters = { @@ -1242,24 +1222,6 @@ function deleteReportComment(reportID, reportAction) { reportActionID: reportAction.reportActionID, }; API.write('DeleteComment', parameters, {optimisticData, successData, failureData}); - - // If all fails 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); } /** From 219dae4a8b412d648c930be7af72131021dec2d9 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Tue, 30 Aug 2022 17:19:10 +0200 Subject: [PATCH 28/46] Update unreadActionCount on deletion --- src/libs/actions/Report.js | 74 ++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 31 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 5ce31d8c8462..ca9aa2bfcb00 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1165,44 +1165,29 @@ Onyx.connect({ */ function deleteReportComment(reportID, reportAction) { const sequenceNumber = reportAction.sequenceNumber; + + // Let's add the strike-through to the message if the user goes/is offline. const optimisticReportActions = { [sequenceNumber]: { pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, }, }; - // TODO: Change the unreadCount here (I'll remove this comment later, I swear :p) - // const optimisticReport = { - // lastMessageText - // }; - // If the deleted comment is more recent than our last read comment, update the unread count - // if (sequenceNumber > getLastReadSequenceNumber(reportID)) { - // optimisticReport.unreadActionCount = Math.max(getUnreadActionCount(reportID) - 1, 0); - // } - - // List of optimistic data to change - 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, - // }, - ]; - - // On Success remove pendingAction state, the message itself will be cleared by Pusher - const successData = { - [sequenceNumber]: { - pendingAction: null, - }, + // 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: '', + }], + }, + }), }; - // On Error clear the pendingAction state and revert the message - // TODO: Rollback the unreadCount (I'll remove this comment later, I swear :p) + // If the API call fails we must show the message again, so let's revert the message content back to how it was + // and let's remove the pendingAction so the strike-trough is gone const failureData = [ { onyxMethod: CONST.ONYX.METHOD.MERGE, @@ -1216,12 +1201,39 @@ function deleteReportComment(reportID, reportAction) { }, ]; + // If we are deleting an unread message, let's decrease the unreadActionCount. + if (sequenceNumber > getLastReadSequenceNumber(reportID)) { + const unreadActionCount = getUnreadActionCount(reportID); + optimisticReport.unreadActionCount = Math.max(unreadActionCount - 1, 0); + + // And if the API call fails, let's rollback to the previous counter value. + failureData.push({ + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, + value: {unreadActionCount}, + }); + } + + // No need for successData because the API will clear the message on success with Pusher + 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}); + API.write('DeleteComment', parameters, {optimisticData, failureData}); } /** From 493768aaa459067f2d942051a2741220f6252d80 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Tue, 30 Aug 2022 17:20:55 +0200 Subject: [PATCH 29/46] Remove old editComment subscription logic --- src/libs/actions/Report.js | 42 -------------------------------------- 1 file changed, 42 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index ca9aa2bfcb00..7eb0b15ac501 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -436,48 +436,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. */ From c335b0a002b3013866bef54ee4f3b0f5a84a06a3 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Tue, 30 Aug 2022 17:21:21 +0200 Subject: [PATCH 30/46] Remove old editComment subscription logic reference --- src/libs/Navigation/AppNavigator/AuthScreens.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index cbb6e0af27a3..b4cd14fe6cd0 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -110,7 +110,6 @@ class AuthScreens extends React.Component { cluster: CONFIG.PUSHER.CLUSTER, authEndpoint: `${CONFIG.EXPENSIFY.URL_API_ROOT}api?command=AuthenticatePusher`, }).then(() => { - Report.subscribeToUserEvents(); User.subscribeToUserEvents(); Policy.subscribeToPolicyEvents(); }); From c498a35d8e088c89e9e0401f114d776e47180b43 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Tue, 30 Aug 2022 17:23:36 +0200 Subject: [PATCH 31/46] Remove remaining references to old pusher subscriber for edit comment --- src/libs/Navigation/AppNavigator/AuthScreens.js | 1 - src/libs/actions/Report.js | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index b4cd14fe6cd0..0d1eceb4d711 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -8,7 +8,6 @@ import * as StyleUtils from '../../../styles/StyleUtils'; import withWindowDimensions, {windowDimensionsPropTypes} from '../../../components/withWindowDimensions'; import CONST from '../../../CONST'; import compose from '../../compose'; -import * as Report from '../../actions/Report'; import * as PersonalDetails from '../../actions/PersonalDetails'; import * as Pusher from '../../Pusher/pusher'; import PusherConnectionManager from '../../PusherConnectionManager'; diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 7eb0b15ac501..64a145532b41 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -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'; @@ -1598,7 +1597,6 @@ export { updateNotificationPreference, setNewMarkerPosition, subscribeToReportTypingEvents, - subscribeToUserEvents, subscribeToReportCommentPushNotifications, unsubscribeFromReportChannel, saveReportComment, From b6a179e67d99a51cebdcb52a76955ab950406f2b Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Wed, 31 Aug 2022 12:54:02 +0200 Subject: [PATCH 32/46] Spread reportActionItemMessage styles property --- src/pages/home/report/ReportActionItemMessage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/home/report/ReportActionItemMessage.js b/src/pages/home/report/ReportActionItemMessage.js index 5acdb40ea53b..9cfa1e5eb537 100644 --- a/src/pages/home/report/ReportActionItemMessage.js +++ b/src/pages/home/report/ReportActionItemMessage.js @@ -36,7 +36,7 @@ const ReportActionItemMessage = (props) => { const isUnsent = props.network.isOffline && props.action.isLoading; return ( - + {_.map(_.compact(props.action.message), (fragment, index) => ( Date: Wed, 31 Aug 2022 14:33:49 +0200 Subject: [PATCH 33/46] Only hide delete function instead of entire contextMenu when pending deletion --- src/libs/ReportUtils.js | 3 ++- src/pages/home/report/ReportActionItem.js | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 9dc4158537f0..39bf2a02548e 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -90,7 +90,8 @@ function canEditReportAction(reportAction) { function canDeleteReportAction(reportAction) { return reportAction.actorEmail === sessionEmail && reportAction.reportActionID - && reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT; + && reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT + && reportAction.pendingAction !== 'delete'; } /** diff --git a/src/pages/home/report/ReportActionItem.js b/src/pages/home/report/ReportActionItem.js index fd1271dc42df..f6d0a2c3153a 100644 --- a/src/pages/home/report/ReportActionItem.js +++ b/src/pages/home/report/ReportActionItem.js @@ -214,7 +214,6 @@ class ReportActionItem extends Component { hovered && !this.state.isContextMenuActive && !this.props.draftMessage - && this.props.action.pendingAction !== 'delete' } draftMessage={this.props.draftMessage} /> From a41fa8487fd7e22eb20e0d4e757234facd37e0a0 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Wed, 31 Aug 2022 14:37:14 +0200 Subject: [PATCH 34/46] Remove edit option for pending deleted comments --- 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 39bf2a02548e..6c9457833991 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -76,7 +76,8 @@ function canEditReportAction(reportAction) { return reportAction.actorEmail === sessionEmail && reportAction.reportActionID && reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT - && !isReportMessageAttachment(lodashGet(reportAction, ['message', 0], {})); + && !isReportMessageAttachment(lodashGet(reportAction, ['message', 0], {})) + && reportAction.pendingAction !== 'delete'; } /** From 2fb375cc768cbdda1911e8b7e05798b259d16dc3 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Wed, 31 Aug 2022 15:08:30 +0200 Subject: [PATCH 35/46] Add getLastVisibleReportAction utility --- src/libs/actions/ReportActions.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/libs/actions/ReportActions.js b/src/libs/actions/ReportActions.js index 4cbb0a4a00c6..d90dbabf501d 100644 --- a/src/libs/actions/ReportActions.js +++ b/src/libs/actions/ReportActions.js @@ -86,6 +86,21 @@ function getLastVisibleMessageText(reportID, actionsToMerge = {}) { return ReportUtils.formatReportLastMessageText(messageText); } +/** + * Get last reportAction in the chat that was not deleted + * @param {Number} reportID + * @param {Object} [actionsToMerge] + * @return {Object} + */ +function getLastVisibleReportAction(reportID, actionsToMerge = {}) { + const existingReportActions = _.indexBy(reportActions[reportID], 'sequenceNumber'); + const actions = _.toArray(lodashMerge({}, existingReportActions, actionsToMerge)); + const lastVisibleReportAction = _.findLast(actions, action => ( + !ReportActionsUtils.isDeletedAction(action) + )); + return lastVisibleReportAction; +} + /** * @param {Number} reportID * @param {Number} sequenceNumber @@ -112,6 +127,7 @@ function deleteOptimisticReportAction(reportID, sequenceNumber) { export { getDeletedCommentsCount, getLastVisibleMessageText, + getLastVisibleReportAction, isFromCurrentUser, deleteOptimisticReportAction, }; From 765b0d81f856f3aab9bd84958989a67308bf7f39 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Wed, 31 Aug 2022 15:12:19 +0200 Subject: [PATCH 36/46] Update lastMessageText if latest visible message is edited --- src/libs/actions/Report.js | 39 ++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 32f18ebe9d43..1b5988571f64 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1240,9 +1240,22 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { }, ]; - // Optimistically update the report's last message if the user edited it - const maxSequenceNumber = getMaxSequenceNumber(reportID); - if (sequenceNumber === maxSequenceNumber) { + const failureData = [ + { + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, + value: { + [sequenceNumber]: { + ...originalReportAction, + pendingAction: null, + }, + }, + }, + ]; + + // If the very last visible message is being edited, let's update the chat lastMessageText and rollback it if everything fails + const lastVisibleReportAction = ReportUtils.getLastVisibleReportAction(reportID); + if (lastVisibleReportAction && sequenceNumber === lastVisibleReportAction.sequenceNumber) { optimisticData.push({ onyxMethod: CONST.ONYX.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, @@ -1250,26 +1263,24 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { lastMessageText: ReportUtils.formatReportLastMessageText(textForNewComment), }, }); - } - const successData = [ - { + const messageHtml = lodashGet(lastVisibleReportAction, ['message', 0, 'html'], ''); + const previousLastMessage = ReportUtils.formatReportLastMessageText(messageHtml); + failureData.push({ onyxMethod: CONST.ONYX.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, value: { - [sequenceNumber]: { - pendingAction: null, - }, + lastMessageText: previousLastMessage, }, - }, - ]; - const failureData = [ + }); + } + + const successData = [ { onyxMethod: CONST.ONYX.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, value: { [sequenceNumber]: { - ...originalReportAction, pendingAction: null, }, }, From 827ad50ddc5bc5c416d0a017454ac72e3061a803 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Wed, 31 Aug 2022 15:33:08 +0200 Subject: [PATCH 37/46] Remove unreadActionCount rollback --- src/libs/actions/Report.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 7ace5237e87e..32ea8be42606 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1154,13 +1154,6 @@ function deleteReportComment(reportID, reportAction) { if (sequenceNumber > getLastReadSequenceNumber(reportID)) { const unreadActionCount = getUnreadActionCount(reportID); optimisticReport.unreadActionCount = Math.max(unreadActionCount - 1, 0); - - // And if the API call fails, let's rollback to the previous counter value. - failureData.push({ - onyxMethod: CONST.ONYX.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, - value: {unreadActionCount}, - }); } // No need for successData because the API will clear the message on success with Pusher From 0e0376c0a489316beee2e1115f29843bb3ee1983 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Wed, 31 Aug 2022 15:49:53 +0200 Subject: [PATCH 38/46] Fix reference typo for getLastVisibleReportAction --- src/libs/actions/Report.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 32ea8be42606..0171702c29f0 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1238,7 +1238,7 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { ]; // If the very last visible message is being edited, let's update the chat lastMessageText and rollback it if everything fails - const lastVisibleReportAction = ReportUtils.getLastVisibleReportAction(reportID); + const lastVisibleReportAction = ReportActions.getLastVisibleReportAction(reportID); if (lastVisibleReportAction && sequenceNumber === lastVisibleReportAction.sequenceNumber) { optimisticData.push({ onyxMethod: CONST.ONYX.METHOD.MERGE, From e076529de6981a500a317ef90c4e92cc751c5c14 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Wed, 31 Aug 2022 15:52:53 +0200 Subject: [PATCH 39/46] Fix underscore findLast non-existing reference --- src/libs/actions/ReportActions.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/ReportActions.js b/src/libs/actions/ReportActions.js index d90dbabf501d..fa73c7a72ec1 100644 --- a/src/libs/actions/ReportActions.js +++ b/src/libs/actions/ReportActions.js @@ -95,10 +95,10 @@ function getLastVisibleMessageText(reportID, actionsToMerge = {}) { function getLastVisibleReportAction(reportID, actionsToMerge = {}) { const existingReportActions = _.indexBy(reportActions[reportID], 'sequenceNumber'); const actions = _.toArray(lodashMerge({}, existingReportActions, actionsToMerge)); - const lastVisibleReportAction = _.findLast(actions, action => ( + const lastVisibleReportActionIndex = _.findLastIndex(actions, action => ( !ReportActionsUtils.isDeletedAction(action) )); - return lastVisibleReportAction; + return actions[lastVisibleReportActionIndex]; } /** From fb063f014407ba430d8c046d31a8dd27152cd592 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Wed, 31 Aug 2022 16:02:16 +0200 Subject: [PATCH 40/46] Dismiss errors for edit and delete comment --- src/libs/actions/ReportActions.js | 18 ++++++++++++++---- src/pages/home/report/ReportActionItem.js | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/libs/actions/ReportActions.js b/src/libs/actions/ReportActions.js index fa73c7a72ec1..fb159424a372 100644 --- a/src/libs/actions/ReportActions.js +++ b/src/libs/actions/ReportActions.js @@ -117,11 +117,21 @@ function isFromCurrentUser(reportID, sequenceNumber, currentUserAccountID, actio /** * @param {Number} reportID * @param {String} sequenceNumber + * @param {String} pendingAction */ -function deleteOptimisticReportAction(reportID, sequenceNumber) { - Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, { - [sequenceNumber]: null, - }); +function deleteOptimisticReportAction(reportID, sequenceNumber, pendingAction) { + if (pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) { + Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, { + [sequenceNumber]: null, + }); + } else { + Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, { + [sequenceNumber]: { + pendingAction: null, + errors: null, + }, + }); + } } export { diff --git a/src/pages/home/report/ReportActionItem.js b/src/pages/home/report/ReportActionItem.js index f6d0a2c3153a..167519c4ca3d 100644 --- a/src/pages/home/report/ReportActionItem.js +++ b/src/pages/home/report/ReportActionItem.js @@ -188,7 +188,7 @@ class ReportActionItem extends Component { )} > ReportActions.deleteOptimisticReportAction(this.props.report.reportID, this.props.action.sequenceNumber)} + onClose={() => ReportActions.deleteOptimisticReportAction(this.props.report.reportID, this.props.action.sequenceNumber, this.props.action.pendingAction)} pendingAction={this.props.action.pendingAction} errors={this.props.action.errors} errorRowStyles={[styles.ml10, styles.mr2]} From 70f6d27dafd84d2adb025f36432dd71f2a0a0ba5 Mon Sep 17 00:00:00 2001 From: Paulo Vale Date: Wed, 31 Aug 2022 16:27:25 +0200 Subject: [PATCH 41/46] Clear errors after deletion success for edge case --- src/libs/actions/Report.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 0171702c29f0..bde171d1f213 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1150,6 +1150,20 @@ function deleteReportComment(reportID, reportAction) { }, ]; + // In case the message already has errors but the user tries to delete again and it works, let's clear the errors + const successData = [ + { + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, + value: { + [sequenceNumber]: { + pendingAction: null, + errors: null, + }, + }, + }, + ]; + // If we are deleting an unread message, let's decrease the unreadActionCount. if (sequenceNumber > getLastReadSequenceNumber(reportID)) { const unreadActionCount = getUnreadActionCount(reportID); @@ -1175,7 +1189,7 @@ function deleteReportComment(reportID, reportAction) { sequenceNumber, reportActionID: reportAction.reportActionID, }; - API.write('DeleteComment', parameters, {optimisticData, failureData}); + API.write('DeleteComment', parameters, {optimisticData, successData, failureData}); } /** From c39972a0ad585edf049d5a041c458fa702b874ea Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Wed, 31 Aug 2022 17:40:52 +0200 Subject: [PATCH 42/46] Removing unneeded things --- src/libs/ReportUtils.js | 4 +- src/libs/actions/Report.js | 34 ++------------- src/libs/actions/ReportActions.js | 43 +++++++------------ src/pages/home/report/ReportActionItem.js | 8 +++- .../home/report/ReportActionItemMessage.js | 13 +----- 5 files changed, 30 insertions(+), 72 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 82808c6561a0..5146eeb8e986 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -100,7 +100,7 @@ function canEditReportAction(reportAction) { && reportAction.reportActionID && reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT && !isReportMessageAttachment(lodashGet(reportAction, ['message', 0], {})) - && reportAction.pendingAction !== 'delete'; + && reportAction.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE; } /** @@ -115,7 +115,7 @@ function canDeleteReportAction(reportAction) { return reportAction.actorEmail === sessionEmail && reportAction.reportActionID && reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT - && reportAction.pendingAction !== 'delete'; + && reportAction.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE; } /** diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index bde171d1f213..e4094c7008a5 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -753,8 +753,8 @@ function addActions(reportID, text = '', file) { // We need a newSequenceNumber that is n larger than the current depending on how many actions we are adding. const actionCount = text && file ? 2 : 1; - const maxSequenceNumber = getMaxSequenceNumber(reportID); - const newSequenceNumber = maxSequenceNumber + actionCount; + const highestSequenceNumber = getMaxSequenceNumber(reportID); + const newSequenceNumber = highestSequenceNumber + actionCount; // Update the report in Onyx to have the new sequence number const optimisticReport = { @@ -1114,8 +1114,6 @@ Onyx.connect({ */ function deleteReportComment(reportID, reportAction) { const sequenceNumber = reportAction.sequenceNumber; - - // Let's add the strike-through to the message if the user goes/is offline. const optimisticReportActions = { [sequenceNumber]: { pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, @@ -1150,7 +1148,6 @@ function deleteReportComment(reportID, reportAction) { }, ]; - // In case the message already has errors but the user tries to delete again and it works, let's clear the errors const successData = [ { onyxMethod: CONST.ONYX.METHOD.MERGE, @@ -1158,19 +1155,18 @@ function deleteReportComment(reportID, reportAction) { value: { [sequenceNumber]: { pendingAction: null, - errors: null, }, }, }, ]; - // If we are deleting an unread message, let's decrease the unreadActionCount. + // 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); } - // No need for successData because the API will clear the message on success with Pusher const optimisticData = [ { onyxMethod: CONST.ONYX.METHOD.MERGE, @@ -1251,28 +1247,6 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { }, ]; - // If the very last visible message is being edited, let's update the chat lastMessageText and rollback it if everything fails - const lastVisibleReportAction = ReportActions.getLastVisibleReportAction(reportID); - if (lastVisibleReportAction && sequenceNumber === lastVisibleReportAction.sequenceNumber) { - optimisticData.push({ - onyxMethod: CONST.ONYX.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, - value: { - lastMessageText: ReportUtils.formatReportLastMessageText(textForNewComment), - }, - }); - - const messageHtml = lodashGet(lastVisibleReportAction, ['message', 0, 'html'], ''); - const previousLastMessage = ReportUtils.formatReportLastMessageText(messageHtml); - failureData.push({ - onyxMethod: CONST.ONYX.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, - value: { - lastMessageText: previousLastMessage, - }, - }); - } - const successData = [ { onyxMethod: CONST.ONYX.METHOD.MERGE, diff --git a/src/libs/actions/ReportActions.js b/src/libs/actions/ReportActions.js index fb159424a372..2eeedeb27246 100644 --- a/src/libs/actions/ReportActions.js +++ b/src/libs/actions/ReportActions.js @@ -86,21 +86,6 @@ function getLastVisibleMessageText(reportID, actionsToMerge = {}) { return ReportUtils.formatReportLastMessageText(messageText); } -/** - * Get last reportAction in the chat that was not deleted - * @param {Number} reportID - * @param {Object} [actionsToMerge] - * @return {Object} - */ -function getLastVisibleReportAction(reportID, actionsToMerge = {}) { - const existingReportActions = _.indexBy(reportActions[reportID], 'sequenceNumber'); - const actions = _.toArray(lodashMerge({}, existingReportActions, actionsToMerge)); - const lastVisibleReportActionIndex = _.findLastIndex(actions, action => ( - !ReportActionsUtils.isDeletedAction(action) - )); - return actions[lastVisibleReportActionIndex]; -} - /** * @param {Number} reportID * @param {Number} sequenceNumber @@ -120,24 +105,28 @@ function isFromCurrentUser(reportID, sequenceNumber, currentUserAccountID, actio * @param {String} pendingAction */ function deleteOptimisticReportAction(reportID, sequenceNumber, pendingAction) { - if (pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) { - Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, { - [sequenceNumber]: null, - }); - } else { - Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, { - [sequenceNumber]: { - pendingAction: null, - errors: null, - }, - }); - } + Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, { + [sequenceNumber]: null, + }); +} + +/** + * @param {Number} reportID + * @param {String} sequenceNumber + */ +function clearReportActionErrors(reportID, sequenceNumber) { + Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, { + [sequenceNumber]: { + errors: null, + }, + }); } export { getDeletedCommentsCount, getLastVisibleMessageText, getLastVisibleReportAction, + clearReportActionErrors, isFromCurrentUser, deleteOptimisticReportAction, }; diff --git a/src/pages/home/report/ReportActionItem.js b/src/pages/home/report/ReportActionItem.js index 167519c4ca3d..59475e15d4b1 100644 --- a/src/pages/home/report/ReportActionItem.js +++ b/src/pages/home/report/ReportActionItem.js @@ -188,7 +188,13 @@ class ReportActionItem extends Component { )} > ReportActions.deleteOptimisticReportAction(this.props.report.reportID, this.props.action.sequenceNumber, this.props.action.pendingAction)} + onClose={() => { + if (this.props.action.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) { + ReportActions.deleteOptimisticReportAction(this.props.report.reportID, this.props.action.sequenceNumber); + } else { + ReportActions.clearReportActionErrors(this.props.report.reportID, this.props.action.sequenceNumber); + } + }} pendingAction={this.props.action.pendingAction} errors={this.props.action.errors} errorRowStyles={[styles.ml10, styles.mr2]} diff --git a/src/pages/home/report/ReportActionItemMessage.js b/src/pages/home/report/ReportActionItemMessage.js index 9cfa1e5eb537..18d4b2306807 100644 --- a/src/pages/home/report/ReportActionItemMessage.js +++ b/src/pages/home/report/ReportActionItemMessage.js @@ -18,25 +18,15 @@ const propTypes = { /** Information about the network */ network: networkPropTypes.isRequired, - /** Additional styles to add after local styles. */ - style: PropTypes.oneOfType([ - PropTypes.arrayOf(PropTypes.object), - PropTypes.object, - ]), - /** localization props */ ...withLocalizePropTypes, }; -const defaultProps = { - style: [], -}; - const ReportActionItemMessage = (props) => { const isUnsent = props.network.isOffline && props.action.isLoading; return ( - + {_.map(_.compact(props.action.message), (fragment, index) => ( { }; ReportActionItemMessage.propTypes = propTypes; -ReportActionItemMessage.defaultProps = defaultProps; ReportActionItemMessage.displayName = 'ReportActionItemMessage'; export default compose( From 542d0e1605e890e15e0bd4efad9e977b19954f91 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Wed, 31 Aug 2022 17:42:01 +0200 Subject: [PATCH 43/46] remove some other changes --- src/libs/actions/Report.js | 2 +- src/libs/actions/ReportActions.js | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index e4094c7008a5..0711d8bd95c8 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1269,7 +1269,7 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { } /** - * Saves the draft for a comment reportAction. This will put the comment into "edit mode" + * Saves the draft for a comment report action. This will put the comment into "edit mode" * * @param {Number} reportID * @param {Number} reportActionID diff --git a/src/libs/actions/ReportActions.js b/src/libs/actions/ReportActions.js index 2eeedeb27246..c38f5f07bc95 100644 --- a/src/libs/actions/ReportActions.js +++ b/src/libs/actions/ReportActions.js @@ -102,9 +102,8 @@ function isFromCurrentUser(reportID, sequenceNumber, currentUserAccountID, actio /** * @param {Number} reportID * @param {String} sequenceNumber - * @param {String} pendingAction */ -function deleteOptimisticReportAction(reportID, sequenceNumber, pendingAction) { +function deleteOptimisticReportAction(reportID, sequenceNumber) { Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, { [sequenceNumber]: null, }); From 2f9de3d5532c7d4d01f95a863299d14349c4baaf Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Wed, 31 Aug 2022 17:42:29 +0200 Subject: [PATCH 44/46] Remove lastVisibleReportAction export --- src/libs/actions/ReportActions.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/actions/ReportActions.js b/src/libs/actions/ReportActions.js index c38f5f07bc95..504e915ef76b 100644 --- a/src/libs/actions/ReportActions.js +++ b/src/libs/actions/ReportActions.js @@ -124,7 +124,6 @@ function clearReportActionErrors(reportID, sequenceNumber) { export { getDeletedCommentsCount, getLastVisibleMessageText, - getLastVisibleReportAction, clearReportActionErrors, isFromCurrentUser, deleteOptimisticReportAction, From 764a569dd8b1d23e63b67eea52cb70ad56dfd6ce Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Wed, 31 Aug 2022 17:43:50 +0200 Subject: [PATCH 45/46] fix typo clean up commment --- src/libs/actions/Report.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 0711d8bd95c8..397a6893231c 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1133,8 +1133,8 @@ function deleteReportComment(reportID, reportAction) { }), }; - // If the API call fails we must show the message again, so let's revert the message content back to how it was - // and let's remove the pendingAction so the strike-trough is gone + // 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, From b5b7fb3aec6506c26bc8cf4fd3726d6a58180605 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Wed, 31 Aug 2022 18:00:29 +0200 Subject: [PATCH 46/46] this is actually needed --- src/pages/home/report/ReportActionItemMessage.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/pages/home/report/ReportActionItemMessage.js b/src/pages/home/report/ReportActionItemMessage.js index 18d4b2306807..9cfa1e5eb537 100644 --- a/src/pages/home/report/ReportActionItemMessage.js +++ b/src/pages/home/report/ReportActionItemMessage.js @@ -18,15 +18,25 @@ const propTypes = { /** Information about the network */ network: networkPropTypes.isRequired, + /** Additional styles to add after local styles. */ + style: PropTypes.oneOfType([ + PropTypes.arrayOf(PropTypes.object), + PropTypes.object, + ]), + /** localization props */ ...withLocalizePropTypes, }; +const defaultProps = { + style: [], +}; + const ReportActionItemMessage = (props) => { const isUnsent = props.network.isOffline && props.action.isLoading; return ( - + {_.map(_.compact(props.action.message), (fragment, index) => ( { }; ReportActionItemMessage.propTypes = propTypes; +ReportActionItemMessage.defaultProps = defaultProps; ReportActionItemMessage.displayName = 'ReportActionItemMessage'; export default compose(