11import React , { Component } from 'react'
2- import MaskedInput from 'react-text-mask' // Delete?
32import Formsy from 'formsy-react'
43import { FormsyInput } from '../../../../common/FormsyInput'
5- import { FormsyDatePicker } from '../../../../common/FormsyDatePicker '
4+ import { FormsyHiddenInput } from '../../../../common/FormsyHiddenInput '
65import { FormsyMaskedInput } from '../../../../common/FormsyMaskedInput'
76import { wireUpCustomFormsyValidators } from '../../../../common/CustomValidators'
87import { telephoneFormat } from '../../../../common/Formatters'
98
10-
119class Contact extends Component {
1210 constructor ( ) {
1311 super ( )
@@ -18,32 +16,38 @@ class Contact extends Component {
1816
1917 this . handleCancel = this . handleCancel . bind ( this )
2018 this . handleEdit = this . handleEdit . bind ( this )
19+ this . handleDelete = this . handleDelete . bind ( this )
2120 this . handleInputChange = this . handleInputChange . bind ( this )
2221 wireUpCustomFormsyValidators ( )
2322 }
2423
2524 handleCancel ( ) {
26- console . log ( 'handleCancel' )
27- this . setState ( this . state . cachedForm )
25+ console . debug ( `Contact ${ this . props . contact . id } component in cancel mode` )
2826 this . setState ( { cachedForm : { } } )
29- this . setState ( { showForm : false } )
27+ this . setState ( { showForm : false } )
3028 }
3129
3230 handleDelete ( ) {
33- console . log ( 'handleDelete' )
31+ console . debug ( `Contact ${ this . props . contact . id } component is being deleted` )
32+ this . props . deleteContact ( this . props . contact . id )
3433 }
3534
3635 handleEdit ( ) {
37- console . log ( 'handleEdit' )
36+ console . debug ( `Contact ${ this . props . contact . id } component in edit mode` )
3837 this . setPropsToLocalState ( )
3938 this . setState ( { showForm : true } )
4039 this . setState ( { cachedForm : this . props . contact } )
4140 }
4241
42+ componentDidMount ( ) {
43+ if ( this . props . contact . isNewContact ) {
44+ this . handleEdit ( )
45+ }
46+ }
47+
4348 handleInputChange ( event ) {
44- console . log ( 'handleInputChange' )
4549 let value
46- if ( event . target . name === 'phone' ) {
50+ if ( event . target . name === 'phone' ) {
4751 value = this . sanitizeToJustNumbers ( event . target . value . toString ( ) )
4852 } else {
4953 value = event . target . value
@@ -54,8 +58,8 @@ class Contact extends Component {
5458 }
5559
5660 handleSubmit ( formValues ) {
57- console . log ( 'handleSubmit ')
58- this . props . updatePatientData ( formValues )
61+ console . debug ( 'Submitting contact info updates ')
62+ this . props . updateContactData ( formValues )
5963 this . setState ( { showForm : false } )
6064 }
6165
@@ -68,13 +72,15 @@ class Contact extends Component {
6872 }
6973
7074 setPropsToLocalState ( ) {
71- const keys = [ 'name' , 'relation' , 'address' , 'phone' , 'city' , 'postal' , 'state' , 'country' , 'email' ]
75+ const keys = [ 'id' , ' name', 'relation' , 'address' , 'phone' , 'city' , 'postal' , 'state' , 'country' , 'email' ]
7276
7377 keys . forEach ( ( keyName ) => {
7478 let value
75-
76- if ( keyName === 'phone' ) {
77- value = this . sanitizeToJustNumbers ( this . props . contact [ keyName ] . toString ( ) )
79+ // Make switch statement
80+ if ( keyName === 'phone' ) {
81+ value = this . sanitizeToJustNumbers ( ( this . props . contact [ keyName ] || '' ) . toString ( ) )
82+ } else if ( keyName === 'id' ) {
83+ value = this . props . contact [ keyName ]
7884 } else {
7985 value = this . props . contact [ keyName ]
8086 }
@@ -109,23 +115,23 @@ class Contact extends Component {
109115 </ tr >
110116 < tr >
111117 < td > < strong > Email:</ strong > { this . props . contact . email } </ td >
112- < td > </ td >
113118 </ tr >
114119 </ tbody >
115120 </ table >
116121
117122 < button type = 'button' className = 'btn btn-default btn-sm' onClick = { this . handleDelete } > DELETE</ button >
118123 < button type = 'button' className = 'btn btn-default btn-sm' onClick = { this . handleEdit } > EDIT</ button >
119- < br />
120- < br />
124+
125+ < hr />
126+
121127 </ div >
122128 )
123129 } else if ( this . props . contact && this . state . showForm === true ) {
124130 return (
125131
126132 < Formsy . Form onValidSubmit = { this . handleSubmit . bind ( this ) }
127133 name = 'contactInfoForm'
128- className = 'contact-info-form'
134+ className = 'container contact-info-form'
129135 noValidate >
130136 < table className = 'table' >
131137 < tbody >
@@ -149,8 +155,8 @@ class Contact extends Component {
149155 < td >
150156 < FormsyInput value = { this . state . relation }
151157 onChange = { this . handleInputChange }
152- name = 'relationship '
153- label = 'Relationship '
158+ name = 'relation '
159+ label = 'Relation '
154160 validations = { {
155161 maxLength : 25 ,
156162 minLength : 1
@@ -212,22 +218,6 @@ class Contact extends Component {
212218 } }
213219 required />
214220 </ td >
215- < td >
216- < FormsyInput value = { this . state . postal }
217- onChange = { this . handleInputChange }
218- name = 'postal'
219- label = 'Postal'
220- validations = { {
221- maxLength : 5 ,
222- minLength : 5
223- } }
224- validationErrors = { {
225- isDefaultRequiredValue : 'Valid postal code is required' ,
226- maxLength : 'You must enter a valid postal code' ,
227- minLength : 'You must enter a valid postal code'
228- } }
229- required />
230- </ td >
231221 </ tr >
232222 < tr >
233223 < td >
@@ -246,18 +236,38 @@ class Contact extends Component {
246236 } }
247237 required />
248238 </ td >
239+
240+ < td >
241+ < FormsyInput
242+ value = { this . state . postal }
243+ onChange = { this . handleInputChange }
244+ name = 'postal'
245+ label = 'Postal'
246+ validations = { {
247+ maxLength : 10 ,
248+ minLength : 4
249+ } }
250+ validationErrors = { {
251+ isDefaultRequiredValue : 'Valid postal is required' ,
252+ maxLength : 'You must not enter more than 10 characters' ,
253+ minLength : 'You must enter at least 4 characters'
254+ } }
255+ required />
256+ </ td >
257+ </ tr >
258+ < tr >
249259 < td >
250260 < FormsyInput value = { this . state . country }
251261 onChange = { this . handleInputChange }
252262 name = 'country'
253263 label = 'Country'
254264 validations = { {
255- maxLength : 20 ,
265+ maxLength : 30 ,
256266 minLength : 2
257267 } }
258268 validationErrors = { {
259269 isDefaultRequiredValue : 'Valid country is required' ,
260- maxLength : 'You must not enter more than 20 characters' ,
270+ maxLength : 'You must not enter more than 30 characters' ,
261271 minLength : 'You must enter at least 2 characters'
262272 } }
263273 required />
@@ -280,17 +290,25 @@ class Contact extends Component {
280290 } }
281291 required />
282292 </ td >
283- < td > </ td >
284293 </ tr >
285- </ tbody >
294+ < tr >
295+ < td >
296+ < FormsyHiddenInput value = { this . state . id }
297+ onChange = { this . handleInputChange }
298+ name = 'id'
299+ required />
300+ </ td >
301+ </ tr >
302+ </ tbody >
286303 </ table >
287304
288305 < button className = 'btn btn-default btn-sm' type = 'submit' > SAVE</ button >
289- < button type = 'button' className = 'btn btn-default btn-sm' onClick = { this . handleCancel } > CANCEL </ button >
290- < br />
291- < br / >
306+ < button type = 'button'
307+ className = 'btn btn-default btn-sm'
308+ onClick = { this . handleCancel . bind ( this ) } > CANCEL </ button >
292309
293310 </ Formsy . Form >
311+
294312 )
295313 }
296314 }
0 commit comments