From 5dd71c18a9bb3877d84f0a8a98a48ad59521f5ae Mon Sep 17 00:00:00 2001 From: Pep Oliver Date: Thu, 28 Oct 2021 12:11:22 +0200 Subject: [PATCH 1/3] check if reports data is up to date before update last read action id --- src/libs/actions/Report.js | 13 +++++++++++++ src/pages/home/report/ReportActionsView.js | 5 ++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 4d8567ff67c1..d6a774c6d97f 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -78,6 +78,9 @@ const lastReadSequenceNumbers = {}; // since we will then be up to date and any optimistic actions that are still waiting to be replaced can be removed. const optimisticReportActionIDs = {}; +// Boolean to indicate if reports data comes from the API request or from local cache. +let latestReportsData = false; + /** * Checks the report to see if there are any unread action items * @@ -940,6 +943,7 @@ function fetchAllReports( }) .then((returnedReports) => { Onyx.set(ONYXKEYS.INITIAL_REPORT_DATA_LOADED, true); + latestReportsData = true; // If at this point the user still doesn't have a Concierge report, create it for them. // This means they were a participant in reports before their account was created (e.g. default rooms) @@ -1390,6 +1394,14 @@ function handleInaccessibleReport() { navigateToConciergeChat(); } +/** + * Check if report data is up to date or it comes from local cache + * @returns {Boolean} + */ +function isReportsDataUptoDate() { + return latestReportsData; +} + export { fetchAllReports, fetchActions, @@ -1416,4 +1428,5 @@ export { syncChatAndIOUReports, navigateToConciergeChat, handleInaccessibleReport, + isReportsDataUptoDate, }; diff --git a/src/pages/home/report/ReportActionsView.js b/src/pages/home/report/ReportActionsView.js index 7dcc30391857..b1947f723fcd 100755 --- a/src/pages/home/report/ReportActionsView.js +++ b/src/pages/home/report/ReportActionsView.js @@ -16,6 +16,7 @@ import { setNewMarkerPosition, subscribeToReportTypingEvents, unsubscribeFromReportChannel, + isReportsDataUptoDate, } from '../../../libs/actions/Report'; import ReportActionItem from './ReportActionItem'; import styles from '../../../styles/styles'; @@ -132,7 +133,9 @@ class ReportActionsView extends React.Component { // Only mark as read if the report is open if (!this.props.isDrawerOpen) { - updateLastReadActionID(this.props.reportID); + if (isReportsDataUptoDate()) { + updateLastReadActionID(this.props.reportID); + } } this.updateUnreadIndicatorPosition(this.props.report.unreadActionCount); From bed6ebcd136738cb70337794e9f4510bef9cdb46 Mon Sep 17 00:00:00 2001 From: Pep Oliver Date: Thu, 28 Oct 2021 19:52:35 +0200 Subject: [PATCH 2/3] Variable renamed to be more clear and grammatical correction done --- src/libs/actions/Report.js | 12 ++++++------ src/pages/home/report/ReportActionsView.js | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index d6a774c6d97f..1e1e8ea79071 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -78,8 +78,8 @@ const lastReadSequenceNumbers = {}; // since we will then be up to date and any optimistic actions that are still waiting to be replaced can be removed. const optimisticReportActionIDs = {}; -// Boolean to indicate if reports data comes from the API request or from local cache. -let latestReportsData = false; +// Boolean to indicate if report data comes from the API request or from local cache. +let isReportDataFresh = false; /** * Checks the report to see if there are any unread action items @@ -943,7 +943,7 @@ function fetchAllReports( }) .then((returnedReports) => { Onyx.set(ONYXKEYS.INITIAL_REPORT_DATA_LOADED, true); - latestReportsData = true; + isReportDataFresh = true; // If at this point the user still doesn't have a Concierge report, create it for them. // This means they were a participant in reports before their account was created (e.g. default rooms) @@ -1398,8 +1398,8 @@ function handleInaccessibleReport() { * Check if report data is up to date or it comes from local cache * @returns {Boolean} */ -function isReportsDataUptoDate() { - return latestReportsData; +function isReportDataUptoDate() { + return isReportDataFresh; } export { @@ -1428,5 +1428,5 @@ export { syncChatAndIOUReports, navigateToConciergeChat, handleInaccessibleReport, - isReportsDataUptoDate, + isReportDataUptoDate, }; diff --git a/src/pages/home/report/ReportActionsView.js b/src/pages/home/report/ReportActionsView.js index b1947f723fcd..a62107225bc2 100755 --- a/src/pages/home/report/ReportActionsView.js +++ b/src/pages/home/report/ReportActionsView.js @@ -16,7 +16,7 @@ import { setNewMarkerPosition, subscribeToReportTypingEvents, unsubscribeFromReportChannel, - isReportsDataUptoDate, + isReportDataUptoDate, } from '../../../libs/actions/Report'; import ReportActionItem from './ReportActionItem'; import styles from '../../../styles/styles'; @@ -133,7 +133,7 @@ class ReportActionsView extends React.Component { // Only mark as read if the report is open if (!this.props.isDrawerOpen) { - if (isReportsDataUptoDate()) { + if (isReportDataUptoDate()) { updateLastReadActionID(this.props.reportID); } } From 74bb20123b0f6d6d2656c42849be35c86a5f82da Mon Sep 17 00:00:00 2001 From: Pep Oliver Date: Fri, 5 Nov 2021 17:12:04 +0100 Subject: [PATCH 3/3] IS_LOADING_REPORT_DATA Onyx key added. updateLastReadActionID function checks its value before to update a sequence number --- src/ONYXKEYS.js | 3 +++ src/libs/actions/Report.js | 25 +++++++++++----------- src/pages/home/report/ReportActionsView.js | 4 +--- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/ONYXKEYS.js b/src/ONYXKEYS.js index 947d0aaf0c31..6cc10430cea6 100755 --- a/src/ONYXKEYS.js +++ b/src/ONYXKEYS.js @@ -147,4 +147,7 @@ export default { // Are report actions loading? IS_LOADING_REPORT_ACTIONS: 'isLoadingReportActions', + + // Is report data loading? + IS_LOADING_REPORT_DATA: 'isLoadingReportData', }; diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 84f2d23da1c0..641df36897c7 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -78,8 +78,12 @@ const lastReadSequenceNumbers = {}; // since we will then be up to date and any optimistic actions that are still waiting to be replaced can be removed. const optimisticReportActionIDs = {}; -// Boolean to indicate if report data comes from the API request or from local cache. -let isReportDataFresh = false; +// Boolean to indicate if report data is loading from the API or not. +let isReportDataLoading = true; +Onyx.connect({ + key: ONYXKEYS.IS_LOADING_REPORT_DATA, + callback: val => isReportDataLoading = val, +}); /** * Checks the report to see if there are any unread action items @@ -935,6 +939,7 @@ function fetchAllReports( shouldRecordHomePageTiming = false, shouldDelayActionsFetch = false, ) { + Onyx.set(ONYXKEYS.IS_LOADING_REPORT_DATA, true); return API.Get({ returnValueList: 'chatList', }) @@ -955,7 +960,7 @@ function fetchAllReports( }) .then((returnedReports) => { Onyx.set(ONYXKEYS.INITIAL_REPORT_DATA_LOADED, true); - isReportDataFresh = true; + Onyx.set(ONYXKEYS.IS_LOADING_REPORT_DATA, false); // If at this point the user still doesn't have a Concierge report, create it for them. // This means they were a participant in reports before their account was created (e.g. default rooms) @@ -1171,6 +1176,11 @@ function deleteReportComment(reportID, reportAction) { * is last read (meaning that the entire report history has been read) */ function updateLastReadActionID(reportID, sequenceNumber) { + // If report data is loading, we can't update the last read sequence number because it is obsolete + if (isReportDataLoading) { + return; + } + // If we aren't specifying a sequenceNumber and have no valid maxSequenceNumber for this report then we should not // update the last read. Most likely, we have just created the report and it has no comments. But we should err on // the side of caution and do nothing in this case. @@ -1417,14 +1427,6 @@ function handleInaccessibleReport() { navigateToConciergeChat(); } -/** - * Check if report data is up to date or it comes from local cache - * @returns {Boolean} - */ -function isReportDataUptoDate() { - return isReportDataFresh; -} - export { fetchAllReports, fetchActions, @@ -1451,7 +1453,6 @@ export { syncChatAndIOUReports, navigateToConciergeChat, handleInaccessibleReport, - isReportDataUptoDate, setReportWithDraft, fetchActionsWithLoadingState, }; diff --git a/src/pages/home/report/ReportActionsView.js b/src/pages/home/report/ReportActionsView.js index e80b051fc1a9..a32586f9ec64 100755 --- a/src/pages/home/report/ReportActionsView.js +++ b/src/pages/home/report/ReportActionsView.js @@ -130,9 +130,7 @@ class ReportActionsView extends React.Component { // Only mark as read if the report is open if (!this.props.isDrawerOpen) { - if (Report.isReportDataUptoDate()) { - Report.updateLastReadActionID(this.props.reportID); - } + Report.updateLastReadActionID(this.props.reportID); } this.updateUnreadIndicatorPosition(this.props.report.unreadActionCount);