-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathProfilePage.js
executable file
·312 lines (286 loc) · 12.7 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
import React, {Component} from 'react';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
import {
View,
TextInput,
ScrollView,
} 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 {setPersonalDetails, setAvatar, deleteAvatar} 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 themeColors from '../../../styles/themes/default';
import LoginField from './LoginField';
import Picker from '../../../components/Picker';
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import compose from '../../../libs/compose';
import Button from '../../../components/Button';
import KeyboardAvoidingView from '../../../components/KeyboardAvoidingView';
import FixedFooter from '../../../components/FixedFooter';
import Growl from '../../../libs/Growl';
import FullNameInputRow from '../../../components/FullNameInputRow';
import CheckboxWithLabel from '../../../components/CheckboxWithLabel';
import AvatarWithImagePicker from '../../../components/AvatarWithImagePicker';
import currentUserPersonalDetailsPropsTypes from './currentUserPersonalDetailsPropsTypes';
const propTypes = {
/* Onyx Props */
/** The personal details of the person who is logged in */
myPersonalDetails: PropTypes.shape(currentUserPersonalDetailsPropsTypes),
/** The details about the user that is signed in */
user: PropTypes.shape({
/** Whether or not the user is subscribed to news updates */
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,
};
const defaultProps = {
myPersonalDetails: {},
user: {
loginList: [],
},
};
const timezones = moment.tz.names()
.map(timezone => ({
value: timezone,
label: timezone,
}));
class ProfilePage extends Component {
constructor(props) {
super(props);
const {
firstName,
lastName,
pronouns,
timezone = {},
} = props.myPersonalDetails;
const pronounsList = Object.values(this.props.translate('pronouns'));
let currentUserPronouns = pronouns;
let initialSelfSelectedPronouns = '';
// This handles populating the self-selected pronouns in the form
if (pronouns && !pronounsList.includes(pronouns)) {
currentUserPronouns = this.props.translate('pronouns.selfSelect');
initialSelfSelectedPronouns = pronouns;
}
this.state = {
firstName,
lastName,
pronouns: currentUserPronouns,
selfSelectedPronouns: initialSelfSelectedPronouns,
selectedTimezone: timezone.selected || CONST.DEFAULT_TIME_ZONE.selected,
isAutomaticTimezone: timezone.automatic ?? CONST.DEFAULT_TIME_ZONE.automatic,
logins: this.getLogins(props.user.loginList),
};
this.pronounDropdownValues = pronounsList.map(pronoun => ({value: pronoun, label: pronoun}));
this.updatePersonalDetails = this.updatePersonalDetails.bind(this);
this.setAutomaticTimezone = this.setAutomaticTimezone.bind(this);
this.getLogins = this.getLogins.bind(this);
}
componentDidUpdate(prevProps) {
// Recalculate logins if loginList has changed
if (this.props.user.loginList !== prevProps.user.loginList) {
// eslint-disable-next-line react/no-did-update-set-state
this.setState({
logins: this.getLogins(this.props.user.loginList),
});
}
}
/**
* Set the form to use automatic timezone
*
* @param {Boolean} isAutomaticTimezone
*/
setAutomaticTimezone(isAutomaticTimezone) {
this.setState(({selectedTimezone}) => ({
isAutomaticTimezone,
selectedTimezone: isAutomaticTimezone ? moment.tz.guess() : selectedTimezone,
}));
}
/**
* Get the most validated login of each type
*
* @param {Array} loginList
* @returns {Object}
*/
getLogins(loginList) {
return loginList.reduce((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 !== this.props.myPersonalDetails.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
*/
updatePersonalDetails() {
const {
firstName,
lastName,
pronouns,
selfSelectedPronouns,
selectedTimezone,
isAutomaticTimezone,
} = this.state;
setPersonalDetails({
firstName,
lastName,
pronouns: pronouns === this.props.translate('pronouns.selfSelect')
? selfSelectedPronouns
: pronouns,
timezone: {
automatic: isAutomaticTimezone,
selected: selectedTimezone,
},
});
Growl.show(this.props.translate('profilePage.growlMessageOnSave'), CONST.GROWL.SUCCESS, 3000);
}
render() {
// Determines if the pronouns/selected pronouns have changed
const arePronounsUnchanged = this.props.myPersonalDetails.pronouns === this.state.pronouns
|| (this.props.myPersonalDetails.pronouns
&& this.props.myPersonalDetails.pronouns === this.state.selfSelectedPronouns);
// Disables button if none of the form values have changed
const isButtonDisabled = (this.props.myPersonalDetails.firstName === this.state.firstName)
&& (this.props.myPersonalDetails.lastName === this.state.lastName)
&& (this.props.myPersonalDetails.timezone.selected === this.state.selectedTimezone)
&& (this.props.myPersonalDetails.timezone.automatic === this.state.isAutomaticTimezone)
&& arePronounsUnchanged;
return (
<ScreenWrapper>
<KeyboardAvoidingView>
<HeaderWithCloseButton
title={this.props.translate('common.profile')}
shouldShowBackButton
onBackButtonPress={() => Navigation.navigate(ROUTES.SETTINGS)}
onCloseButtonPress={() => Navigation.dismissModal(true)}
/>
<ScrollView style={styles.flex1} contentContainerStyle={styles.p5}>
<AvatarWithImagePicker
avatarURL={this.props.myPersonalDetails.avatar}
onImageSelected={setAvatar}
onImageRemoved={() => deleteAvatar(this.props.myPersonalDetails.login)}
// eslint-disable-next-line max-len
isUsingDefaultAvatar={this.props.myPersonalDetails.avatar.includes('/images/avatars/avatar')}
anchorPosition={styles.createMenuPositionProfile}
/>
<Text style={[styles.mt6, styles.mb6]}>
{this.props.translate('profilePage.tellUsAboutYourself')}
</Text>
<FullNameInputRow
firstName={this.state.firstName}
lastName={this.state.lastName}
onChangeFirstName={firstName => this.setState({firstName})}
onChangeLastName={lastName => this.setState({lastName})}
style={[styles.mt4, styles.mb4]}
/>
<View style={styles.mb6}>
<Text style={[styles.mb1, styles.formLabel]}>
{this.props.translate('profilePage.preferredPronouns')}
</Text>
<View style={styles.mb1}>
<Picker
onChange={pronouns => this.setState({pronouns, selfSelectedPronouns: ''})}
items={this.pronounDropdownValues}
placeholder={{
value: '',
label: this.props.translate('profilePage.selectYourPronouns'),
}}
value={this.state.pronouns}
/>
</View>
{this.state.pronouns === this.props.translate('pronouns.selfSelect') && (
<TextInput
style={styles.textInput}
value={this.state.selfSelectedPronouns}
onChangeText={selfSelectedPronouns => this.setState({selfSelectedPronouns})}
placeholder={this.props.translate('profilePage.selfSelectYourPronoun')}
placeholderTextColor={themeColors.placeholderText}
/>
)}
</View>
<LoginField
label={this.props.translate('profilePage.emailAddress')}
type="email"
login={this.state.logins.email}
/>
<LoginField
label={this.props.translate('common.phoneNumber')}
type="phone"
login={this.state.logins.phone}
/>
<View style={styles.mb3}>
<Text style={[styles.mb1, styles.formLabel]}>
{this.props.translate('profilePage.timezone')}
</Text>
<Picker
onChange={selectedTimezone => this.setState({selectedTimezone})}
items={timezones}
useDisabledStyles={this.state.isAutomaticTimezone}
value={this.state.selectedTimezone}
disabled={this.state.isAutomaticTimezone}
/>
</View>
<CheckboxWithLabel
label={this.props.translate('profilePage.setMyTimezoneAutomatically')}
isChecked={this.state.isAutomaticTimezone}
onPress={this.setAutomaticTimezone}
/>
</ScrollView>
<FixedFooter>
<Button
success
isDisabled={isButtonDisabled}
onPress={this.updatePersonalDetails}
style={[styles.w100]}
text={this.props.translate('common.save')}
/>
</FixedFooter>
</KeyboardAvoidingView>
</ScreenWrapper>
);
}
}
ProfilePage.propTypes = propTypes;
ProfilePage.defaultProps = defaultProps;
ProfilePage.displayName = 'ProfilePage';
export default compose(
withLocalize,
withOnyx({
myPersonalDetails: {
key: ONYXKEYS.MY_PERSONAL_DETAILS,
},
user: {
key: ONYXKEYS.USER,
},
}),
)(ProfilePage);