-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ContactMethodDetailsPage.js
257 lines (233 loc) · 10.5 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
import Str from 'expensify-common/lib/str';
import lodashGet from 'lodash/get';
import React, {Component} from 'react';
import {View, ScrollView, TouchableOpacity} 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 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';
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,
}).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: {},
route: {
params: {
contactMethod: '',
},
},
};
class ContactMethodDetailsPage extends Component {
constructor(props) {
super(props);
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.validateContactMethod = this.validateContactMethod.bind(this);
this.state = {
isDeleteModalOpen: false,
validateCode: '',
};
}
componentDidMount() {
if (this.getContactMethod()) {
return;
}
Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHODS);
}
/**
* Gets the current contact method from the route params
*
* @returns {string}
*/
getContactMethod() {
return decodeURIComponent(lodashGet(this.props.route, 'params.contactMethod'));
}
/**
* Toggle delete confirm modal visibility
* @param {Boolean} shouldOpen
*/
toggleDeleteModal(shouldOpen) {
this.setState({isDeleteModalOpen: shouldOpen});
}
/**
* Delete the contact method and hide the modal
*/
confirmDeleteAndHideModal() {
const contactMethod = this.getContactMethod();
User.deleteContactMethod(contactMethod);
this.toggleDeleteModal(false);
Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHODS);
}
/**
* Request a validate code / magic code be sent to verify this contact method
*/
resendValidateCode() {
User.requestContactMethodValidateCode(this.getContactMethod());
}
/**
* Attempt to validate this contact method
*/
validateContactMethod() {
User.validateSecondaryLogin(this.getContactMethod(), this.state.validateCode);
}
render() {
const contactMethod = this.getContactMethod();
const loginData = this.props.loginList[contactMethod];
if (!contactMethod || !loginData) {
Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHODS);
return null;
}
const isDefaultContactMethod = (this.props.session.email === loginData.partnerUserID);
const hasMagicCodeBeenSent = lodashGet(this.props.loginList, [contactMethod, 'validateCodeSent'], false);
return (
<ScreenWrapper>
<HeaderWithCloseButton
title={Str.removeSMSDomain(contactMethod)}
shouldShowBackButton
onBackButtonPress={() => Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHODS)}
onCloseButtonPress={() => Navigation.dismissModal(true)}
/>
<ScrollView>
<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
/>
{!loginData.validatedDate && (
<View style={[styles.mh5, styles.mb7]}>
<View style={[styles.flexRow, styles.alignItemsCenter, styles.mb1, styles.mt3]}>
<Icon src={Expensicons.DotIndicator} fill={colors.green} />
<View style={[styles.flex1, styles.ml4]}>
<Text>
{this.props.translate('contacts.enterMagicCode', {contactMethod})}
</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}
blurOnSubmit={false}
/>
<OfflineWithFeedback
pendingAction={lodashGet(loginData, 'pendingFields.validateCodeSent', null)}
errors={ErrorUtils.getLatestErrorField(loginData, 'validateCodeSent')}
errorRowStyles={[styles.mt2]}
onClose={() => User.clearContactMethodErrors(contactMethod, 'validateCodeSent')}
>
<TouchableOpacity
style={[styles.mt2, styles.dFlex, styles.flexRow]}
onPress={this.resendValidateCode}
>
<Text style={[styles.link, styles.mr4]}>
{this.props.translate('contacts.resendMagicCode')}
</Text>
{hasMagicCodeBeenSent && (
<Icon src={Expensicons.Checkmark} fill={colors.green} />
)}
</TouchableOpacity>
</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.validateContactMethod}
style={[styles.mt4]}
success
/>
</OfflineWithFeedback>
</View>
)}
{isDefaultContactMethod ? (
<Text style={[styles.ph5]}>
{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.toggleDeleteModal(true)}
/>
</OfflineWithFeedback>
)}
</ScrollView>
</ScreenWrapper>
);
}
}
ContactMethodDetailsPage.propTypes = propTypes;
ContactMethodDetailsPage.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
loginList: {
key: ONYXKEYS.LOGIN_LIST,
},
session: {
key: ONYXKEYS.SESSION,
},
}),
)(ContactMethodDetailsPage);