-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathDelegateMagicCodeModal.tsx
71 lines (59 loc) · 2.52 KB
/
DelegateMagicCodeModal.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
import React, {useEffect, useState} from 'react';
import {useOnyx} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import ValidateCodeActionModal from '@components/ValidateCodeActionModal';
import useLocalize from '@hooks/useLocalize';
import * as User from '@libs/actions/User';
import * as ErrorUtils from '@libs/ErrorUtils';
import Navigation from '@libs/Navigation/Navigation';
import * as Delegate from '@userActions/Delegate';
import type CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
type DelegateMagicCodeModalProps = {
login: string;
role: ValueOf<typeof CONST.DELEGATE_ROLE>;
};
function DelegateMagicCodeModal({login, role}: DelegateMagicCodeModalProps) {
const {translate} = useLocalize();
const [account] = useOnyx(ONYXKEYS.ACCOUNT);
const [isValidateCodeActionModalVisible, setIsValidateCodeActionModalVisible] = useState(true);
const currentDelegate = account?.delegatedAccess?.delegates?.find((d) => d.email === login);
const validateLoginError = ErrorUtils.getLatestErrorField(currentDelegate, 'addDelegate');
useEffect(() => {
if (!currentDelegate || !!currentDelegate.pendingFields?.email || !!currentDelegate.errorFields?.addDelegate) {
return;
}
// Dismiss modal on successful magic code verification
Navigation.navigate(ROUTES.SETTINGS_SECURITY);
}, [login, currentDelegate, role]);
const onBackButtonPress = () => {
setIsValidateCodeActionModalVisible(false);
};
const clearError = () => {
if (!validateLoginError) {
return;
}
Delegate.clearAddDelegateErrors(currentDelegate?.email ?? '', 'addDelegate');
};
const sendValidateCode = () => {
if (currentDelegate?.validateCodeSent) {
return;
}
User.requestValidateCodeAction();
};
return (
<ValidateCodeActionModal
clearError={clearError}
onClose={onBackButtonPress}
validateError={validateLoginError}
isVisible={isValidateCodeActionModalVisible}
title={translate('delegate.makeSureItIsYou')}
sendValidateCode={sendValidateCode}
handleSubmitForm={(validateCode) => Delegate.addDelegate(login, role, validateCode)}
description={translate('delegate.enterMagicCode', {contactMethod: account?.primaryLogin ?? ''})}
/>
);
}
DelegateMagicCodeModal.displayName = 'DelegateMagicCodeModal';
export default DelegateMagicCodeModal;