-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
WorkspaceInvitePage.js
355 lines (320 loc) · 13.4 KB
/
WorkspaceInvitePage.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import React from 'react';
import PropTypes from 'prop-types';
import {Pressable, View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import lodashGet from 'lodash/get';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import ScreenWrapper from '../../components/ScreenWrapper';
import HeaderWithCloseButton from '../../components/HeaderWithCloseButton';
import Navigation from '../../libs/Navigation/Navigation';
import styles from '../../styles/styles';
import compose from '../../libs/compose';
import ONYXKEYS from '../../ONYXKEYS';
import * as Policy from '../../libs/actions/Policy';
import TextInput from '../../components/TextInput';
import KeyboardAvoidingView from '../../components/KeyboardAvoidingView';
import FormAlertWithSubmitButton from '../../components/FormAlertWithSubmitButton';
import OptionsSelector from '../../components/OptionsSelector';
import * as OptionsListUtils from '../../libs/OptionsListUtils';
import CONST from '../../CONST';
import FullScreenLoadingIndicator from '../../components/FullscreenLoadingIndicator';
import * as Link from '../../libs/actions/Link';
import Text from '../../components/Text';
import withFullPolicy, {fullPolicyPropTypes, fullPolicyDefaultProps} from './withFullPolicy';
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 = {
/** Beta features list */
betas: PropTypes.arrayOf(PropTypes.string).isRequired,
/** All of the personal details for everyone */
personalDetails: PropTypes.objectOf(personalDetailsPropTypes).isRequired,
/** URL Route params */
route: PropTypes.shape({
/** Params from the URL path */
params: PropTypes.shape({
/** policyID passed via route: /workspace/:policyID/invite */
policyID: PropTypes.string,
}),
}).isRequired,
...fullPolicyPropTypes,
...withLocalizePropTypes,
};
const defaultProps = fullPolicyDefaultProps;
class WorkspaceInvitePage extends React.Component {
constructor(props) {
super(props);
this.inviteUser = this.inviteUser.bind(this);
this.clearErrors = this.clearErrors.bind(this);
this.getExcludedUsers = this.getExcludedUsers.bind(this);
this.toggleOption = this.toggleOption.bind(this);
const {
personalDetails,
userToInvite,
} = OptionsListUtils.getNewChatOptions(
[],
props.personalDetails,
props.betas,
'',
[],
this.getExcludedUsers(),
);
this.state = {
searchValue: '',
personalDetails,
selectedOptions: [],
userToInvite,
welcomeNote: this.getWelcomeNote(),
};
}
componentDidMount() {
this.clearErrors();
}
getExcludedUsers() {
const policyEmployeeList = lodashGet(this.props, 'policy.employeeList', []);
return [...CONST.EXPENSIFY_EMAILS, ...policyEmployeeList];
}
/**
* Gets the welcome note default text
*
* @returns {Object}
*/
getWelcomeNote() {
return this.props.translate('workspace.invite.welcomeNote', {
workspaceName: this.props.policy.name,
});
}
/**
* @returns {String}
*/
getErrorText() {
const errors = lodashGet(this.props.policy, 'errors', {});
if (errors.noUserSelected) {
return this.props.translate('workspace.invite.pleaseSelectUser');
}
return '';
}
/**
* @returns {Boolean}
*/
getShouldShowAlertPrompt() {
return _.size(lodashGet(this.props.policy, 'errors', {})) > 0 || lodashGet(this.props.policy, 'alertMessage', '').length > 0;
}
/**
* Returns the sections needed for the OptionsSelector
* @returns {Array}
*/
getSections() {
const sections = [];
sections.push({
title: undefined,
data: this.state.selectedOptions,
shouldShow: true,
indexOffset: 0,
});
sections.push({
title: this.props.translate('common.contacts'),
data: this.state.personalDetails,
shouldShow: !_.isEmpty(this.state.personalDetails),
indexOffset: _.reduce(sections, (prev, {data}) => prev + data.length, 0),
});
if (this.state.userToInvite) {
sections.push(({
title: undefined,
data: [this.state.userToInvite],
shouldShow: true,
indexOffset: 0,
}));
}
return sections;
}
clearErrors() {
Policy.setWorkspaceErrors(this.props.route.params.policyID, {});
Policy.hideWorkspaceAlertMessage(this.props.route.params.policyID);
}
/**
* Removes a selected option from list if already selected. If not already selected add this option to the list.
* @param {Object} option
*/
toggleOption(option) {
this.clearErrors();
this.setState((prevState) => {
const isOptionInList = _.some(prevState.selectedOptions, selectedOption => (
selectedOption.login === option.login
));
let newSelectedOptions;
if (isOptionInList) {
newSelectedOptions = _.reject(prevState.selectedOptions, selectedOption => (
selectedOption.login === option.login
));
} else {
newSelectedOptions = [...prevState.selectedOptions, option];
}
const {
personalDetails,
userToInvite,
} = OptionsListUtils.getNewChatOptions(
[],
this.props.personalDetails,
this.props.betas,
isOptionInList ? prevState.searchValue : '',
newSelectedOptions,
this.getExcludedUsers(),
);
return {
selectedOptions: newSelectedOptions,
personalDetails,
userToInvite,
searchValue: isOptionInList ? prevState.searchValue : '',
};
});
}
/**
* Handle the invite button click
*/
inviteUser() {
if (!this.validate()) {
return;
}
const logins = _.map(this.state.selectedOptions, option => option.login);
const filteredLogins = _.uniq(_.compact(_.map(logins, login => login.toLowerCase().trim())));
Policy.invite(filteredLogins, this.state.welcomeNote || this.getWelcomeNote(), this.props.route.params.policyID);
}
/**
* @returns {Boolean}
*/
validate() {
const errors = {};
if (this.state.selectedOptions.length <= 0) {
errors.noUserSelected = true;
}
Policy.setWorkspaceErrors(this.props.route.params.policyID, errors);
return _.size(errors) <= 0;
}
render() {
const sections = this.getSections();
const headerMessage = OptionsListUtils.getHeaderMessage(
this.state.personalDetails.length !== 0,
Boolean(this.state.userToInvite),
this.state.searchValue,
);
return (
<ScreenWrapper>
{({didScreenTransitionEnd}) => (
<KeyboardAvoidingView>
<HeaderWithCloseButton
title={this.props.translate('workspace.invite.invitePeople')}
onCloseButtonPress={() => {
this.clearErrors();
Navigation.dismissModal();
}}
shouldShowGetAssistanceButton
guidesCallTaskID={CONST.GUIDES_CALL_TASK_IDS.WORKSPACE_MEMBERS}
shouldShowBackButton
onBackButtonPress={() => Navigation.goBack()}
/>
<View style={[styles.flex1]}>
<FullScreenLoadingIndicator visible={!didScreenTransitionEnd} />
{didScreenTransitionEnd && (
<OptionsSelector
canSelectMultipleOptions
sections={sections}
selectedOptions={this.state.selectedOptions}
value={this.state.searchValue}
onSelectRow={this.toggleOption}
onChangeText={(searchValue = '') => {
const {
personalDetails,
userToInvite,
} = OptionsListUtils.getNewChatOptions(
[],
this.props.personalDetails,
this.props.betas,
searchValue,
[],
this.getExcludedUsers(),
);
this.setState({
searchValue,
userToInvite,
personalDetails,
});
}}
headerMessage={headerMessage}
disableArrowKeysActions
hideSectionHeaders
hideAdditionalOptionStates
forceTextUnreadStyle
/>
)}
</View>
<View style={[styles.flexShrink0]}>
<View style={[styles.ph5, styles.pv3]}>
<TextInput
label={this.props.translate('workspace.invite.personalMessagePrompt')}
autoCompleteType="off"
autoCorrect={false}
numberOfLines={4}
textAlignVertical="top"
multiline
containerStyles={[styles.workspaceInviteWelcome]}
value={this.state.welcomeNote}
onChangeText={text => this.setState({welcomeNote: text})}
/>
</View>
<FormAlertWithSubmitButton
isDisabled={!this.state.selectedOptions.length}
isAlertVisible={this.getShouldShowAlertPrompt()}
buttonText={this.props.translate('common.invite')}
onSubmit={this.inviteUser}
onFixTheErrorsLinkPressed={() => {}}
message={this.props.policy.alertMessage}
containerStyles={[styles.flexReset, styles.mb0, styles.flexGrow0, styles.flexShrink0, styles.flexBasisAuto]}
/>
<Pressable
onPress={(e) => {
e.preventDefault();
Link.openExternalLink(CONST.PRIVACY_URL);
}}
accessibilityRole="link"
href={CONST.PRIVACY_URL}
style={[styles.mh5, styles.mv2, styles.alignSelfStart]}
>
{({hovered, pressed}) => (
<View style={[styles.flexRow]}>
<Text
style={[styles.mr1, styles.label, (hovered || pressed) ? styles.linkHovered : styles.link]}
>
{this.props.translate('common.privacyPolicy')}
</Text>
</View>
)}
</Pressable>
</View>
</KeyboardAvoidingView>
)}
</ScreenWrapper>
);
}
}
WorkspaceInvitePage.propTypes = propTypes;
WorkspaceInvitePage.defaultProps = defaultProps;
export default compose(
withLocalize,
withFullPolicy,
withOnyx({
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS,
},
betas: {
key: ONYXKEYS.BETAS,
},
}),
)(WorkspaceInvitePage);