@@ -2,15 +2,132 @@ import React ,{Component} from 'react';
2
2
import SideMenu from "../../Layout/SideMenu" ;
3
3
import Navigation from "../../Layout/TopNav" ;
4
4
import { connect } from "react-redux" ;
5
+ import * as UserAction from "../UserList/Container/UserController" ;
6
+ import { Link } from "react-router-dom" ;
5
7
8
+ const NotificationIcon = require ( 'react-feather/dist/icons/alert-triangle' ) . default ;
6
9
const UserIcon = require ( 'react-feather/dist/icons/user' ) . default ;
7
10
11
+ const validEmailRegex = RegExp (
12
+ / ^ ( ( [ ^ < > ( ) \[ \] \. , ; : \s @ \" ] + ( \. [ ^ < > ( ) \[ \] \. , ; : \s @ \" ] + ) * ) | ( \" .+ \" ) ) @ ( ( [ ^ < > ( ) [ \] \. , ; : \s @ \" ] + \. ) + [ ^ < > ( ) [ \] \. , ; : \s @ \" ] { 2 , } ) $ / i
13
+ ) ;
14
+ const validateForm = errors => {
15
+ let valid = true ;
16
+ Object . values ( errors ) . forEach ( val => val . length > 0 && ( valid = false ) ) ;
17
+ return valid ;
18
+ } ;
19
+
8
20
class Index extends Component {
9
21
constructor ( props ) {
10
22
super ( props ) ;
23
+ let userID = '' ;
24
+ if ( props . match . params . id ) {
25
+ userID = props . match . params . id ;
26
+ }
27
+ this . onFileChange = this . onFileChange . bind ( this ) ;
28
+ this . onChange = this . onChange . bind ( this ) ;
29
+ this . handleSubmit = this . handleSubmit . bind ( this ) ;
30
+ this . state = {
31
+ userID : userID ,
32
+ username : '' ,
33
+ email : '' ,
34
+ avatar : '' ,
35
+ profile : [ ] ,
36
+ errors : {
37
+ username : '' ,
38
+ email : '' ,
39
+ }
40
+ } ;
41
+ }
42
+ componentDidMount ( ) {
43
+ if ( this . state . userID ) {
44
+ this . props . getUserData ( this . state . userID ) ;
45
+ }
46
+ }
47
+
48
+ componentWillReceiveProps ( nextProps ) {
49
+ this . setState ( {
50
+ userID : nextProps . userID ,
51
+ username : nextProps . user_name ,
52
+ email : nextProps . user_email ,
53
+ avatar : nextProps . user_avatar ,
54
+ } ) ;
55
+ }
56
+
57
+ onFileChange ( event ) {
58
+ this . createImage ( event . target . files [ 0 ] ) ;
59
+ } ;
60
+
61
+ createImage ( file ) {
62
+ let reader = new FileReader ( ) ;
63
+ reader . onload = ( e ) => {
64
+ this . setState ( {
65
+ profile : e . target . result ,
66
+ } )
67
+ }
68
+ reader . readAsDataURL ( file ) ;
11
69
}
12
- render ( ) {
13
70
71
+ onChange ( event ) {
72
+ event . preventDefault ( ) ;
73
+ const { name, value } = event . target ;
74
+ let errors = this . state . errors ;
75
+ switch ( name ) {
76
+ case 'username' :
77
+ errors . username =
78
+ value . length < 4
79
+ ? 'Name must be at least 4 characters long!'
80
+ : '' ;
81
+ break ;
82
+ case 'email' :
83
+ errors . email =
84
+ validEmailRegex . test ( value )
85
+ ? ''
86
+ : 'Please enter valid Email!' ;
87
+ break ;
88
+ default :
89
+ break ;
90
+ }
91
+ this . setState ( { errors, [ name ] : value } ) ;
92
+ }
93
+
94
+ handleSubmit ( event ) {
95
+ event . preventDefault ( ) ;
96
+ const data = {
97
+ avatar : this . state . profile ,
98
+ name : this . state . username ,
99
+ email : this . state . email ,
100
+ update_user : this . state . userID ,
101
+ } ;
102
+
103
+ if ( validateForm ( this . state . errors ) && this . state . username !== '' && this . state . email !== '' ) {
104
+ this . props . createUserData ( data ) ;
105
+ } else {
106
+ console . error ( 'Invalid Form' ) ;
107
+ }
108
+ }
109
+
110
+ componentWillUnmount ( ) {
111
+ this . props . resetStateValue ( ) ;
112
+ return true ;
113
+ }
114
+
115
+ render ( ) {
116
+ let errorMessage = '' ;
117
+ let defaultErrorStatus = '' ;
118
+ const { errors} = this . state ;
119
+ if ( this . props . message ) {
120
+ errorMessage = this . props . message ;
121
+ defaultErrorStatus = this . props . errorStatus ;
122
+ }
123
+ if ( errors . email . length > 0 ) {
124
+ errorMessage = errors . email ;
125
+ defaultErrorStatus = 'alert alert-danger alert-dismissible show fade' ;
126
+ }
127
+ if ( errors . username . length > 0 ) {
128
+ errorMessage = errors . username ;
129
+ defaultErrorStatus = 'alert alert-danger alert-dismissible show fade' ;
130
+ }
14
131
return (
15
132
< div className = "UserSaveLayout" >
16
133
< div id = "app" >
@@ -19,24 +136,42 @@ class Index extends Component{
19
136
< Navigation />
20
137
< div className = "main-content container-fluid" >
21
138
< div className = "page-title" >
22
- < h4 > < UserIcon /> Create User</ h4 >
139
+ < h4 > < UserIcon /> { this . state . userID ? 'Update Profile' : ' Create User' } </ h4 >
23
140
< hr />
24
141
</ div >
25
142
< section id = "basic-horizontal-layouts" >
26
143
< div className = "row match-height" >
27
144
< div className = "col-md-8 col-12" >
28
145
< div className = "card" >
146
+ { this . state . avatar ?
147
+ < div className = "card-header bg-primary" >
148
+ < h4 className = "card-title text-white" >
149
+ < div className = "avatar avatar-xl mr-3" >
150
+ < img src = { this . state . avatar } alt = { this . state . username } />
151
+ </ div > { this . state . username }
152
+ </ h4 >
153
+ </ div >
154
+ : '' }
29
155
< div className = "card-content" >
30
156
< div className = "card-body" >
31
- < form className = "form form-horizontal" >
157
+ { errorMessage . length > 0 &&
158
+ < div className = { defaultErrorStatus } >
159
+ < NotificationIcon size = { 15 } /> { errorMessage } .
160
+ < button type = "button" className = "close" data-dismiss = "alert"
161
+ aria-label = "Close" >
162
+ < span aria-hidden = "true" > ×</ span >
163
+ </ button >
164
+ </ div >
165
+ }
166
+ < form className = "form form-horizontal" onSubmit = { this . handleSubmit } encType = "multipart/form-data" noValidate >
32
167
< div className = "form-body" >
33
168
< div className = "row" >
34
169
35
170
< div className = "col-md-4" >
36
171
< label > Avatar</ label >
37
172
</ div >
38
173
< div className = "col-md-8 form-group" >
39
- < input type = "file" className = "form-control" name = "profile" id = { 'profile' } />
174
+ < input type = "file" onChange = { this . onFileChange } className = "form-control" name = "profile" id = { 'profile' } />
40
175
</ div >
41
176
42
177
< div className = "col-md-4" >
@@ -45,48 +180,33 @@ class Index extends Component{
45
180
< div className = "col-md-8 form-group" >
46
181
< input type = "text"
47
182
className = "form-control" name = "username"
48
- placeholder = "User Name" />
183
+ placeholder = "User Name" onChange = { this . onChange } autoComplete = { 'off' } value = { this . state . username } />
49
184
</ div >
50
185
< div className = "col-md-4" >
51
186
< label > Email</ label >
52
187
</ div >
53
188
< div className = "col-md-8 form-group" >
54
189
< input type = "email"
55
190
className = "form-control" name = "email"
56
- placeholder = "User Email" autoComplete = { 'off' } />
191
+ placeholder = "User Email" onChange = { this . onChange } autoComplete = { 'off' } value = { this . state . email } />
57
192
</ div >
58
193
< div className = "col-md-12" >
59
194
< hr />
60
195
</ div >
61
196
62
- < div className = "col-md-4" >
63
- < label > New Password</ label >
64
- </ div >
65
- < div className = "col-md-8 form-group" >
66
- < input type = "password" id = "password_confirmation"
67
- className = "form-control" name = "password_confirmation"
68
- placeholder = "Enter new password" onChange = { this . onChange } autoComplete = { 'off' } />
69
- </ div >
70
-
71
- < div className = "col-md-12" >
72
- < hr />
73
- </ div >
74
-
75
197
< div className = "col-sm-12 d-flex justify-content-end" >
76
- < button type = "submit"
77
- className = "btn btn-primary mr-1 mb-1" > Create User
198
+ < button type = "submit" className = "btn btn-primary mr-1 mb-1" >
199
+ { this . state . userID ? 'Update User' : 'Save User' }
78
200
</ button >
201
+ < Link to = '/users' className = { 'float-right btn btn-danger mr-1 mb-1' } > Back</ Link >
79
202
</ div >
80
203
</ div >
81
-
82
204
</ div >
83
205
</ form >
84
206
</ div >
85
207
</ div >
86
208
</ div >
87
209
</ div >
88
-
89
-
90
210
</ div >
91
211
</ section >
92
212
</ div >
@@ -98,5 +218,23 @@ class Index extends Component{
98
218
}
99
219
}
100
220
101
- export default Index ;
221
+ const mapStateToProps = state => {
222
+ return {
223
+ userID : state . UserSection . user_Id ,
224
+ user_name : state . UserSection . user_name ,
225
+ user_email : state . UserSection . user_email ,
226
+ user_avatar : state . UserSection . user_avatar ,
227
+ errorStatus : state . UserSection . status ,
228
+ message : state . UserSection . message ,
229
+ }
230
+ } ;
231
+
232
+ const mapDispatchToProps = dispatch => {
233
+ return {
234
+ getUserData : ( data ) => dispatch ( UserAction . getUserData ( data ) ) ,
235
+ createUserData : ( data ) => dispatch ( UserAction . createUser ( data ) ) ,
236
+ resetStateValue : ( ) => dispatch ( UserAction . resetStateValue ( ) )
237
+ } ;
238
+ } ;
102
239
240
+ export default connect ( mapStateToProps , mapDispatchToProps ) ( Index ) ;
0 commit comments