-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
NewContactMethodPage.js
159 lines (142 loc) · 5.97 KB
/
NewContactMethodPage.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
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {View} from 'react-native';
import {ScrollView} from 'react-native-gesture-handler';
import {withOnyx} from 'react-native-onyx';
import {compose} from 'underscore';
import lodashGet from 'lodash/get';
import Str from 'expensify-common/lib/str';
import Button from '../../../../components/Button';
import FixedFooter from '../../../../components/FixedFooter';
import HeaderWithCloseButton from '../../../../components/HeaderWithCloseButton';
import ScreenWrapper from '../../../../components/ScreenWrapper';
import Text from '../../../../components/Text';
import TextInput from '../../../../components/TextInput';
import withLocalize, {withLocalizePropTypes} from '../../../../components/withLocalize';
import Navigation from '../../../../libs/Navigation/Navigation';
import Permissions from '../../../../libs/Permissions';
import ONYXKEYS from '../../../../ONYXKEYS';
import ROUTES from '../../../../ROUTES';
import styles from '../../../../styles/styles';
import * as User from '../../../../libs/actions/User';
import * as LoginUtils from '../../../../libs/LoginUtils';
const propTypes = {
/* Onyx Props */
/** List of betas available to current user */
betas: PropTypes.arrayOf(PropTypes.string),
/** Login list for the user that is signed in */
loginList: PropTypes.shape({
/** The partner creating the account. It depends on the source: website, mobile, integrations, ... */
partnerName: PropTypes.string,
/** Phone/Email associated with user */
partnerUserID: PropTypes.string,
/** The date when the login was validated, used to show the brickroad status */
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)),
}),
...withLocalizePropTypes,
};
const defaultProps = {
betas: [],
loginList: {},
};
class NewContactMethodPage extends Component {
constructor(props) {
super(props);
this.state = {
login: '',
password: '',
};
this.onLoginChange = this.onLoginChange.bind(this);
this.validateForm = this.validateForm.bind(this);
this.submitForm = this.submitForm.bind(this);
}
onLoginChange(login) {
this.setState({login});
}
/**
* Determine whether the form is valid
*
* @returns {Boolean}
*/
validateForm() {
const login = this.state.login.trim();
const phoneLogin = LoginUtils.getPhoneNumberWithoutSpecialChars(login);
return (Permissions.canUsePasswordlessLogins(this.props.betas) || this.state.password)
&& (Str.isValidEmail(login) || Str.isValidPhone(phoneLogin));
}
submitForm() {
// If this login already exists, just go back.
if (lodashGet(this.props.loginList, this.state.login)) {
Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHODS);
return;
}
User.addNewContactMethodAndNavigate(this.state.login, this.state.password);
}
render() {
return (
<ScreenWrapper
onTransitionEnd={() => {
if (!this.loginInputRef) {
return;
}
this.loginInputRef.focus();
}}
>
<HeaderWithCloseButton
title={this.props.translate('contacts.newContactMethod')}
shouldShowBackButton
onBackButtonPress={() => Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHODS)}
onCloseButtonPress={() => Navigation.dismissModal(true)}
/>
<ScrollView>
<Text style={[styles.ph5, styles.mb5]}>
{this.props.translate('common.pleaseEnterEmailOrPhoneNumber')}
</Text>
<View style={[styles.ph5, styles.mb6]}>
<TextInput
label={`${this.props.translate('common.email')}/${this.props.translate('common.phoneNumber')}`}
ref={el => this.loginInputRef = el}
value={this.state.login}
onChangeText={this.onLoginChange}
autoCapitalize="none"
returnKeyType={Permissions.canUsePasswordlessLogins(this.props.betas) ? 'done' : 'next'}
/>
</View>
{!Permissions.canUsePasswordlessLogins(this.props.betas)
&& (
<View style={[styles.ph5, styles.mb6]}>
<TextInput
label={this.props.translate('common.password')}
value={this.state.password}
onChangeText={password => this.setState({password})}
returnKeyType="done"
/>
</View>
)}
</ScrollView>
<FixedFooter style={[styles.flexGrow0]}>
<Button
success
isDisabled={!this.validateForm()}
text={this.props.translate('common.add')}
onPress={this.submitForm}
pressOnEnter
/>
</FixedFooter>
</ScreenWrapper>
);
}
}
NewContactMethodPage.propTypes = propTypes;
NewContactMethodPage.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
betas: {key: ONYXKEYS.BETAS},
loginList: {key: ONYXKEYS.LOGIN_LIST},
}),
)(NewContactMethodPage);