-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathBaseValidateCodeForm.tsx
281 lines (252 loc) · 11.2 KB
/
BaseValidateCodeForm.tsx
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
import {useFocusEffect} from '@react-navigation/native';
import type {ForwardedRef} from 'react';
import React, {useCallback, useEffect, useImperativeHandle, useRef, useState} from 'react';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import Button from '@components/Button';
import DotIndicatorMessage from '@components/DotIndicatorMessage';
import MagicCodeInput from '@components/MagicCodeInput';
import type {AutoCompleteVariant, MagicCodeInputHandle} from '@components/MagicCodeInput';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import PressableWithFeedback from '@components/Pressable/PressableWithFeedback';
import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ErrorUtils from '@libs/ErrorUtils';
import * as ValidationUtils from '@libs/ValidationUtils';
import * as Session from '@userActions/Session';
import * as User from '@userActions/User';
import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Account, LoginList, PendingContactAction} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
type ValidateCodeFormHandle = {
focus: () => void;
focusLastSelected: () => void;
};
type ValidateCodeFormError = {
validateCode?: TranslationPaths;
};
type BaseValidateCodeFormOnyxProps = {
/** The details about the account that the user is signing in with */
account: OnyxEntry<Account>;
};
type ValidateCodeFormProps = {
/** The contact method being valdiated */
contactMethod: string;
/** If the magic code has been resent previously */
hasMagicCodeBeenSent?: boolean;
/** Login list for the user that is signed in */
loginList?: LoginList;
/** Specifies autocomplete hints for the system, so it can provide autofill */
autoComplete?: AutoCompleteVariant;
/** Forwarded inner ref */
innerRef?: ForwardedRef<ValidateCodeFormHandle>;
/** Whether we are validating the action taken to add the magic code */
isValidatingAction?: boolean;
/** The contact that's going to be added after successful validation */
pendingContact?: PendingContactAction;
};
type BaseValidateCodeFormProps = BaseValidateCodeFormOnyxProps & ValidateCodeFormProps;
function BaseValidateCodeForm({
account = {},
contactMethod,
hasMagicCodeBeenSent,
loginList,
autoComplete = 'one-time-code',
innerRef = () => {},
isValidatingAction = false,
pendingContact,
}: BaseValidateCodeFormProps) {
const {translate} = useLocalize();
const {isOffline} = useNetwork();
const theme = useTheme();
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const [formError, setFormError] = useState<ValidateCodeFormError>({});
const [validateCode, setValidateCode] = useState('');
const loginData = loginList?.[pendingContact?.contactMethod ?? contactMethod];
const inputValidateCodeRef = useRef<MagicCodeInputHandle>(null);
const validateLoginError = ErrorUtils.getEarliestErrorField(loginData, 'validateLogin');
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- nullish coalescing doesn't achieve the same result in this case
const shouldDisableResendValidateCode = !!isOffline || account?.isLoading;
const focusTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const validateCodeSentIsPressedRef = useRef(false);
const [showDotIndicator, setShowDotIndicator] = useState(false);
useImperativeHandle(innerRef, () => ({
focus() {
inputValidateCodeRef.current?.focus();
},
focusLastSelected() {
if (!inputValidateCodeRef.current) {
return;
}
if (focusTimeoutRef.current) {
clearTimeout(focusTimeoutRef.current);
}
focusTimeoutRef.current = setTimeout(() => {
inputValidateCodeRef.current?.focusLastSelected();
}, CONST.ANIMATED_TRANSITION);
},
}));
useFocusEffect(
useCallback(() => {
if (!inputValidateCodeRef.current) {
return;
}
if (focusTimeoutRef.current) {
clearTimeout(focusTimeoutRef.current);
}
focusTimeoutRef.current = setTimeout(() => {
inputValidateCodeRef.current?.focusLastSelected();
}, CONST.ANIMATED_TRANSITION);
return () => {
if (!focusTimeoutRef.current) {
return;
}
clearTimeout(focusTimeoutRef.current);
};
}, []),
);
useEffect(() => {
Session.clearAccountMessages();
if (!validateLoginError) {
return;
}
User.clearContactMethodErrors(contactMethod, 'validateLogin');
// contactMethod is not added as a dependency since it does not change between renders
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, []);
useEffect(() => {
if (!hasMagicCodeBeenSent) {
return;
}
inputValidateCodeRef.current?.clear();
}, [hasMagicCodeBeenSent]);
useEffect(() => {
// `validateCodeSent` is updated asynchronously,
// and `validateCodeSentIsPressedRef.current` is updated faster than `hasMagicCodeBeenSent`.
// This can cause the component to hide and show the `DotIndicatorMessage` multiple times
// in quick succession, leading to a flickering effect.
if ((hasMagicCodeBeenSent ?? !!pendingContact?.validateCodeSent) && validateCodeSentIsPressedRef.current) {
setShowDotIndicator(true);
} else {
setShowDotIndicator(false);
}
}, [hasMagicCodeBeenSent, pendingContact, validateCodeSentIsPressedRef]);
/**
* Request a validate code / magic code be sent to verify this contact method
*/
const resendValidateCode = () => {
if (!!pendingContact?.contactMethod && isValidatingAction) {
User.saveNewContactMethodAndRequestValidationCode(pendingContact?.contactMethod);
} else {
User.requestContactMethodValidateCode(contactMethod);
}
inputValidateCodeRef.current?.clear();
validateCodeSentIsPressedRef.current = true;
};
/**
* Handle text input and clear formError upon text change
*/
const onTextInput = useCallback(
(text: string) => {
setValidateCode(text);
setFormError({});
if (validateLoginError) {
User.clearContactMethodErrors(contactMethod, 'validateLogin');
}
},
[validateLoginError, contactMethod],
);
/**
* 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({});
if (!!pendingContact?.contactMethod && isValidatingAction) {
User.addNewContactMethod(pendingContact?.contactMethod, validateCode);
return;
}
User.validateSecondaryLogin(loginList, contactMethod, validateCode);
}, [loginList, validateCode, contactMethod, isValidatingAction, pendingContact?.contactMethod]);
return (
<>
<MagicCodeInput
autoComplete={autoComplete}
ref={inputValidateCodeRef}
name="validateCode"
value={validateCode}
onChangeText={onTextInput}
errorText={formError?.validateCode ? translate(formError?.validateCode) : ErrorUtils.getLatestErrorMessage(account ?? {})}
hasError={!isEmptyObject(validateLoginError)}
onFulfill={validateAndSubmitForm}
autoFocus={false}
/>
<OfflineWithFeedback
pendingAction={pendingContact?.pendingFields?.validateCodeSent ?? loginData?.pendingFields?.validateCodeSent}
errors={ErrorUtils.getLatestErrorField(pendingContact ?? loginData, pendingContact ? 'actionVerified' : 'validateCodeSent')}
errorRowStyles={[styles.mt2]}
onClose={() => User.clearContactMethodErrors(contactMethod, 'validateCodeSent')}
>
<View style={[styles.mt2, styles.dFlex, styles.flexColumn, styles.alignItemsStart]}>
<PressableWithFeedback
disabled={shouldDisableResendValidateCode}
style={[styles.mr1]}
onPress={resendValidateCode}
underlayColor={theme.componentBG}
hoverDimmingValue={1}
pressDimmingValue={0.2}
role={CONST.ROLE.BUTTON}
accessibilityLabel={translate('validateCodeForm.magicCodeNotReceived')}
>
<Text style={[StyleUtils.getDisabledLinkStyles(shouldDisableResendValidateCode)]}>{translate('validateCodeForm.magicCodeNotReceived')}</Text>
</PressableWithFeedback>
{showDotIndicator && (
<DotIndicatorMessage
type="success"
style={[styles.mt6, styles.flex0]}
// eslint-disable-next-line @typescript-eslint/naming-convention
messages={{0: pendingContact?.validateCodeSent ? translate('validateCodeModal.successfulNewCodeRequest') : translate('resendValidationForm.linkHasBeenResent')}}
/>
)}
</View>
</OfflineWithFeedback>
<OfflineWithFeedback
pendingAction={loginData?.pendingFields?.validateLogin}
errors={validateLoginError}
errorRowStyles={[styles.mt2]}
onClose={() => User.clearContactMethodErrors(contactMethod, 'validateLogin')}
>
<Button
isDisabled={isOffline}
text={translate('common.verify')}
onPress={validateAndSubmitForm}
style={[styles.mt4]}
success
pressOnEnter
large
isLoading={account?.isLoading}
/>
</OfflineWithFeedback>
</>
);
}
BaseValidateCodeForm.displayName = 'BaseValidateCodeForm';
export type {ValidateCodeFormProps, ValidateCodeFormHandle};
export default withOnyx<BaseValidateCodeFormProps, BaseValidateCodeFormOnyxProps>({
account: {key: ONYXKEYS.ACCOUNT},
})(BaseValidateCodeForm);