@@ -45,6 +45,8 @@ interface UserState {
4545interface State {
4646 isDirty : boolean
4747 user : UserState
48+ validated : boolean
49+ nameIsValid : boolean
4850}
4951
5052let email = "" ;
@@ -63,6 +65,8 @@ class UserComponent extends Component<{}, State> {
6365 this . state = {
6466 isDirty : false ,
6567 user : state ,
68+ validated : false ,
69+ nameIsValid : true ,
6670 }
6771
6872 fetchClient . GET ( "/user/me" , { credentials : "same-origin" } ) . then ( ( { data, error, response} ) => {
@@ -75,8 +79,24 @@ class UserComponent extends Component<{}, State> {
7579 } ) ;
7680 }
7781
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+
7893 submit = async ( e : SubmitEvent ) => {
7994 e . preventDefault ( ) ;
95+
96+ if ( ! this . validateForm ( ) ) {
97+ return ;
98+ }
99+
80100 const { response, error} = await fetchClient . PUT ( "/user/update_user" , { body : this . state . user , credentials : "same-origin" } ) ;
81101 const status = response . status ;
82102 if ( status === 200 ) {
@@ -89,14 +109,14 @@ class UserComponent extends Component<{}, State> {
89109 render ( ) {
90110 const { t} = useTranslation ( "" , { useSuspense : false , keyPrefix : "user" } ) ;
91111 return ( < >
92- < Form onSubmit = { this . submit } >
112+ < Form onSubmit = { this . submit } noValidate validated = { this . state . validated } >
93113 < Form . Group className = "pb-3" controlId = "userId" >
94114 < Form . Label className = "text-muted" > { t ( "user_id" ) } </ Form . Label >
95115 < Form . Control type = "text" disabled value = { this . state . user . id } className = "bg-light" />
96116 </ Form . Group >
97117 < Form . Group className = "pb-3" controlId = "userEmail" >
98118 < 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 ) => {
100120 this . setState ( { user : { ...this . state . user , email : ( e . target as HTMLInputElement ) . value } , isDirty : true } ) ;
101121 } } disabled = { this . state . user . has_old_charger } />
102122 { this . state . user . has_old_charger &&
@@ -107,9 +127,12 @@ class UserComponent extends Component<{}, State> {
107127 </ Form . Group >
108128 < Form . Group className = "pb-3" controlId = "userName" >
109129 < 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 ) => {
111131 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 >
113136 </ Form . Group >
114137 < Button type = "submit" variant = "primary" disabled = { ! this . state . isDirty } >
115138 { t ( "save_changes" ) }
0 commit comments