-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
NewContactMethodPage.js
149 lines (132 loc) · 5.57 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
import React, {
useCallback, useMemo, useRef, useState,
} 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: {},
};
function NewContactMethodPage(props) {
const [login, setLogin] = useState('');
const [password, setPassword] = useState('');
const loginInputRef = useRef(null);
const handleLoginChange = useCallback((value) => {
setLogin(value.trim());
}, []);
const handlePasswordChange = useCallback((value) => {
setPassword(value.trim());
}, []);
const isFormValid = useMemo(() => {
const phoneLogin = LoginUtils.getPhoneNumberWithoutSpecialChars(login);
return (Permissions.canUsePasswordlessLogins(props.betas) || password)
&& (Str.isValidEmail(login) || Str.isValidPhone(phoneLogin));
}, [login, password, props.betas]);
const submitForm = useCallback(() => {
// If this login already exists, just go back.
if (lodashGet(props.loginList, login)) {
Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHODS);
return;
}
User.addNewContactMethodAndNavigate(login, password);
}, [login, props.loginList, password]);
return (
<ScreenWrapper
onTransitionEnd={() => {
if (!loginInputRef.current) {
return;
}
loginInputRef.current.focus();
}}
>
<HeaderWithCloseButton
title={props.translate('contacts.newContactMethod')}
shouldShowBackButton
onBackButtonPress={() => Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHODS)}
onCloseButtonPress={() => Navigation.dismissModal(true)}
/>
<ScrollView>
<Text style={[styles.ph5, styles.mb5]}>
{props.translate('common.pleaseEnterEmailOrPhoneNumber')}
</Text>
<View style={[styles.ph5, styles.mb6]}>
<TextInput
label={`${props.translate('common.email')}/${props.translate('common.phoneNumber')}`}
ref={loginInputRef}
value={login}
onChangeText={handleLoginChange}
autoCapitalize="none"
returnKeyType={Permissions.canUsePasswordlessLogins(props.betas) ? 'done' : 'next'}
/>
</View>
{!Permissions.canUsePasswordlessLogins(props.betas)
&& (
<View style={[styles.ph5, styles.mb6]}>
<TextInput
label={props.translate('common.password')}
value={password}
onChangeText={handlePasswordChange}
returnKeyType="done"
/>
</View>
)}
</ScrollView>
<FixedFooter style={[styles.flexGrow0]}>
<Button
success
isDisabled={!isFormValid}
text={props.translate('common.add')}
onPress={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);