@@ -45,6 +45,8 @@ interface UserState {
45
45
interface State {
46
46
isDirty : boolean
47
47
user : UserState
48
+ validated : boolean
49
+ nameIsValid : boolean
48
50
}
49
51
50
52
let email = "" ;
@@ -63,6 +65,8 @@ class UserComponent extends Component<{}, State> {
63
65
this . state = {
64
66
isDirty : false ,
65
67
user : state ,
68
+ validated : false ,
69
+ nameIsValid : true ,
66
70
}
67
71
68
72
fetchClient . GET ( "/user/me" , { credentials : "same-origin" } ) . then ( ( { data, error, response} ) => {
@@ -75,8 +79,24 @@ class UserComponent extends Component<{}, State> {
75
79
} ) ;
76
80
}
77
81
82
+ validateForm = ( ) => {
83
+ const nameIsValid = this . state . user . name . trim ( ) . length > 0 ;
84
+
85
+ this . setState ( {
86
+ nameIsValid,
87
+ validated : true
88
+ } ) ;
89
+
90
+ return nameIsValid ;
91
+ }
92
+
78
93
submit = async ( e : SubmitEvent ) => {
79
94
e . preventDefault ( ) ;
95
+
96
+ if ( ! this . validateForm ( ) ) {
97
+ return ;
98
+ }
99
+
80
100
const { response, error} = await fetchClient . PUT ( "/user/update_user" , { body : this . state . user , credentials : "same-origin" } ) ;
81
101
const status = response . status ;
82
102
if ( status === 200 ) {
@@ -89,14 +109,14 @@ class UserComponent extends Component<{}, State> {
89
109
render ( ) {
90
110
const { t} = useTranslation ( "" , { useSuspense : false , keyPrefix : "user" } ) ;
91
111
return ( < >
92
- < Form onSubmit = { this . submit } >
112
+ < Form onSubmit = { this . submit } noValidate validated = { this . state . validated } >
93
113
< Form . Group className = "pb-3" controlId = "userId" >
94
114
< Form . Label className = "text-muted" > { t ( "user_id" ) } </ Form . Label >
95
115
< Form . Control type = "text" disabled value = { this . state . user . id } className = "bg-light" />
96
116
</ Form . Group >
97
117
< Form . Group className = "pb-3" controlId = "userEmail" >
98
118
< Form . Label className = "text-muted" > { t ( "email" ) } </ Form . Label >
99
- < Form . Control type = "email" value = { this . state . user . email } onChange = { ( e ) => {
119
+ < Form . Control required type = "email" value = { this . state . user . email } onChange = { ( e ) => {
100
120
this . setState ( { user : { ...this . state . user , email : ( e . target as HTMLInputElement ) . value } , isDirty : true } ) ;
101
121
} } disabled = { this . state . user . has_old_charger } />
102
122
{ this . state . user . has_old_charger &&
@@ -107,9 +127,12 @@ class UserComponent extends Component<{}, State> {
107
127
</ Form . Group >
108
128
< Form . Group className = "pb-3" controlId = "userName" >
109
129
< Form . Label className = "text-muted" > { t ( "name" ) } </ Form . Label >
110
- < Form . Control type = "text" value = { this . state . user . name } onChange = { ( e ) => {
130
+ < Form . Control required type = "text" value = { this . state . user . name } onChange = { ( e ) => {
111
131
this . setState ( { user : { ...this . state . user , name : ( e . target as HTMLInputElement ) . value } , isDirty : true } ) ;
112
- } } />
132
+ } } isInvalid = { ! this . state . nameIsValid } />
133
+ < Form . Control . Feedback type = "invalid" >
134
+ { t ( "invalid_name" ) }
135
+ </ Form . Control . Feedback >
113
136
</ Form . Group >
114
137
< Button type = "submit" variant = "primary" disabled = { ! this . state . isDirty } >
115
138
{ t ( "save_changes" ) }
0 commit comments