1
- import { Fragment , useState } from 'react' ;
2
- import { Text } from '@deriv/components' ;
1
+ import { Fragment , useEffect , useState } from 'react' ;
2
+
3
3
import { useVerifyEmail } from '@deriv/api' ;
4
+ import { Button , Modal , Text } from '@deriv/components' ;
4
5
import { toTitleCase } from '@deriv/shared' ;
5
6
import { observer , useStore } from '@deriv/stores' ;
6
7
import { Localize , useTranslations } from '@deriv-com/translations' ;
8
+
7
9
import SentEmailModal from '../../../Components/sent-email-modal' ;
8
10
import UnlinkAccountModal from '../../../Components/unlink-account-modal' ;
11
+
9
12
import EmailPasswordSection from './email-password-section' ;
10
13
11
14
type TVerifyEmailPayload = Parameters < ReturnType < typeof useVerifyEmail > [ 'mutate' ] > [ 0 ] ;
@@ -20,19 +23,29 @@ const DerivEmail = observer(() => {
20
23
common : { is_from_derivgo } ,
21
24
client : { social_identity_provider, is_social_signup, email } ,
22
25
} = useStore ( ) ;
23
- const { mutate } = useVerifyEmail ( ) ;
26
+
27
+ const { mutate, isError, error, isSuccess } = useVerifyEmail ( ) ;
24
28
const { localize } = useTranslations ( ) ;
25
29
const [ is_unlink_account_modal_open , setIsUnlinkAccountModalOpen ] = useState ( false ) ;
26
30
const [ is_send_email_modal_open , setIsSendEmailModalOpen ] = useState ( false ) ;
31
+ const [ is_error_modal_open , setIsErrorModalOpen ] = useState ( false ) ;
27
32
28
33
const payload : TVerifyEmailPayload = { verify_email : email , type : 'request_email' } ;
29
34
35
+ useEffect ( ( ) => {
36
+ if ( isError ) {
37
+ setIsErrorModalOpen ( true ) ;
38
+ setIsSendEmailModalOpen ( false ) ;
39
+ } else if ( isSuccess ) {
40
+ setIsSendEmailModalOpen ( true ) ;
41
+ }
42
+ } , [ isError , isSuccess ] ) ;
43
+
30
44
const onClickChangeEmail = ( ) => {
31
45
if ( is_social_signup ) {
32
46
setIsUnlinkAccountModalOpen ( true ) ;
33
47
} else {
34
48
mutate ( payload ) ;
35
- setIsSendEmailModalOpen ( true ) ;
36
49
}
37
50
} ;
38
51
@@ -42,6 +55,15 @@ const DerivEmail = observer(() => {
42
55
setIsSendEmailModalOpen ( true ) ;
43
56
} ;
44
57
58
+ const onClose = ( ) => {
59
+ setIsErrorModalOpen ( false ) ;
60
+ } ;
61
+
62
+ type VerifyEmailError = {
63
+ code ?: string ;
64
+ message ?: string ;
65
+ } ;
66
+
45
67
return (
46
68
< Fragment >
47
69
< div className = 'account__passwords-wrapper' >
@@ -74,6 +96,31 @@ const DerivEmail = observer(() => {
74
96
is_modal_when_mobile = { true }
75
97
/>
76
98
</ div >
99
+ { is_error_modal_open && error && (
100
+ < Modal
101
+ is_open = { is_error_modal_open }
102
+ has_close_icon
103
+ should_header_stick_body
104
+ title = { localize ( 'Email change currently unavailable' ) }
105
+ toggleModal = { onClose }
106
+ width = '440px'
107
+ height = '200px'
108
+ >
109
+ < div className = 'account__email-unhandled-error' >
110
+ < Text className = 'account__email-unhandled-error-error_text' as = 'p' size = 'xs' >
111
+ { ( error as VerifyEmailError ) ?. code === 'EmailChangeFailP2PActive' ? (
112
+ < Localize i18n_default_text = 'Complete P2P orders and deactivate ads to proceed.' />
113
+ ) : (
114
+ < Localize i18n_default_text = { ( error as VerifyEmailError ) ?. message || '' } />
115
+ ) }
116
+ </ Text >
117
+
118
+ < Button onClick = { onClose } has_effect primary large >
119
+ < Localize i18n_default_text = 'OK' />
120
+ </ Button >
121
+ </ div >
122
+ </ Modal >
123
+ ) }
77
124
</ Fragment >
78
125
) ;
79
126
} ) ;
0 commit comments