Skip to content

Commit

Permalink
refactor: ReportScreen handle initial state
Browse files Browse the repository at this point in the history
  • Loading branch information
kidroca committed Apr 3, 2021
1 parent 283e6c0 commit 4cf35fd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/components/Loading/LoadingIndicator.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable react/jsx-props-no-spreading */
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {
Expand Down
20 changes: 15 additions & 5 deletions src/pages/home/ReportScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import ONYXKEYS from '../../ONYXKEYS';

const propTypes = {
/* The ID of the report this screen should display */
reportID: PropTypes.string.isRequired,
reportID: PropTypes.string,
};

const defaultProps = {
reportID: '0',
};

class ReportScreen extends React.Component {
Expand All @@ -26,25 +30,29 @@ class ReportScreen extends React.Component {
};
}

componentDidMount() {
this.fetchReport();
}

componentDidUpdate(prevProps) {
// Reports changed, reset and load new data
if (this.props.reportID !== prevProps.reportID) {
if (this.reportID) {
this.fetchReport();
}
this.fetchReport();
}
}

// Todo: ask why getters aren't on top?
get canRenderMainContent() {
return !this.state.isLoading && !this.state.error;
return this.reportID && !this.state.isLoading && !this.state.error;
}

get reportID() {
return parseInt(this.props.reportID, 10);
}

fetchReport() {
if (!this.reportID) { return; }

console.debug('[ReportScreen] Fetch started: ');
this.setState({
isLoading: true,
Expand Down Expand Up @@ -91,6 +99,8 @@ class ReportScreen extends React.Component {

ReportScreen.displayName = 'ReportScreen';
ReportScreen.propTypes = propTypes;
ReportScreen.defaultProps = defaultProps;

export default withOnyx({
reportID: {
key: ONYXKEYS.CURRENTLY_VIEWED_REPORTID,
Expand Down

0 comments on commit 4cf35fd

Please sign in to comment.