11import React from 'react' ;
2- import { Link } from 'react-router'
2+ import { Link , Router } from 'react-router'
33
44import Error from 'components/Error/Error'
5- import styles from 'components/Client/Profile/styles.css'
5+ import AreaService from 'services/Area'
6+ import ClientService from 'services/Client'
7+ import styles from 'components/Client/Create/styles.css'
68
79class Client extends React . Component
810{
@@ -11,24 +13,55 @@ class Client extends React.Component
1113 super ( props ) ;
1214 this . handleSubmit = this . handleSubmit . bind ( this ) ;
1315 this . state = {
14- client : null ,
15- error : ''
16+ error : '' ,
17+ areas : null ,
18+ client : { }
1619 } ;
1720 }
1821
22+ componentWillMount ( ) {
23+
24+ AreaService . getAll ( ) . then ( ( response ) => {
25+ this . setState ( { areas : response . data . areas } ) ;
26+ } ) . catch ( ( error ) => {
27+ this . setState ( { error : 'Error Found: Trying get areas' } ) ;
28+ if ( typeof error . response . data . error !== 'undefined' ) {
29+ this . setState ( { error : error . response . data . error } ) ;
30+ }
31+ } ) ;
32+ }
33+
1934 handleSubmit ( e ) {
2035 e . preventDefault ( ) ;
2136
37+ let client = { }
2238 for ( let i in this . refs ) {
23- console . log ( this . refs [ i ] . value ) ;
39+ client [ i ] = this . refs [ i ] . value ;
2440 }
2541
42+ this . setState ( { client : client } ) ;
43+
44+ ClientService . save ( client ) . then ( ( response ) => {
45+ this . context . router . push ( "/clients" ) ;
46+ } ) . catch ( ( error ) => {
47+
48+ this . setState ( { error : 'Error trying create client' } ) ;
49+
50+ let responseValid = typeof error . response . data !== 'undefined' ;
51+
52+ if ( responseValid && typeof error . response . data . error !== 'undefined' ) {
53+ this . setState ( { error : error . response . data . error } ) ;
54+ }
55+ } ) ;
2656 }
2757
2858 render ( ) {
2959 if ( this . state . error ) {
3060 return ( < Error error = { this . state . error } /> ) ;
3161 }
62+ if ( ! this . state . areas ) {
63+ return < div > Loading...</ div > ;
64+ }
3265
3366 return (
3467 < div className = "container" >
@@ -42,8 +75,9 @@ class Client extends React.Component
4275 < label className = "label" > Name</ label >
4376 < p className = "control has-icon" >
4477 < input
78+ required = 'required'
4579 ref = 'name'
46- className = "input"
80+ className = "input marco "
4781 type = "text"
4882 placeholder = "John Smith"
4983 />
@@ -52,26 +86,30 @@ class Client extends React.Component
5286 < label className = "label" > Phone</ label >
5387 < p className = "control has-icon" >
5488 < input
89+ required = 'required'
90+ pattern = "[\(]\d{2}[\)]\d{4,5}[\-]\d{4}"
5591 ref = 'phone'
5692 className = "input"
5793 type = "text"
58- placeholder = "21 99999-00000"
94+ placeholder = "(21) 99999-00000"
5995 />
6096 < i className = "fa fa-phone" aria-hidden = "true" />
6197 </ p >
6298 < label className = "label" > Address</ label >
6399 < p className = "control has-icon" >
64100 < input
101+ required = 'required'
65102 ref = 'address'
66103 className = "input"
67104 type = "text"
68- placeholder = "Street 32 - Nª 23"
105+ placeholder = "Street 32 - Nº 23"
69106 />
70107 < i className = "fa fa-address-card" aria-hidden = "true" />
71108 </ p >
72109 < label className = "label" > City</ label >
73110 < p className = "control has-icon" >
74111 < input
112+ required = 'required'
75113 ref = 'city'
76114 className = "input"
77115 type = "text"
@@ -81,27 +119,32 @@ class Client extends React.Component
81119 </ p >
82120 < label className = "label" > Area</ label >
83121 < p className = "control has-icon" >
84- < input
85- ref = 'area'
86- className = "input"
87- type = "text"
88- placeholder = "Center"
89- />
122+ < span className = "select" >
123+ < select ref = 'area' >
124+ {
125+ this . state . areas . map ( ( area , key ) => (
126+ < option value = { area . _id } key = { key } > { area . _id } </ option >
127+ ) )
128+ }
129+ </ select >
130+ </ span >
90131 < i className = "fa fa-map-marker" aria-hidden = "true" />
91132 </ p >
92133 < label className = "label" > Frequency</ label >
93134 < p className = "control has-icon" >
94135 < input
136+ required = 'required'
95137 ref = 'frequency'
96138 className = "input"
97139 type = "number"
98140 placeholder = "10"
99141 />
100- < i className = "fa fa-calendar-check-o " aria-hidden = "true" />
142+ < i className = "fa fa-refresh " aria-hidden = "true" />
101143 </ p >
102144 < label className = "label" > Ability</ label >
103145 < p className = "control has-icon" >
104146 < input
147+ required = 'required'
105148 ref = 'ability'
106149 className = "input"
107150 type = "number"
@@ -125,6 +168,9 @@ class Client extends React.Component
125168 }
126169}
127170
128- export default Client ;
171+ Client . contextTypes = {
172+ router : React . PropTypes . object . isRequired
173+ } ;
129174
175+ export default Client ;
130176
0 commit comments