11import React , { Component } from "react" ;
2+ import axiosInstance from "../axiosApi" ;
23
34class Signup extends Component {
45 constructor ( props ) {
56 super ( props ) ;
67 this . state = {
78 username : "" ,
89 password : "" ,
9- email :""
10+ email :"" ,
11+ errors :{ }
1012 } ;
1113
1214 this . handleChange = this . handleChange . bind ( this ) ;
@@ -17,9 +19,21 @@ class Signup extends Component{
1719 this . setState ( { [ event . target . name ] : event . target . value } ) ;
1820 }
1921
20- handleSubmit ( event ) {
21- alert ( 'A username and password was submitted: ' + this . state . username + " " + this . state . password + " " + this . state . email ) ;
22+ async handleSubmit ( event ) {
2223 event . preventDefault ( ) ;
24+ try {
25+ const response = await axiosInstance . post ( '/user/create/' , {
26+ username : this . state . username ,
27+ email : this . state . email ,
28+ password : this . state . password
29+ } ) ;
30+ return response ;
31+ } catch ( error ) {
32+ console . log ( error . stack ) ;
33+ this . setState ( {
34+ errors :error . response . data
35+ } ) ;
36+ }
2337 }
2438
2539 render ( ) {
@@ -30,19 +44,23 @@ class Signup extends Component{
3044 < label >
3145 Username:
3246 < input name = "username" type = "text" value = { this . state . username } onChange = { this . handleChange } />
47+ { this . state . errors . username ? this . state . errors . username : null }
3348 </ label >
3449 < label >
3550 Email:
3651 < input name = "email" type = "email" value = { this . state . email } onChange = { this . handleChange } />
52+ { this . state . errors . email ? this . state . errors . email : null }
3753 </ label >
3854 < label >
3955 Password:
4056 < input name = "password" type = "password" value = { this . state . password } onChange = { this . handleChange } />
57+ { this . state . errors . password ? this . state . errors . password : null }
4158 </ label >
4259 < input type = "submit" value = "Submit" />
4360 </ form >
4461 </ div >
4562 )
4663 }
4764}
65+
4866export default Signup ;
0 commit comments