-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathindex.tsx
89 lines (81 loc) · 3.36 KB
/
index.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
import React, {forwardRef, useEffect, useRef, useState} from 'react';
import {InteractionManager, View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import Text from '@components/Text';
import ValidateCodeForm from '@components/ValidateCodeActionModal/ValidateCodeForm';
import type {ValidateCodeFormHandle} from '@components/ValidateCodeActionModal/ValidateCodeForm/BaseValidateCodeForm';
import useThemeStyles from '@hooks/useThemeStyles';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ValidateCodeActionFormProps} from './type';
function ValidateCodeActionForm({
isValidated,
descriptionPrimary,
descriptionSecondary,
validatePendingAction,
validateError,
handleSubmitForm,
clearError,
sendValidateCode,
hasMagicCodeBeenSent,
isLoading,
forwardedRef,
}: ValidateCodeActionFormProps) {
const themeStyles = useThemeStyles();
const firstRenderRef = useRef(true);
const isClosedRef = useRef(false);
const [validateCodeAction] = useOnyx(ONYXKEYS.VALIDATE_ACTION_CODE);
const [canSendHasMagicCodeBeenSent, setCanSendHasMagicCodeBeenSent] = useState(false);
useEffect(
() => () => {
firstRenderRef.current = true;
isClosedRef.current = true;
},
[],
);
useEffect(() => {
if (!firstRenderRef.current || hasMagicCodeBeenSent) {
// eslint-disable-next-line rulesdir/prefer-early-return
return () => {
// We need to run clearError in cleanup function to use as onClose function.
// As 'useEffect cleanup function' runs when even the component is unmounted, we need to put clearError() in the if condition.
// So clearError() will not run when the form is unmounted.
if (isClosedRef.current && !isValidated) {
clearError();
}
};
}
firstRenderRef.current = false;
sendValidateCode();
if (hasMagicCodeBeenSent) {
InteractionManager.runAfterInteractions(() => {
setCanSendHasMagicCodeBeenSent(true);
});
}
}, [isValidated, sendValidateCode, hasMagicCodeBeenSent, clearError]);
return (
<View style={[themeStyles.ph5, themeStyles.mt3, themeStyles.mb5, themeStyles.flex1]}>
<Text style={[themeStyles.mb3]}>{descriptionPrimary}</Text>
{!!descriptionSecondary && <Text style={[themeStyles.mb3]}>{descriptionSecondary}</Text>}
<ValidateCodeForm
isLoading={isLoading}
validateCodeAction={validateCodeAction}
validatePendingAction={validatePendingAction}
validateError={validateError}
handleSubmitForm={handleSubmitForm}
sendValidateCode={sendValidateCode}
clearError={clearError}
buttonStyles={[themeStyles.justifyContentEnd, themeStyles.flex1]}
ref={forwardedRef}
hasMagicCodeBeenSent={canSendHasMagicCodeBeenSent}
/>
</View>
);
}
ValidateCodeActionForm.displayName = 'ValidateCodeActionForm';
export default forwardRef<ValidateCodeFormHandle, ValidateCodeActionFormProps>((props, ref) => (
<ValidateCodeActionForm
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
forwardedRef={ref}
/>
));