-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
IOUParticipantsSplit.js
executable file
·259 lines (229 loc) · 8.29 KB
/
IOUParticipantsSplit.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import React, {Component} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import _ from 'underscore';
import {withOnyx} from 'react-native-onyx';
import ONYXKEYS from '../../../../ONYXKEYS';
import styles from '../../../../styles/styles';
import OptionsSelector from '../../../../components/OptionsSelector';
import * as OptionsListUtils from '../../../../libs/OptionsListUtils';
import CONST from '../../../../CONST';
import withLocalize, {withLocalizePropTypes} from '../../../../components/withLocalize';
import compose from '../../../../libs/compose';
import Text from '../../../../components/Text';
import personalDetailsPropType from '../../../personalDetailsPropType';
import reportPropTypes from '../../../reportPropTypes';
const propTypes = {
/** Beta features list */
betas: PropTypes.arrayOf(PropTypes.string).isRequired,
/** Callback to inform parent modal of success */
onStepComplete: PropTypes.func.isRequired,
/** Callback to add participants in IOUModal */
onAddParticipants: PropTypes.func.isRequired,
/** Selected participants from IOUModal with login */
participants: PropTypes.arrayOf(PropTypes.shape({
login: PropTypes.string.isRequired,
alternateText: PropTypes.string,
hasDraftComment: PropTypes.bool,
icons: PropTypes.arrayOf(PropTypes.string),
searchText: PropTypes.string,
text: PropTypes.string,
keyForList: PropTypes.string,
reportID: PropTypes.string,
})),
/** All of the personal details for everyone */
personalDetails: PropTypes.objectOf(personalDetailsPropType).isRequired,
/** All reports shared with the user */
reports: PropTypes.objectOf(reportPropTypes).isRequired,
/** padding bottom style of safe area */
safeAreaPaddingBottomStyle: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.object),
PropTypes.object,
]),
...withLocalizePropTypes,
};
const defaultProps = {
participants: [],
safeAreaPaddingBottomStyle: {},
};
class IOUParticipantsSplit extends Component {
constructor(props) {
super(props);
this.toggleOption = this.toggleOption.bind(this);
this.finalizeParticipants = this.finalizeParticipants.bind(this);
this.updateOptionsWithSearchTerm = this.updateOptionsWithSearchTerm.bind(this);
const {
recentReports,
personalDetails,
userToInvite,
} = OptionsListUtils.getNewChatOptions(
props.reports,
props.personalDetails,
props.betas,
'',
props.participants,
CONST.EXPENSIFY_EMAILS,
);
this.state = {
searchTerm: '',
recentReports,
personalDetails,
userToInvite,
};
}
/**
* Returns the sections needed for the OptionsSelector
*
* @param {Boolean} maxParticipantsReached
* @returns {Array}
*/
getSections(maxParticipantsReached) {
const sections = [];
let indexOffset = 0;
sections.push({
title: undefined,
data: this.props.participants,
shouldShow: true,
indexOffset,
});
indexOffset += this.props.participants.length;
if (maxParticipantsReached) {
return sections;
}
sections.push({
title: this.props.translate('common.recents'),
data: this.state.recentReports,
shouldShow: !_.isEmpty(this.state.recentReports),
indexOffset,
});
indexOffset += this.state.recentReports.length;
sections.push({
title: this.props.translate('common.contacts'),
data: this.state.personalDetails,
shouldShow: !_.isEmpty(this.state.personalDetails),
indexOffset,
});
indexOffset += this.state.personalDetails.length;
if (this.state.userToInvite && !OptionsListUtils.isCurrentUser(this.state.userToInvite)) {
sections.push(({
undefined,
data: [this.state.userToInvite],
shouldShow: true,
indexOffset,
}));
}
return sections;
}
updateOptionsWithSearchTerm(searchTerm = '') {
const {
recentReports,
personalDetails,
userToInvite,
} = OptionsListUtils.getNewChatOptions(
this.props.reports,
this.props.personalDetails,
this.props.betas,
searchTerm,
this.props.participants,
CONST.EXPENSIFY_EMAILS,
);
this.setState({
searchTerm,
userToInvite,
recentReports,
personalDetails,
});
}
/**
* Once a single or more users are selected, navigates to next step
*/
finalizeParticipants() {
this.props.onStepComplete();
}
/**
* Removes a selected option from list if already selected. If not already selected add this option to the list.
* @param {Object} option
*/
toggleOption(option) {
const isOptionInList = _.some(this.props.participants, selectedOption => (
selectedOption.login === option.login
));
let newSelectedOptions;
if (isOptionInList) {
newSelectedOptions = _.reject(this.props.participants, selectedOption => (
selectedOption.login === option.login
));
} else {
newSelectedOptions = [...this.props.participants, option];
}
this.props.onAddParticipants(newSelectedOptions);
this.setState((prevState) => {
const {
recentReports,
personalDetails,
userToInvite,
} = OptionsListUtils.getNewChatOptions(
this.props.reports,
this.props.personalDetails,
this.props.betas,
isOptionInList ? prevState.searchTerm : '',
newSelectedOptions,
CONST.EXPENSIFY_EMAILS,
);
return {
recentReports,
personalDetails,
userToInvite,
searchTerm: isOptionInList ? prevState.searchTerm : '',
};
});
}
render() {
const maxParticipantsReached = this.props.participants.length === CONST.REPORT.MAXIMUM_PARTICIPANTS;
const sections = this.getSections(maxParticipantsReached);
const headerMessage = OptionsListUtils.getHeaderMessage(
this.state.personalDetails.length + this.state.recentReports.length !== 0,
Boolean(this.state.userToInvite),
this.state.searchTerm,
maxParticipantsReached,
);
return (
<View style={[styles.flex1, styles.w100, (this.props.participants.length > 0 ? this.props.safeAreaPaddingBottomStyle : {})]}>
<Text style={[styles.textLabelSupporting, styles.pt3, styles.ph5]}>
{this.props.translate('common.to')}
</Text>
<OptionsSelector
canSelectMultipleOptions
sections={sections}
selectedOptions={this.props.participants}
value={this.state.searchTerm}
onSelectRow={this.toggleOption}
onChangeText={this.updateOptionsWithSearchTerm}
headerMessage={headerMessage}
boldStyle
shouldShowConfirmButton
confirmButtonText={this.props.translate('common.next')}
onConfirmSelection={this.finalizeParticipants}
placeholderText={this.props.translate('optionsSelector.nameEmailOrPhoneNumber')}
safeAreaPaddingBottomStyle={this.props.safeAreaPaddingBottomStyle}
/>
</View>
);
}
}
IOUParticipantsSplit.propTypes = propTypes;
IOUParticipantsSplit.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS,
},
reports: {
key: ONYXKEYS.COLLECTION.REPORT,
},
betas: {
key: ONYXKEYS.BETAS,
},
}),
)(IOUParticipantsSplit);