1
1
import React , { FC , useEffect , useState } from 'react'
2
2
import Button from 'components/base/forms/Button'
3
3
import Utils from 'common/utils/utils'
4
- import { Organisation } from 'common/types/responses'
4
+ import { AuthType , Organisation } from 'common/types/responses'
5
5
import { useDeleteAccountMutation } from 'common/services/useAccount'
6
6
import InputGroup from 'components/base/forms/InputGroup'
7
7
import ModalHR from './ModalHR'
@@ -10,13 +10,35 @@ import ErrorMessage from 'components/ErrorMessage'
10
10
11
11
type ConfirmDeleteAccountType = {
12
12
lastUserOrganisations : Organisation [ ]
13
+ email ?: string
14
+ auth_type ?: AuthType
15
+ }
16
+
17
+ const ERROR_MESSAGES = {
18
+ default : 'Error deleting your account.' ,
19
+ mismatchEmail :
20
+ 'Error deleting your account, please ensure you have entered your current email.' ,
21
+ mismatchPassword :
22
+ 'Error deleting your account, please ensure you have entered your current password.' ,
13
23
}
14
24
const ConfirmDeleteAccount : FC < ConfirmDeleteAccountType > = ( {
25
+ auth_type,
26
+ email,
15
27
lastUserOrganisations,
16
28
} ) => {
17
29
const [ password , setPassword ] = useState < string > ( '' )
18
- const [ deleteUserAccount , { isError, isSuccess : updateSuccess } ] =
19
- useDeleteAccountMutation ( )
30
+ const [ currentEmail , setCurrentEmail ] = useState < string > ( '' )
31
+ const [ errorMessage , setErrorMessage ] = useState < string > (
32
+ ERROR_MESSAGES . default ,
33
+ )
34
+ const [ isEmailMismatchError , setIsEmailMismatchError ] =
35
+ useState < boolean > ( false )
36
+ const [
37
+ deleteUserAccount ,
38
+ { isError : isMutationError , isSuccess : updateSuccess } ,
39
+ ] = useDeleteAccountMutation ( )
40
+ const skipPasswordConfirmation =
41
+ auth_type === 'GOOGLE' || auth_type === 'GITHUB'
20
42
21
43
useEffect ( ( ) => {
22
44
if ( updateSuccess ) {
@@ -54,6 +76,20 @@ const ConfirmDeleteAccount: FC<ConfirmDeleteAccountType> = ({
54
76
< form
55
77
onSubmit = { ( e ) => {
56
78
Utils . preventDefault ( e )
79
+
80
+ if ( skipPasswordConfirmation ) {
81
+ if ( currentEmail !== email ) {
82
+ setIsEmailMismatchError ( true )
83
+ setErrorMessage ( ERROR_MESSAGES . mismatchEmail )
84
+ return
85
+ } else {
86
+ setIsEmailMismatchError ( false )
87
+ setErrorMessage ( ERROR_MESSAGES . default )
88
+ }
89
+ } else {
90
+ setErrorMessage ( ERROR_MESSAGES . mismatchPassword )
91
+ }
92
+
57
93
deleteUserAccount ( {
58
94
current_password : password ,
59
95
delete_orphan_organisations : true ,
@@ -64,26 +100,40 @@ const ConfirmDeleteAccount: FC<ConfirmDeleteAccountType> = ({
64
100
< FormGroup >
65
101
< ModalBody lastUserOrganisations = { lastUserOrganisations } />
66
102
</ FormGroup >
67
- < InputGroup
68
- title = 'Confirm Password'
69
- className = 'mb-0'
70
- inputProps = { {
71
- className : 'full-width' ,
72
- name : 'currentPassword' ,
73
- } }
74
- value = { password }
75
- onChange = { ( event : React . ChangeEvent < HTMLInputElement > ) => {
76
- setPassword ( Utils . safeParseEventValue ( event ) )
77
- } }
78
- type = 'password'
79
- name = 'password'
80
- />
81
- { isError && (
82
- < ErrorMessage
83
- error = 'Error deleting your account, please ensure you have entered your
84
- current password.'
103
+ { skipPasswordConfirmation ? (
104
+ < InputGroup
105
+ title = 'Confirm Email'
106
+ className = 'mb-0'
107
+ inputProps = { {
108
+ className : 'full-width' ,
109
+ name : 'currentEmail' ,
110
+ } }
111
+ value = { currentEmail }
112
+ onChange = { ( event : React . ChangeEvent < HTMLInputElement > ) => {
113
+ setCurrentEmail ( Utils . safeParseEventValue ( event ) )
114
+ } }
115
+ type = 'email'
116
+ name = 'currentEmail'
117
+ />
118
+ ) : (
119
+ < InputGroup
120
+ title = 'Confirm Password'
121
+ className = 'mb-0'
122
+ inputProps = { {
123
+ className : 'full-width' ,
124
+ name : 'currentPassword' ,
125
+ } }
126
+ value = { password }
127
+ onChange = { ( event : React . ChangeEvent < HTMLInputElement > ) => {
128
+ setPassword ( Utils . safeParseEventValue ( event ) )
129
+ } }
130
+ type = 'password'
131
+ name = 'password'
85
132
/>
86
133
) }
134
+ { ( isMutationError || isEmailMismatchError ) && (
135
+ < ErrorMessage error = { errorMessage } />
136
+ ) }
87
137
</ div >
88
138
< ModalHR />
89
139
< div className = 'modal-footer' >
0 commit comments