-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ContactMethodDetailsPage.js
283 lines (257 loc) · 12 KB
/
ContactMethodDetailsPage.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
import Str from 'expensify-common/lib/str';
import lodashGet from 'lodash/get';
import _ from 'underscore';
import React, {Component} from 'react';
import {View, ScrollView} from 'react-native';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import Navigation from '../../../../libs/Navigation/Navigation';
import ROUTES from '../../../../ROUTES';
import ScreenWrapper from '../../../../components/ScreenWrapper';
import HeaderWithCloseButton from '../../../../components/HeaderWithCloseButton';
import compose from '../../../../libs/compose';
import ONYXKEYS from '../../../../ONYXKEYS';
import withLocalize, {withLocalizePropTypes} from '../../../../components/withLocalize';
import MenuItem from '../../../../components/MenuItem';
import styles from '../../../../styles/styles';
import * as Expensicons from '../../../../components/Icon/Expensicons';
import Text from '../../../../components/Text';
import OfflineWithFeedback from '../../../../components/OfflineWithFeedback';
import DotIndicatorMessage from '../../../../components/DotIndicatorMessage';
import ConfirmModal from '../../../../components/ConfirmModal';
import * as User from '../../../../libs/actions/User';
import TextInput from '../../../../components/TextInput';
import CONST from '../../../../CONST';
import Icon from '../../../../components/Icon';
import colors from '../../../../styles/colors';
import Button from '../../../../components/Button';
import * as ErrorUtils from '../../../../libs/ErrorUtils';
import themeColors from '../../../../styles/themes/default';
import NotFoundPage from '../../../ErrorPage/NotFoundPage';
import * as ValidationUtils from '../../../../libs/ValidationUtils';
const propTypes = {
/* Onyx Props */
/** Login list for the user that is signed in */
loginList: PropTypes.shape({
/** Value of partner name */
partnerName: PropTypes.string,
/** Phone/Email associated with user */
partnerUserID: PropTypes.string,
/** Date when login was validated */
validatedDate: PropTypes.string,
/** Field-specific server side errors keyed by microtime */
errorFields: PropTypes.objectOf(PropTypes.objectOf(PropTypes.string)),
/** Field-specific pending states for offline UI status */
pendingFields: PropTypes.objectOf(PropTypes.objectOf(PropTypes.string)),
}),
/** Current user session */
session: PropTypes.shape({
email: PropTypes.string.isRequired,
}),
/** Route params */
route: PropTypes.shape({
params: PropTypes.shape({
/** Passed via route /settings/profile/contact-methods/:contactMethod/details */
contactMethod: PropTypes.string,
}),
}),
...withLocalizePropTypes,
};
const defaultProps = {
loginList: {},
session: {
email: null,
},
route: {
params: {
contactMethod: '',
},
},
};
class ContactMethodDetailsPage extends Component {
constructor(props) {
super(props);
this.deleteContactMethod = this.deleteContactMethod.bind(this);
this.toggleDeleteModal = this.toggleDeleteModal.bind(this);
this.confirmDeleteAndHideModal = this.confirmDeleteAndHideModal.bind(this);
this.resendValidateCode = this.resendValidateCode.bind(this);
this.getContactMethod = this.getContactMethod.bind(this);
this.validateAndSubmitCode = this.validateAndSubmitCode.bind(this);
this.state = {
formError: '',
isDeleteModalOpen: false,
validateCode: '',
};
}
/**
* Gets the current contact method from the route params
*
* @returns {string}
*/
getContactMethod() {
return decodeURIComponent(lodashGet(this.props.route, 'params.contactMethod'));
}
/**
* Deletes the contact method if it has errors. Otherwise, it shows the confirmation alert and deletes it only if the user confirms.
*/
deleteContactMethod() {
if (!_.isEmpty(lodashGet(this.props.loginList, [this.getContactMethod(), 'errorFields'], {}))) {
User.deleteContactMethod(this.getContactMethod(), this.props.loginList);
return;
}
this.toggleDeleteModal(true);
}
/**
* Toggle delete confirm modal visibility
* @param {Boolean} isOpen
*/
toggleDeleteModal(isOpen) {
this.setState({isDeleteModalOpen: isOpen});
}
/**
* Delete the contact method and hide the modal
*/
confirmDeleteAndHideModal() {
this.toggleDeleteModal(false);
User.deleteContactMethod(this.getContactMethod(), this.props.loginList);
}
/**
* Request a validate code / magic code be sent to verify this contact method
*/
resendValidateCode() {
User.requestContactMethodValidateCode(this.getContactMethod());
}
/**
* Attempt to validate this contact method
*/
validateAndSubmitCode() {
if (!this.state.validateCode) {
this.setState({formError: 'validateCodeForm.error.pleaseFillMagicCode'});
} else if (!ValidationUtils.isValidValidateCode(this.state.validateCode)) {
this.setState({formError: 'validateCodeForm.error.incorrectMagicCode'});
} else {
this.setState({formError: ''});
User.validateSecondaryLogin(this.getContactMethod(), this.state.validateCode);
}
}
render() {
const contactMethod = this.getContactMethod();
// replacing spaces with "hard spaces" to prevent breaking the number
const formattedContactMethod = Str.isSMSLogin(contactMethod)
? this.props.formatPhoneNumber(contactMethod).replace(/ /g, '\u00A0')
: contactMethod;
const loginData = this.props.loginList[contactMethod];
if (!contactMethod || !loginData) {
return <NotFoundPage />;
}
const isDefaultContactMethod = this.props.session.email === loginData.partnerUserID;
const hasMagicCodeBeenSent = lodashGet(this.props.loginList, [contactMethod, 'validateCodeSent'], false);
const formErrorText = this.state.formError ? this.props.translate(this.state.formError) : '';
const isFailedAddContactMethod = Boolean(lodashGet(loginData, 'errorFields.addedLogin'));
return (
<ScreenWrapper>
<HeaderWithCloseButton
title={formattedContactMethod}
shouldShowBackButton
onBackButtonPress={() => Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHODS)}
onCloseButtonPress={() => Navigation.dismissModal(true)}
/>
<ScrollView keyboardShouldPersistTaps="handled">
<ConfirmModal
title={this.props.translate('contacts.removeContactMethod')}
onConfirm={this.confirmDeleteAndHideModal}
onCancel={() => this.toggleDeleteModal(false)}
prompt={this.props.translate('contacts.removeAreYouSure')}
confirmText={this.props.translate('common.yesContinue')}
isVisible={this.state.isDeleteModalOpen}
danger
/>
{isFailedAddContactMethod && <DotIndicatorMessage style={[styles.mh5]} messages={ErrorUtils.getLatestErrorField(loginData, 'addedLogin')} type="error" />}
{!loginData.validatedDate && !isFailedAddContactMethod && (
<View style={[styles.ph5, styles.mt3, styles.mb7]}>
<View style={[styles.flexRow, styles.alignItemsCenter, styles.mb1]}>
<Icon src={Expensicons.DotIndicator} fill={colors.green} />
<View style={[styles.flex1, styles.ml4]}>
<Text>
{this.props.translate('contacts.enterMagicCode', {contactMethod: formattedContactMethod})}
</Text>
</View>
</View>
<TextInput
label={this.props.translate('common.magicCode')}
name="validateCode"
value={this.state.validateCode}
onChangeText={text => this.setState({validateCode: text})}
keyboardType={CONST.KEYBOARD_TYPE.NUMBER_PAD}
errorText={formErrorText}
/>
<OfflineWithFeedback
pendingAction={lodashGet(loginData, 'pendingFields.validateCodeSent', null)}
errors={ErrorUtils.getLatestErrorField(loginData, 'validateCodeSent')}
errorRowStyles={[styles.mt2]}
onClose={() => User.clearContactMethodErrors(contactMethod, 'validateCodeSent')}
>
<View
style={[styles.mt2, styles.dFlex, styles.flexRow]}
>
<Text style={[styles.link, styles.mr1]} onPress={this.resendValidateCode}>
{this.props.translate('contacts.resendMagicCode')}
</Text>
{hasMagicCodeBeenSent && (
<Icon src={Expensicons.Checkmark} fill={colors.green} />
)}
</View>
</OfflineWithFeedback>
<OfflineWithFeedback
pendingAction={lodashGet(loginData, 'pendingFields.validateLogin', null)}
errors={ErrorUtils.getLatestErrorField(loginData, 'validateLogin')}
errorRowStyles={[styles.mt2]}
onClose={() => User.clearContactMethodErrors(contactMethod, 'validateLogin')}
>
<Button
text={this.props.translate('common.verify')}
onPress={this.validateAndSubmitCode}
style={[styles.mt4]}
success
pressOnEnter
/>
</OfflineWithFeedback>
</View>
)}
{isDefaultContactMethod ? (
<Text style={[styles.ph5, styles.mv3]}>
{this.props.translate('contacts.yourDefaultContactMethod')}
</Text>
) : (
<OfflineWithFeedback
pendingAction={lodashGet(loginData, 'pendingFields.deletedLogin', null)}
errors={ErrorUtils.getLatestErrorField(loginData, 'deletedLogin')}
errorRowStyles={[styles.mt6]}
onClose={() => User.clearContactMethodErrors(contactMethod, 'deletedLogin')}
>
<MenuItem
title={this.props.translate('common.remove')}
icon={Expensicons.Trashcan}
iconFill={themeColors.danger}
onPress={this.deleteContactMethod}
/>
</OfflineWithFeedback>
)}
</ScrollView>
</ScreenWrapper>
);
}
}
ContactMethodDetailsPage.propTypes = propTypes;
ContactMethodDetailsPage.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
loginList: {
key: ONYXKEYS.LOGIN_LIST,
},
session: {
key: ONYXKEYS.SESSION,
},
}),
)(ContactMethodDetailsPage);