-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
BaseValidateCodeForm.js
198 lines (175 loc) · 7.71 KB
/
BaseValidateCodeForm.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
import React, {useCallback, useState, useEffect, useRef} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import lodashGet from 'lodash/get';
import MagicCodeInput from '../../../../../components/MagicCodeInput';
import * as ErrorUtils from '../../../../../libs/ErrorUtils';
import withLocalize, {withLocalizePropTypes} from '../../../../../components/withLocalize';
import ONYXKEYS from '../../../../../ONYXKEYS';
import compose from '../../../../../libs/compose';
import styles from '../../../../../styles/styles';
import OfflineWithFeedback from '../../../../../components/OfflineWithFeedback';
import * as ValidationUtils from '../../../../../libs/ValidationUtils';
import * as User from '../../../../../libs/actions/User';
import Button from '../../../../../components/Button';
import DotIndicatorMessage from '../../../../../components/DotIndicatorMessage';
import * as Session from '../../../../../libs/actions/Session';
import shouldDelayFocus from '../../../../../libs/shouldDelayFocus';
import Text from '../../../../../components/Text';
import {withNetwork} from '../../../../../components/OnyxProvider';
import PressableWithFeedback from '../../../../../components/Pressable/PressableWithFeedback';
import themeColors from '../../../../../styles/themes/default';
import * as StyleUtils from '../../../../../styles/StyleUtils';
import CONST from '../../../../../CONST';
const propTypes = {
...withLocalizePropTypes,
/** The contact method being valdiated */
contactMethod: PropTypes.string.isRequired,
/** If the magic code has been resent previously */
hasMagicCodeBeenSent: PropTypes.bool.isRequired,
/** 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)),
}).isRequired,
/* Onyx Props */
/** The details about the account that the user is signing in with */
account: PropTypes.shape({
/** Whether or not a sign on form is loading (being submitted) */
isLoading: PropTypes.bool,
}),
/** Specifies autocomplete hints for the system, so it can provide autofill */
autoComplete: PropTypes.oneOf(['sms-otp', 'one-time-code']).isRequired,
};
const defaultProps = {
account: {},
};
function BaseValidateCodeForm(props) {
const [formError, setFormError] = useState({});
const [validateCode, setValidateCode] = useState('');
const loginData = props.loginList[props.contactMethod];
const inputValidateCodeRef = useRef();
useEffect(() => {
if (!props.hasMagicCodeBeenSent) {
return;
}
setValidateCode('');
inputValidateCodeRef.current.clear();
}, [props.hasMagicCodeBeenSent]);
/**
* Request a validate code / magic code be sent to verify this contact method
*/
const resendValidateCode = () => {
User.requestContactMethodValidateCode(props.contactMethod);
setValidateCode('');
inputValidateCodeRef.current.focus();
};
/**
* Handle text input and clear formError upon text change
*
* @param {String} text
*/
const onTextInput = useCallback(
(text) => {
setValidateCode(text);
setFormError({});
if (props.account.errors) {
Session.clearAccountMessages();
}
},
[props.account.errors],
);
/**
* Check that all the form fields are valid, then trigger the submit callback
*/
const validateAndSubmitForm = useCallback(() => {
if (!validateCode.trim()) {
setFormError({validateCode: 'validateCodeForm.error.pleaseFillMagicCode'});
return;
}
if (!ValidationUtils.isValidValidateCode(validateCode)) {
setFormError({validateCode: 'validateCodeForm.error.incorrectMagicCode'});
return;
}
setFormError({});
User.validateSecondaryLogin(props.contactMethod, validateCode);
}, [validateCode, props.contactMethod]);
return (
<>
<MagicCodeInput
autoComplete={props.autoComplete}
ref={inputValidateCodeRef}
label={props.translate('common.magicCode')}
name="validateCode"
value={validateCode}
onChangeText={onTextInput}
errorText={formError.validateCode ? props.translate(formError.validateCode) : ErrorUtils.getLatestErrorMessage(props.account)}
onFulfill={validateAndSubmitForm}
autoFocus
shouldDelayFocus={shouldDelayFocus}
/>
<OfflineWithFeedback
pendingAction={lodashGet(loginData, 'pendingFields.validateCodeSent', null)}
errors={ErrorUtils.getLatestErrorField(loginData, 'validateCodeSent')}
errorRowStyles={[styles.mt2]}
onClose={() => User.clearContactMethodErrors(props.contactMethod, 'validateCodeSent')}
>
<View style={[styles.mt2, styles.dFlex, styles.flexColumn, styles.alignItemsStart]}>
<PressableWithFeedback
disabled={props.network.isOffline}
style={[styles.mr1]}
onPress={resendValidateCode}
underlayColor={themeColors.componentBG}
hoverDimmingValue={1}
pressDimmingValue={0.2}
accessibilityRole={CONST.ACCESSIBILITY_ROLE.BUTTON}
accessibilityLabel={props.translate('validateCodeForm.magicCodeNotReceived')}
>
<Text style={[StyleUtils.getDisabledLinkStyles(props.network.isOffline)]}>{props.translate('validateCodeForm.magicCodeNotReceived')}</Text>
</PressableWithFeedback>
{props.hasMagicCodeBeenSent && (
<DotIndicatorMessage
type="success"
style={[styles.mt6, styles.flex0]}
messages={{0: 'resendValidationForm.linkHasBeenResent'}}
/>
)}
</View>
</OfflineWithFeedback>
<OfflineWithFeedback
pendingAction={lodashGet(loginData, 'pendingFields.validateLogin', null)}
errors={ErrorUtils.getEarliestErrorField(loginData, 'validateLogin')}
errorRowStyles={[styles.mt2]}
onClose={() => User.clearContactMethodErrors(props.contactMethod, 'validateLogin')}
>
<Button
isDisabled={props.network.isOffline}
text={props.translate('common.verify')}
onPress={validateAndSubmitForm}
style={[styles.mt4]}
success
pressOnEnter
isLoading={props.account.isLoading}
/>
</OfflineWithFeedback>
</>
);
}
BaseValidateCodeForm.propTypes = propTypes;
BaseValidateCodeForm.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
account: {key: ONYXKEYS.ACCOUNT},
}),
withNetwork(),
)(BaseValidateCodeForm);