Skip to content

Commit

Permalink
Merge pull request #10164 from Expensify/afonseca_get_history_open_re…
Browse files Browse the repository at this point in the history
…port

Refactor OpenReport API call for Report GetHistory
  • Loading branch information
tgolen authored Aug 18, 2022
2 parents 99271b8 + d2695b2 commit a4c70a6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 109 deletions.
2 changes: 0 additions & 2 deletions src/ONYXKEYS.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ export default {
POLICY: 'policy_',
REPORTS_WITH_DRAFT: 'reportWithDraft_',
REPORT_IS_COMPOSER_FULL_SIZE: 'reportIsComposerFullSize_',
IS_LOADING_INITIAL_REPORT_ACTIONS: 'isLoadingInitialReportActions_',
IS_LOADING_MORE_REPORT_ACTIONS: 'isLoadingMoreReportActions_',
POLICY_MEMBER_LIST: 'policyMemberList_',
},

Expand Down
54 changes: 30 additions & 24 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,15 +625,14 @@ function fetchOrCreateChatReport(participants, shouldNavigate = true) {
}

/**
* Get the actions of a report
* Get the initial actions of a report
*
* @param {Number} reportID
* @returns {Promise}
*/
function fetchActions(reportID) {
function fetchInitialActions(reportID) {
const reportActionsOffset = -1;

return DeprecatedAPI.Report_GetHistory({
DeprecatedAPI.Report_GetHistory({
reportID,
reportActionsOffset,
reportActionsLimit: CONST.REPORT.ACTIONS.LIMIT,
Expand All @@ -644,17 +643,6 @@ function fetchActions(reportID) {
});
}

/**
* Get the initial actions of a report
*
* @param {Number} reportID
*/
function fetchInitialActions(reportID) {
Onyx.set(`${ONYXKEYS.COLLECTION.IS_LOADING_INITIAL_REPORT_ACTIONS}${reportID}`, true);
fetchActions(reportID)
.finally(() => Onyx.set(`${ONYXKEYS.COLLECTION.IS_LOADING_INITIAL_REPORT_ACTIONS}${reportID}`, false));
}

/**
* Get all of our reports
*
Expand Down Expand Up @@ -989,22 +977,34 @@ function deleteReportComment(reportID, reportAction) {
* @param {Number} reportID
*/
function openReport(reportID) {
const sequenceNumber = getMaxSequenceNumber(reportID);
API.write('OpenReport',
{
reportID,
sequenceNumber,
},
{
optimisticData: [{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
value: {
lastReadSequenceNumber: sequenceNumber,
isLoadingReportActions: true,
lastVisitedTimestamp: Date.now(),
unreadActionCount: 0,
},
}],
successData: [{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
value: {
isLoadingReportActions: false,
},
}],
failureData: [{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
value: {
isLoadingReportActions: false,
},
}],
});
}

Expand All @@ -1024,18 +1024,24 @@ function readOldestAction(reportID, oldestActionSequenceNumber) {
{
optimisticData: [{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.IS_LOADING_MORE_REPORT_ACTIONS}${reportID}`,
value: true,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
value: {
isLoadingMoreReportActions: true,
},
}],
successData: [{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.IS_LOADING_MORE_REPORT_ACTIONS}${reportID}`,
value: false,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
value: {
isLoadingMoreReportActions: false,
},
}],
failureData: [{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.IS_LOADING_MORE_REPORT_ACTIONS}${reportID}`,
value: false,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
value: {
isLoadingMoreReportActions: false,
},
}],
});
}
Expand Down
19 changes: 8 additions & 11 deletions src/pages/home/ReportScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ const propTypes = {

/** Whether there is an outstanding amount in IOU */
hasOutstandingIOU: PropTypes.bool,

/** Flag to check if the report actions data are loading */
isLoadingReportActions: PropTypes.bool,
}),

/** Array of report actions for this report */
Expand All @@ -71,9 +74,6 @@ const propTypes = {
/** Beta features list */
betas: PropTypes.arrayOf(PropTypes.string),

/** Flag to check if the initial report actions data are loading */
isLoadingInitialReportActions: PropTypes.bool,

/** The policies which the user has access to */
policies: PropTypes.objectOf(PropTypes.shape({
/** The policy name */
Expand All @@ -99,10 +99,10 @@ const defaultProps = {
unreadActionCount: 0,
maxSequenceNumber: 0,
hasOutstandingIOU: false,
isLoadingReportActions: false,
},
isComposerFullSize: false,
betas: [],
isLoadingInitialReportActions: false,
policies: {},
};

Expand Down Expand Up @@ -146,7 +146,6 @@ class ReportScreen extends React.Component {
}

componentWillUnmount() {
clearTimeout(this.loadingTimerId);
this.removeViewportResizeListener();
}

Expand All @@ -163,11 +162,14 @@ class ReportScreen extends React.Component {

/**
* When reports change there's a brief time content is not ready to be displayed
* It Should show the loader if it's the first time we are opening the report
*
* @returns {Boolean}
*/
shouldShowLoader() {
return !getReportID(this.props.route) || (_.isEmpty(this.props.reportActions) && this.props.isLoadingInitialReportActions);
// This means there are no reportActions at all to display, but it is still in the process of loading the next set of actions.
const isLoadingInitialReportActions = _.isEmpty(this.props.reportActions) && this.props.report.isLoadingReportActions;
return !getReportID(this.props.route) || isLoadingInitialReportActions;
}

/**
Expand Down Expand Up @@ -240,7 +242,6 @@ class ReportScreen extends React.Component {
)
: (
<ReportActionsView
reportID={reportID}
reportActions={this.props.reportActions}
report={this.props.report}
session={this.props.session}
Expand Down Expand Up @@ -303,10 +304,6 @@ export default compose(
betas: {
key: ONYXKEYS.BETAS,
},
isLoadingInitialReportActions: {
key: ({route}) => `${ONYXKEYS.COLLECTION.IS_LOADING_INITIAL_REPORT_ACTIONS}${getReportID(route)}`,
initWithStoredValues: false,
},
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
},
Expand Down
Loading

0 comments on commit a4c70a6

Please sign in to comment.