-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathReportScreen.js
104 lines (88 loc) · 2.95 KB
/
ReportScreen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import React from 'react';
import PropTypes from 'prop-types';
import styles from '../../styles/styles';
import ReportView from './report/ReportView';
import ScreenWrapper from '../../components/ScreenWrapper';
import HeaderView from './HeaderView';
import Navigation from '../../libs/Navigation/Navigation';
import ROUTES from '../../ROUTES';
import FullScreenLoadingIndicator from '../../components/FullscreenLoadingIndicator';
import {updateCurrentlyViewedReportID} from '../../libs/actions/Report';
const propTypes = {
/** Navigation route context info provided by react navigation */
route: PropTypes.shape({
/** Route specific parameters used on this screen */
params: PropTypes.shape({
/** The ID of the report this screen should display */
reportID: PropTypes.string,
}).isRequired,
}).isRequired,
};
class ReportScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
};
}
componentDidMount() {
this.prepareTransition();
this.storeCurrentlyViewedReport();
}
componentDidUpdate(prevProps) {
const reportChanged = this.props.route.params.reportID !== prevProps.route.params.reportID;
if (reportChanged) {
this.prepareTransition();
this.storeCurrentlyViewedReport();
}
}
componentWillUnmount() {
clearTimeout(this.loadingTimerId);
}
/**
* Get the currently viewed report ID as number
*
* @returns {Number}
*/
getReportID() {
const params = this.props.route.params;
return Number.parseInt(params.reportID, 10);
}
/**
* When reports change there's a brief time content is not ready to be displayed
*
* @returns {Boolean}
*/
shouldShowLoader() {
return this.state.isLoading || !this.getReportID();
}
/**
* Configures a small loading transition of fixed time and proceeds with rendering available data
*/
prepareTransition() {
this.setState({isLoading: true});
clearTimeout(this.loadingTimerId);
this.loadingTimerId = setTimeout(() => this.setState({isLoading: false}), 300);
}
/**
* Persists the currently viewed report id
*/
storeCurrentlyViewedReport() {
const reportID = this.getReportID();
updateCurrentlyViewedReportID(reportID);
}
render() {
return (
<ScreenWrapper style={[styles.appContent, styles.flex1]}>
<HeaderView
reportID={this.getReportID()}
onNavigationMenuButtonClicked={() => Navigation.navigate(ROUTES.HOME)}
/>
<FullScreenLoadingIndicator visible={this.shouldShowLoader()} />
<ReportView reportID={this.getReportID()} />
</ScreenWrapper>
);
}
}
ReportScreen.propTypes = propTypes;
export default ReportScreen;