11import React , { PureComponent } from 'react' ;
2+ import head from 'lodash.head' ;
3+ import validate from 'validate.js' ;
24import { Legend , Button , InputGroup , ErrorMsg , ServerErrorMsg } from 'binary-components' ;
35import { actions } from '../_store' ;
46import States from './States' ;
57import UpdateNotice from '../containers/UpdateNotice' ;
68import * as LiveData from '../_data/LiveData' ;
9+ import { getConstraints } from './SettingsAddress.validation.config' ;
710
811export default class SettingsAddress extends PureComponent {
912
@@ -28,6 +31,14 @@ export default class SettingsAddress extends PureComponent {
2831 address_state : props . address_state ,
2932 address_postcode : props . address_postcode ,
3033 phone : props . phone ,
34+ touched : {
35+ address_line_1 : false ,
36+ address_line_2 : false ,
37+ address_city : false ,
38+ address_state : false ,
39+ address_postcode : false ,
40+ phone : false ,
41+ } ,
3142 } ;
3243 }
3344
@@ -36,15 +47,27 @@ export default class SettingsAddress extends PureComponent {
3647 actions . getStatesForCountry ( country_code ) ;
3748 }
3849
39- onEntryChange = ( e : SyntheticEvent ) =>
40- this . setState ( { [ e . target . id ] : e . target . value } ) ;
50+ onEntryChange = ( e : SyntheticEvent ) =>
51+ this . setState ( {
52+ [ e . target . id ] : e . target . value ,
53+ touched : { ...this . state . touched , [ e . target . id ] : true } ,
54+ hasError : false ,
55+ } , ( ) => {
56+ this . validateForm ( ) ;
57+ } ) ;
4158
42- onFormSubmit = ( e : SyntheticEvent ) => {
43- e . preventDefault ( ) ;
59+ validateForm = ( ) => {
60+ this . constraints = getConstraints ( this . props , this . state ) ;
4461 this . setState ( {
45- validatedOnce : true ,
62+ errors : validate ( this . state , this . constraints , { format : 'grouped' , fullMessages : false , cleanAttributes : false } ) || { } ,
4663 } ) ;
47- if ( this . allValid ) {
64+ }
65+
66+ onFormSubmit = ( e : SyntheticEvent ) => {
67+ e . preventDefault ( ) ;
68+ if ( Object . keys ( this . state . errors ) . length > 0 ) {
69+ this . setState ( { hasError : true } ) ;
70+ } else {
4871 this . performUpdateSettings ( ) ;
4972 }
5073 }
@@ -71,63 +94,69 @@ export default class SettingsAddress extends PureComponent {
7194
7295 render ( ) {
7396 const { states } = this . props ;
74- const { validatedOnce, address_line_1, address_line_2, address_city, address_state,
75- address_postcode, country_code, phone, phoneError, serverError, success } = this . state ;
76-
77- this . allValid = address_line_1 && address_city ;
97+ const { address_line_1, address_line_2, address_city, address_state,
98+ address_postcode, country_code, phone, serverError, success, hasError, touched, errors } = this . state ;
7899
79100 return (
80101 < form className = "settings-address" onSubmit = { this . onFormSubmit } >
81102 { serverError && < ServerErrorMsg text = { serverError } /> }
103+ { hasError && < ErrorMsg text = "Please fill the form with valid values" /> }
82104 < UpdateNotice text = "Address updated" show = { success } />
83105 < Legend text = "Address" />
84106 < InputGroup
85107 id = "address_line_1"
108+ name = "address_line_1"
86109 type = "text"
87110 label = "Address"
88111 value = { address_line_1 }
89112 onChange = { this . onEntryChange }
90113 />
91- { validatedOnce && ! address_line_1 &&
92- < ErrorMsg text = "This field is required." />
93- }
114+ { touched . address_line_1 && < ErrorMsg text = { head ( ( errors || { } ) . address_line_1 ) } /> }
94115 < InputGroup
95116 id = "address_line_2"
117+ name = "address_line_2"
96118 type = "text"
97119 label = " "
98120 value = { address_line_2 }
99121 onChange = { this . onEntryChange }
100122 />
123+ { touched . address_line_2 && < ErrorMsg text = { head ( ( errors || { } ) . address_line_2 ) } /> }
101124 < InputGroup
102125 id = "address_city"
126+ name = "address_city"
103127 type = "text"
104128 label = "Town/City"
105129 defaultValue = { address_city }
106130 onChange = { this . onEntryChange }
107131 />
132+ { touched . address_city && < ErrorMsg text = { head ( ( errors || { } ) . address_city ) } /> }
108133 < States
109134 id = "address_state"
135+ name = "address_state"
110136 country = { country_code }
111137 states = { states }
112138 onChange = { this . onEntryChange }
113139 selected = { address_state }
114140 />
141+ { touched . address_state && < ErrorMsg text = { head ( ( errors || { } ) . address_state ) } /> }
115142 < InputGroup
116143 id = "address_postcode"
144+ name = "address_postcode"
117145 type = "text"
118146 label = "Postal Code / ZIP"
119147 defaultValue = { address_postcode }
120148 onChange = { this . onEntryChange }
121149 />
150+ { touched . address_postcode && < ErrorMsg text = { head ( ( errors || { } ) . address_postcode ) } /> }
122151 < InputGroup
123152 id = "phone"
153+ name = "phone"
124154 type = "tel"
125155 label = "Telephone"
126156 defaultValue = { phone }
127157 onChange = { this . onEntryChange }
128158 />
129- { phoneError === 'length' && < ErrorMsg text = "You should enter between 6-35 characters." /> }
130- { phoneError === 'allowed' && < ErrorMsg text = "Only numbers, space, - are allowed." /> }
159+ { touched . phone && < ErrorMsg text = { head ( ( errors || { } ) . phone ) } /> }
131160 < Button
132161 text = "Update"
133162 onClick = { this . tryUpdate }
0 commit comments