-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathSearchPage.js
executable file
·223 lines (197 loc) · 6.75 KB
/
SearchPage.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
211
212
213
214
215
216
217
218
219
220
221
222
223
import _ from 'underscore';
import React, {Component} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import OptionsSelector from '../components/OptionsSelector';
import {getSearchOptions, getHeaderMessage} from '../libs/OptionsListUtils';
import ONYXKEYS from '../ONYXKEYS';
import styles from '../styles/styles';
import KeyboardSpacer from '../components/KeyboardSpacer';
import Navigation from '../libs/Navigation/Navigation';
import ROUTES from '../ROUTES';
import withWindowDimensions, {windowDimensionsPropTypes} from '../components/withWindowDimensions';
import {fetchOrCreateChatReport} from '../libs/actions/Report';
import HeaderWithCloseButton from '../components/HeaderWithCloseButton';
import ScreenWrapper from '../components/ScreenWrapper';
import Timing from '../libs/actions/Timing';
import CONST from '../CONST';
import FullScreenLoadingIndicator from '../components/FullscreenLoadingIndicator';
import withLocalize, {withLocalizePropTypes} from '../components/withLocalize';
import compose from '../libs/compose';
const personalDetailsPropTypes = PropTypes.shape({
/** The login of the person (either email or phone number) */
login: PropTypes.string.isRequired,
/** The URL of the person's avatar (there should already be a default avatar if
the person doesn't have their own avatar uploaded yet) */
avatar: PropTypes.string.isRequired,
/** This is either the user's full name, or their login if full name is an empty string */
displayName: PropTypes.string.isRequired,
});
const propTypes = {
/* Onyx Props */
/** Beta features list */
betas: PropTypes.arrayOf(PropTypes.string).isRequired,
/** All of the personal details for everyone */
personalDetails: PropTypes.objectOf(personalDetailsPropTypes).isRequired,
/** All reports shared with the user */
reports: PropTypes.shape({
reportID: PropTypes.number,
reportName: PropTypes.string,
}).isRequired,
/** Session of currently logged in user */
session: PropTypes.shape({
email: PropTypes.string.isRequired,
}).isRequired,
/** Window Dimensions Props */
...windowDimensionsPropTypes,
...withLocalizePropTypes,
};
class SearchPage extends Component {
constructor(props) {
super(props);
Timing.start(CONST.TIMING.SEARCH_RENDER);
this.selectReport = this.selectReport.bind(this);
this.onChangeText = this.onChangeText.bind(this);
this.debouncedUpdateOptions = _.debounce(this.updateOptions.bind(this), 300);
const {
recentReports,
personalDetails,
userToInvite,
} = getSearchOptions(
props.reports,
props.personalDetails,
'',
props.betas,
);
this.state = {
searchValue: '',
recentReports,
personalDetails,
userToInvite,
};
}
componentDidMount() {
Timing.end(CONST.TIMING.SEARCH_RENDER);
}
onChangeText(searchValue = '') {
this.setState({searchValue}, this.debouncedUpdateOptions);
}
/**
* Returns the sections needed for the OptionsSelector
*
* @returns {Array}
*/
getSections() {
const sections = [{
title: this.props.translate('common.recents'),
data: this.state.recentReports.concat(this.state.personalDetails),
shouldShow: true,
indexOffset: 0,
}];
if (this.state.userToInvite) {
sections.push(({
undefined,
data: [this.state.userToInvite],
shouldShow: true,
indexOffset: 0,
}));
}
return sections;
}
updateOptions() {
const {
recentReports,
personalDetails,
userToInvite,
} = getSearchOptions(
this.props.reports,
this.props.personalDetails,
this.state.searchValue,
this.props.betas,
);
this.setState({
userToInvite,
recentReports,
personalDetails,
});
}
/**
* Reset the search value and redirect to the selected report
*
* @param {Object} option
*/
selectReport(option) {
if (!option) {
return;
}
if (option.reportID) {
this.setState({
searchValue: '',
}, () => {
Navigation.navigate(ROUTES.getReportRoute(option.reportID));
});
} else {
fetchOrCreateChatReport([
this.props.session.email,
option.login,
]);
}
}
render() {
const sections = this.getSections();
const headerMessage = getHeaderMessage(
(this.state.recentReports.length + this.state.personalDetails.length) !== 0,
Boolean(this.state.userToInvite),
this.state.searchValue,
);
return (
<ScreenWrapper>
{({didScreenTransitionEnd}) => (
<>
<HeaderWithCloseButton
title={this.props.translate('common.search')}
onCloseButtonPress={() => Navigation.dismissModal(true)}
/>
<View style={[styles.flex1, styles.w100, styles.pRelative]}>
<FullScreenLoadingIndicator visible={!didScreenTransitionEnd} />
{didScreenTransitionEnd && (
<OptionsSelector
sections={sections}
value={this.state.searchValue}
onSelectRow={this.selectReport}
onChangeText={this.onChangeText}
headerMessage={headerMessage}
hideSectionHeaders
hideAdditionalOptionStates
showTitleTooltip
/>
)}
</View>
<KeyboardSpacer />
</>
)}
</ScreenWrapper>
);
}
}
SearchPage.propTypes = propTypes;
SearchPage.displayName = 'SearchPage';
export default compose(
withLocalize,
withWindowDimensions,
withOnyx({
reports: {
key: ONYXKEYS.COLLECTION.REPORT,
},
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS,
},
session: {
key: ONYXKEYS.SESSION,
},
betas: {
key: ONYXKEYS.BETAS,
},
}),
)(SearchPage);