@@ -11,6 +11,11 @@ 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
+ PASSWORD_REQUIRED_ERROR_MESSAGE ,
17
+ USE_PROFILE_ERROR_MESSAGE ,
18
+ } from "../../utils/constants" ;
14
19
15
20
interface ChangePasswordModalProps {
16
21
open : boolean ;
@@ -26,8 +31,9 @@ const ChangePasswordModal: React.FC<ChangePasswordModalProps> = (props) => {
26
31
const {
27
32
register,
28
33
handleSubmit,
29
- formState : { errors, isDirty, isValid } ,
34
+ formState : { errors, dirtyFields , isDirty, isValid } ,
30
35
watch,
36
+ trigger,
31
37
} = useForm < {
32
38
oldPassword : string ;
33
39
newPassword : string ;
@@ -39,16 +45,18 @@ 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
54
< Dialog fullWidth open = { open } onClose = { onClose } >
49
- < DialogTitle fontSize = { 24 } > Change password</ DialogTitle >
55
+ < DialogTitle fontSize = { 24 } sx = { { paddingBottom : 0 } } >
56
+ Change password
57
+ </ DialogTitle >
50
58
< DialogContent >
51
- < Container maxWidth = "sm" >
59
+ < Container maxWidth = "sm" disableGutters >
52
60
< StyledForm
53
61
onSubmit = { handleSubmit ( ( data ) => {
54
62
updatePassword ( {
@@ -63,8 +71,10 @@ const ChangePasswordModal: React.FC<ChangePasswordModalProps> = (props) => {
63
71
required
64
72
fullWidth
65
73
margin = "normal"
74
+ sx = { ( theme ) => ( { marginTop : theme . spacing ( 1 ) } ) }
66
75
{ ...register ( "oldPassword" , {
67
- required : "Required field" ,
76
+ setValueAs : ( value : string ) => value . trim ( ) ,
77
+ required : PASSWORD_REQUIRED_ERROR_MESSAGE ,
68
78
} ) }
69
79
error = { ! ! errors . oldPassword }
70
80
helperText = { errors . oldPassword ?. message }
@@ -75,8 +85,16 @@ const ChangePasswordModal: React.FC<ChangePasswordModalProps> = (props) => {
75
85
required
76
86
fullWidth
77
87
margin = "normal"
88
+ sx = { ( theme ) => ( { marginTop : theme . spacing ( 1 ) } ) }
89
+ input = { watch ( "newPassword" , "" ) }
78
90
{ ...register ( "newPassword" , {
91
+ setValueAs : ( value : string ) => value . trim ( ) ,
79
92
validate : { passwordValidator } ,
93
+ onChange : ( ) => {
94
+ if ( dirtyFields . confirmPassword ) {
95
+ trigger ( "confirmPassword" ) ;
96
+ }
97
+ } ,
80
98
} ) }
81
99
error = { ! ! errors . newPassword }
82
100
helperText = { errors . newPassword ?. message }
@@ -86,10 +104,13 @@ const ChangePasswordModal: React.FC<ChangePasswordModalProps> = (props) => {
86
104
required
87
105
fullWidth
88
106
margin = "normal"
107
+ sx = { ( theme ) => ( { marginTop : theme . spacing ( 1 ) } ) }
89
108
{ ...register ( "confirmPassword" , {
109
+ setValueAs : ( value : string ) => value . trim ( ) ,
90
110
validate : {
91
111
matchPassword : ( value ) =>
92
- watch ( "newPassword" ) === value || "Password does not match" ,
112
+ watch ( "newPassword" ) === value ||
113
+ PASSWORD_MISMATCH_ERROR_MESSAGE ,
93
114
} ,
94
115
} ) }
95
116
error = { ! ! errors . confirmPassword }
0 commit comments