-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathProfilePage.js
executable file
·318 lines (289 loc) · 13.4 KB
/
ProfilePage.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
import lodashGet from 'lodash/get';
import React, {Component} from 'react';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
import {View} from 'react-native';
import Str from 'expensify-common/lib/str';
import moment from 'moment-timezone';
import _ from 'underscore';
import HeaderWithCloseButton from '../../../components/HeaderWithCloseButton';
import Navigation from '../../../libs/Navigation/Navigation';
import ScreenWrapper from '../../../components/ScreenWrapper';
import * as PersonalDetails from '../../../libs/actions/PersonalDetails';
import ROUTES from '../../../ROUTES';
import ONYXKEYS from '../../../ONYXKEYS';
import CONST from '../../../CONST';
import styles from '../../../styles/styles';
import Text from '../../../components/Text';
import LoginField from './LoginField';
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import * as Localize from '../../../libs/Localize';
import compose from '../../../libs/compose';
import TextInput from '../../../components/TextInput';
import Picker from '../../../components/Picker';
import CheckboxWithLabel from '../../../components/CheckboxWithLabel';
import AvatarWithImagePicker from '../../../components/AvatarWithImagePicker';
import withCurrentUserPersonalDetails, {withCurrentUserPersonalDetailsPropTypes, withCurrentUserPersonalDetailsDefaultProps} from '../../../components/withCurrentUserPersonalDetails';
import * as ValidationUtils from '../../../libs/ValidationUtils';
import * as ReportUtils from '../../../libs/ReportUtils';
import Form from '../../../components/Form';
import OfflineWithFeedback from '../../../components/OfflineWithFeedback';
const propTypes = {
/* Onyx Props */
/** Login list for the user that is signed in */
loginList: PropTypes.arrayOf(PropTypes.shape({
/** Value of partner name */
partnerName: PropTypes.string,
/** Phone/Email associated with user */
partnerUserID: PropTypes.string,
/** Date of when login was validated */
validatedDate: PropTypes.string,
})),
...withLocalizePropTypes,
...withCurrentUserPersonalDetailsPropTypes,
};
const defaultProps = {
loginList: [],
...withCurrentUserPersonalDetailsDefaultProps,
};
const timezones = _.chain(moment.tz.names())
.filter(timezone => !timezone.startsWith('Etc/GMT'))
.map(timezone => ({
value: timezone,
label: timezone,
}))
.value();
class ProfilePage extends Component {
constructor(props) {
super(props);
this.defaultAvatar = ReportUtils.getDefaultAvatar(this.props.currentUserPersonalDetails.login);
this.avatar = {uri: lodashGet(this.props.currentUserPersonalDetails, 'avatar') || this.defaultAvatar};
this.pronouns = props.currentUserPersonalDetails.pronouns;
this.state = {
logins: this.getLogins(props.loginList),
selectedTimezone: lodashGet(props.currentUserPersonalDetails.timezone, 'selected', CONST.DEFAULT_TIME_ZONE.selected),
isAutomaticTimezone: lodashGet(props.currentUserPersonalDetails.timezone, 'automatic', CONST.DEFAULT_TIME_ZONE.automatic),
hasSelfSelectedPronouns: !_.isEmpty(props.currentUserPersonalDetails.pronouns) && !props.currentUserPersonalDetails.pronouns.startsWith(CONST.PRONOUNS.PREFIX),
};
this.getLogins = this.getLogins.bind(this);
this.validate = this.validate.bind(this);
this.updatePersonalDetails = this.updatePersonalDetails.bind(this);
}
componentDidUpdate(prevProps) {
let stateToUpdate = {};
// Recalculate logins if loginList has changed
if (this.props.loginList !== prevProps.loginList) {
stateToUpdate = {...stateToUpdate, logins: this.getLogins(this.props.loginList)};
}
if (_.isEmpty(stateToUpdate)) {
return;
}
// eslint-disable-next-line react/no-did-update-set-state
this.setState(stateToUpdate);
}
/**
* Get the most validated login of each type
*
* @param {Array} loginList
* @returns {Object}
*/
getLogins(loginList) {
return _.reduce(loginList, (logins, currentLogin) => {
const type = Str.isSMSLogin(currentLogin.partnerUserID) ? CONST.LOGIN_TYPE.PHONE : CONST.LOGIN_TYPE.EMAIL;
const login = Str.removeSMSDomain(currentLogin.partnerUserID);
// If there's already a login type that's validated and/or currentLogin isn't valid then return early
if ((login !== lodashGet(this.props.currentUserPersonalDetails, 'login')) && !_.isEmpty(logins[type])
&& (logins[type].validatedDate || !currentLogin.validatedDate)) {
return logins;
}
return {
...logins,
[type]: {
...currentLogin,
type,
partnerUserID: Str.removeSMSDomain(currentLogin.partnerUserID),
},
};
}, {
phone: {},
email: {},
});
}
/**
* Submit form to update personal details
* @param {Object} values
* @param {String} values.firstName
* @param {String} values.lastName
* @param {String} values.pronouns
* @param {Boolean} values.isAutomaticTimezone
* @param {String} values.timezone
* @param {String} values.selfSelectedPronoun
*/
updatePersonalDetails(values) {
PersonalDetails.updateProfile(
values.firstName.trim(),
values.lastName.trim(),
(this.state.hasSelfSelectedPronouns) ? values.selfSelectedPronoun.trim() : values.pronouns.trim(),
{
automatic: values.isAutomaticTimezone,
selected: values.timezone,
},
);
}
/**
* @param {Object} values - An object containing the value of each inputID
* @param {String} values.firstName
* @param {String} values.lastName
* @param {String} values.pronouns
* @param {Boolean} values.isAutomaticTimezone
* @param {String} values.timezone
* @param {String} values.selfSelectedPronoun
* @returns {Object} - An object containing the errors for each inputID
*/
validate(values) {
const errors = {};
const [hasFirstNameError, hasLastNameError, hasPronounError] = ValidationUtils.doesFailCharacterLimitAfterTrim(
CONST.FORM_CHARACTER_LIMIT,
[values.firstName, values.lastName, values.pronouns],
);
const hasSelfSelectedPronouns = values.pronouns === CONST.PRONOUNS.SELF_SELECT;
this.pronouns = hasSelfSelectedPronouns ? '' : values.pronouns;
this.setState({
hasSelfSelectedPronouns,
isAutomaticTimezone: values.isAutomaticTimezone,
selectedTimezone: values.isAutomaticTimezone ? moment.tz.guess() : values.timezone,
});
if (hasFirstNameError) {
errors.firstName = Localize.translateLocal('personalDetails.error.characterLimit', {limit: CONST.FORM_CHARACTER_LIMIT});
}
if (hasLastNameError) {
errors.lastName = Localize.translateLocal('personalDetails.error.characterLimit', {limit: CONST.FORM_CHARACTER_LIMIT});
}
if (hasPronounError) {
errors.pronouns = Localize.translateLocal('personalDetails.error.characterLimit', {limit: CONST.FORM_CHARACTER_LIMIT});
}
return errors;
}
render() {
const pronounsList = _.map(this.props.translate('pronouns'), (value, key) => ({
label: value,
value: `${CONST.PRONOUNS.PREFIX}${key}`,
}));
const currentUserDetails = this.props.currentUserPersonalDetails || {};
const pronounsPickerValue = this.state.hasSelfSelectedPronouns ? CONST.PRONOUNS.SELF_SELECT : this.pronouns;
return (
<ScreenWrapper>
<HeaderWithCloseButton
title={this.props.translate('common.profile')}
shouldShowBackButton
onBackButtonPress={() => Navigation.navigate(ROUTES.SETTINGS)}
onCloseButtonPress={() => Navigation.dismissModal(true)}
/>
<Form
style={[styles.flexGrow1, styles.ph5]}
formID={CONST.PROFILE_SETTINGS_FORM}
validate={this.validate}
onSubmit={this.updatePersonalDetails}
submitButtonText={this.props.translate('common.save')}
>
<OfflineWithFeedback
pendingAction={lodashGet(this.props.currentUserPersonalDetails, 'pendingFields.avatar', null)}
errors={lodashGet(this.props.currentUserPersonalDetails, 'errorFields.avatar', null)}
errorRowStyles={[styles.mt6]}
onClose={PersonalDetails.clearAvatarErrors}
>
<AvatarWithImagePicker
isUsingDefaultAvatar={lodashGet(currentUserDetails, 'avatar', '').includes('/images/avatars/avatar')}
avatarURL={currentUserDetails.avatar}
onImageSelected={PersonalDetails.updateAvatar}
onImageRemoved={PersonalDetails.deleteAvatar}
anchorPosition={styles.createMenuPositionProfile}
size={CONST.AVATAR_SIZE.LARGE}
/>
</OfflineWithFeedback>
<Text style={[styles.mt6, styles.mb6]}>
{this.props.translate('profilePage.tellUsAboutYourself')}
</Text>
<View style={[styles.flexRow, styles.mt4, styles.mb4]}>
<View style={styles.flex1}>
<TextInput
inputID="firstName"
name="fname"
label={this.props.translate('common.firstName')}
defaultValue={lodashGet(currentUserDetails, 'firstName', '')}
placeholder={this.props.translate('profilePage.john')}
/>
</View>
<View style={[styles.flex1, styles.ml2]}>
<TextInput
inputID="lastName"
name="lname"
label={this.props.translate('common.lastName')}
defaultValue={lodashGet(currentUserDetails, 'lastName', '')}
placeholder={this.props.translate('profilePage.doe')}
/>
</View>
</View>
<View style={styles.mb6}>
<Picker
inputID="pronouns"
label={this.props.translate('profilePage.preferredPronouns')}
items={pronounsList}
placeholder={{
value: '',
label: this.props.translate('profilePage.selectYourPronouns'),
}}
defaultValue={pronounsPickerValue}
/>
{this.state.hasSelfSelectedPronouns && (
<View style={styles.mt2}>
<TextInput
inputID="selfSelectedPronoun"
defaultValue={this.pronouns}
placeholder={this.props.translate('profilePage.selfSelectYourPronoun')}
/>
</View>
)}
</View>
<LoginField
label={this.props.translate('profilePage.emailAddress')}
type="email"
login={this.state.logins.email}
defaultValue={this.state.logins.email}
/>
<LoginField
label={this.props.translate('common.phoneNumber')}
type="phone"
login={this.state.logins.phone}
defaultValue={this.state.logins.phone}
/>
<View style={styles.mb3}>
<Picker
inputID="timezone"
label={this.props.translate('profilePage.timezone')}
items={timezones}
isDisabled={this.state.isAutomaticTimezone}
defaultValue={this.state.selectedTimezone}
/>
</View>
<CheckboxWithLabel
inputID="isAutomaticTimezone"
label={this.props.translate('profilePage.setMyTimezoneAutomatically')}
defaultValue={this.state.isAutomaticTimezone}
/>
</Form>
</ScreenWrapper>
);
}
}
ProfilePage.propTypes = propTypes;
ProfilePage.defaultProps = defaultProps;
export default compose(
withLocalize,
withCurrentUserPersonalDetails,
withOnyx({
loginList: {
key: ONYXKEYS.LOGIN_LIST,
},
}),
)(ProfilePage);