-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathReportScreen.js
210 lines (184 loc) · 6.48 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import React from 'react';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
import {Keyboard, View} from 'react-native';
import _ from 'underscore';
import styles from '../../styles/styles';
import ScreenWrapper from '../../components/ScreenWrapper';
import HeaderView from './HeaderView';
import Navigation from '../../libs/Navigation/Navigation';
import ROUTES from '../../ROUTES';
import {handleInaccessibleReport, updateCurrentlyViewedReportID, addAction} from '../../libs/actions/Report';
import ONYXKEYS from '../../ONYXKEYS';
import ReportActionsView from './report/ReportActionsView';
import ReportActionCompose from './report/ReportActionCompose';
import KeyboardSpacer from '../../components/KeyboardSpacer';
import SwipeableView from '../../components/SwipeableView';
import CONST from '../../CONST';
import FullScreenLoadingIndicator from '../../components/FullscreenLoadingIndicator';
import ReportActionPropTypes from './report/ReportActionPropTypes';
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,
/** Tells us if the sidebar has rendered */
isSidebarLoaded: PropTypes.bool,
/** Whether or not to show the Compose Input */
session: PropTypes.shape({
shouldShowComposeInput: PropTypes.bool,
}),
/** The report currently being looked at */
report: PropTypes.shape({
/** Number of actions unread */
unreadActionCount: PropTypes.number,
/** The largest sequenceNumber on this report */
maxSequenceNumber: PropTypes.number,
/** The current position of the new marker */
newMarkerSequenceNumber: PropTypes.number,
/** Whether there is an outstanding amount in IOU */
hasOutstandingIOU: PropTypes.bool,
}),
/** Array of report actions for this report */
reportActions: PropTypes.objectOf(PropTypes.shape(ReportActionPropTypes)),
};
const defaultProps = {
isSidebarLoaded: false,
session: {
shouldShowComposeInput: true,
},
reportActions: {},
report: {
unreadActionCount: 0,
maxSequenceNumber: 0,
hasOutstandingIOU: false,
},
};
/**
* Get the currently viewed report ID as number
*
* @param {Object} route
* @param {Object} route.params
* @param {String} route.params.reportID
* @returns {Number}
*/
function getReportID(route) {
const params = route.params;
return Number.parseInt(params.reportID, 10);
}
class ReportScreen extends React.Component {
constructor(props) {
super(props);
this.onSubmitComment = this.onSubmitComment.bind(this);
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);
}
/**
* @param {String} text
*/
onSubmitComment(text) {
addAction(getReportID(this.props.route), text);
}
/**
* When reports change there's a brief time content is not ready to be displayed
*
* @returns {Boolean}
*/
shouldShowLoader() {
return this.state.isLoading || !getReportID(this.props.route);
}
/**
* Configures a small loading transition and proceeds with rendering available data
*/
prepareTransition() {
this.setState({isLoading: true});
clearTimeout(this.loadingTimerId);
this.loadingTimerId = setTimeout(() => this.setState({isLoading: false}), 0);
}
/**
* Persists the currently viewed report id
*/
storeCurrentlyViewedReport() {
const reportID = getReportID(this.props.route);
if (_.isNaN(reportID)) {
handleInaccessibleReport();
return;
}
updateCurrentlyViewedReportID(reportID);
}
render() {
if (!this.props.isSidebarLoaded) {
return null;
}
const reportID = getReportID(this.props.route);
return (
<ScreenWrapper style={[styles.appContent, styles.flex1]}>
<HeaderView
reportID={reportID}
onNavigationMenuButtonClicked={() => Navigation.navigate(ROUTES.HOME)}
/>
<View
nativeID={CONST.REPORT.DROP_NATIVE_ID}
style={[styles.flex1, styles.justifyContentEnd, styles.overflowHidden]}
>
<FullScreenLoadingIndicator visible={this.shouldShowLoader()} />
{!this.shouldShowLoader() && (
<ReportActionsView
reportID={reportID}
reportActions={this.props.reportActions}
report={this.props.report}
session={this.props.session}
/>
)}
{this.props.session.shouldShowComposeInput && (
<SwipeableView onSwipeDown={() => Keyboard.dismiss()}>
<ReportActionCompose
onSubmit={this.onSubmitComment}
reportID={reportID}
reportActions={this.props.reportActions}
report={this.props.report}
/>
</SwipeableView>
)}
<KeyboardSpacer />
</View>
</ScreenWrapper>
);
}
}
ReportScreen.propTypes = propTypes;
ReportScreen.defaultProps = defaultProps;
export default withOnyx({
isSidebarLoaded: {
key: ONYXKEYS.IS_SIDEBAR_LOADED,
},
session: {
key: ONYXKEYS.SESSION,
},
reportActions: {
key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getReportID(route)}`,
canEvict: false,
},
report: {
key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT}${getReportID(route)}`,
},
})(ReportScreen);