@@ -11,6 +11,10 @@ import { useForm } from "react-hook-form";
11
11
import { useProfile } from "../../contexts/ProfileContext" ;
12
12
import { passwordValidator } from "../../utils/validators" ;
13
13
import PasswordTextField from "../PasswordTextField" ;
14
+ import {
15
+ PASSWORD_MISMATCH_ERROR_MESSAGE ,
16
+ USE_PROFILE_ERROR_MESSAGE ,
17
+ } from "../../utils/constants" ;
14
18
15
19
interface ChangePasswordModalProps {
16
20
open : boolean ;
@@ -26,8 +30,10 @@ const ChangePasswordModal: React.FC<ChangePasswordModalProps> = (props) => {
26
30
const {
27
31
register,
28
32
handleSubmit,
29
- formState : { errors, isDirty, isValid } ,
33
+ formState : { errors, dirtyFields , isDirty, isValid } ,
30
34
watch,
35
+ trigger,
36
+ reset,
31
37
} = useForm < {
32
38
oldPassword : string ;
33
39
newPassword : string ;
@@ -39,41 +45,61 @@ const ChangePasswordModal: React.FC<ChangePasswordModalProps> = (props) => {
39
45
const profile = useProfile ( ) ;
40
46
41
47
if ( ! profile ) {
42
- throw new Error ( "useProfile() must be used within ProfileContextProvider" ) ;
48
+ throw new Error ( USE_PROFILE_ERROR_MESSAGE ) ;
43
49
}
44
50
45
51
const { updatePassword } = profile ;
46
52
47
53
return (
48
- < Dialog fullWidth open = { open } onClose = { onClose } >
49
- < DialogTitle fontSize = { 24 } > Change password</ DialogTitle >
54
+ < Dialog
55
+ fullWidth
56
+ open = { open }
57
+ onClose = { ( ) => {
58
+ onClose ( ) ;
59
+ reset ( ) ;
60
+ } }
61
+ >
62
+ < DialogTitle fontSize = { 24 } sx = { { paddingBottom : 0 } } >
63
+ Change password
64
+ </ DialogTitle >
50
65
< DialogContent >
51
- < Container maxWidth = "sm" >
66
+ < Container maxWidth = "sm" disableGutters >
52
67
< StyledForm
53
68
onSubmit = { handleSubmit ( ( data ) => {
54
69
updatePassword ( {
55
70
oldPassword : data . oldPassword ,
56
71
newPassword : data . newPassword ,
57
72
} ) ;
58
73
onClose ( ) ;
74
+ reset ( ) ;
59
75
} ) }
60
76
>
61
77
< PasswordTextField
62
78
label = "Current password"
63
79
required
64
80
fullWidth
65
81
margin = "normal"
66
- { ...register ( "oldPassword" , { setValueAs : ( value : string ) => value . trim ( ) } ) }
82
+ sx = { ( theme ) => ( { marginTop : theme . spacing ( 1 ) } ) }
83
+ { ...register ( "oldPassword" , {
84
+ setValueAs : ( value : string ) => value . trim ( ) ,
85
+ } ) }
67
86
/>
68
87
< PasswordTextField
69
88
displayTooltip
70
89
label = "New password"
71
90
required
72
91
fullWidth
73
92
margin = "normal"
93
+ sx = { ( theme ) => ( { marginTop : theme . spacing ( 1 ) } ) }
94
+ input = { watch ( "newPassword" , "" ) }
74
95
{ ...register ( "newPassword" , {
75
96
setValueAs : ( value : string ) => value . trim ( ) ,
76
97
validate : { passwordValidator } ,
98
+ onChange : ( ) => {
99
+ if ( dirtyFields . confirmPassword ) {
100
+ trigger ( "confirmPassword" ) ;
101
+ }
102
+ } ,
77
103
} ) }
78
104
error = { ! ! errors . newPassword }
79
105
helperText = { errors . newPassword ?. message }
@@ -83,11 +109,16 @@ const ChangePasswordModal: React.FC<ChangePasswordModalProps> = (props) => {
83
109
required
84
110
fullWidth
85
111
margin = "normal"
112
+ sx = { ( theme ) => ( { marginTop : theme . spacing ( 1 ) } ) }
86
113
{ ...register ( "confirmPassword" , {
87
114
setValueAs : ( value : string ) => value . trim ( ) ,
88
115
validate : {
89
- matchPassword : ( value ) =>
90
- watch ( "newPassword" ) === value || "Password does not match" ,
116
+ matchPassword : ( value ) => {
117
+ return (
118
+ watch ( "newPassword" ) === value ||
119
+ PASSWORD_MISMATCH_ERROR_MESSAGE
120
+ ) ;
121
+ } ,
91
122
} ,
92
123
} ) }
93
124
error = { ! ! errors . confirmPassword }
@@ -102,7 +133,10 @@ const ChangePasswordModal: React.FC<ChangePasswordModalProps> = (props) => {
102
133
fullWidth
103
134
variant = "contained"
104
135
color = "secondary"
105
- onClick = { onClose }
136
+ onClick = { ( ) => {
137
+ onClose ( ) ;
138
+ reset ( ) ;
139
+ } }
106
140
>
107
141
Cancel
108
142
</ Button >
0 commit comments