Skip to content

Commit 4b11234

Browse files
author
Matthew Vita
committed
new contact impl (bug with instant validation failure)
1 parent f1e383b commit 4b11234

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

samples/react-redux-patient-demographics-example/src/routes/Patient/Demographics/Contact/ContactComponent.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ class Contact extends Component {
3939
this.setState({ cachedForm: this.props.contact })
4040
}
4141

42+
componentDidMount() {
43+
if (this.props.contact.isNewContact) {
44+
this.handleEdit()
45+
}
46+
}
47+
4248
handleInputChange(event) {
4349
let value
4450
if (event.target.name === 'phone') {
@@ -72,7 +78,7 @@ class Contact extends Component {
7278
let value
7379
// Make switch statement
7480
if (keyName === 'phone') {
75-
value = this.sanitizeToJustNumbers(this.props.contact[keyName].toString())
81+
value = this.sanitizeToJustNumbers((this.props.contact[keyName] || '').toString())
7682
} else if (keyName === 'id') {
7783
value = this.props.contact[keyName]
7884
} else {

samples/react-redux-patient-demographics-example/src/routes/Patient/Demographics/PatientDemographicsComponent.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class PatientDemographics extends React.Component {
2626
});
2727
}
2828

29+
addNewContact() {
30+
this.props.startAddingNewContact(this.props.routeParams.pid)
31+
}
32+
2933
determineIfRouteIsValid() {
3034
return this.props.routeParams.pid
3135
}
@@ -49,7 +53,8 @@ class PatientDemographics extends React.Component {
4953
}
5054

5155
render() {
52-
let children = null;
56+
let children = null
57+
let addContactVisibility = 'hidden'
5358

5459
switch (this.state.tab) {
5560
case this.TABS.BASIC:
@@ -58,13 +63,15 @@ class PatientDemographics extends React.Component {
5863
break;
5964
case this.TABS.CONTACTS:
6065
if (this.props.contacts) {
61-
children = this.props.contacts.map(contact =>
62-
<Contact updateContactData={this.props.updateContactData}
63-
deleteContact={this.props.deleteContact}
64-
key={contact.id}
65-
contact={contact}/>
66+
children = this.props.contacts.map((contact) => {
67+
return <Contact updateContactData={this.props.updateContactData}
68+
deleteContact={this.props.deleteContact}
69+
key={contact.id}
70+
contact={contact}/>
71+
}
6672
)
6773
}
74+
addContactVisibility = 'visible'
6875
break;
6976
}
7077

@@ -89,6 +96,12 @@ class PatientDemographics extends React.Component {
8996
</div>
9097

9198
{children}
99+
100+
<br />
101+
102+
<button type='button'
103+
className={['btn', 'btn-default', 'btn-sm', addContactVisibility].join(' ')}
104+
onClick={this.addNewContact.bind(this)}>ADD NEW CONTACT</button>
92105
</div>
93106
</div>
94107
)

samples/react-redux-patient-demographics-example/src/routes/Patient/Demographics/PatientDemographicsContainer.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { connect } from 'react-redux'
2-
import { setPatientInContext, updatePatientData, updateContactData, deleteContact } from '../PatientModule'
2+
import { setPatientInContext, updatePatientData, updateContactData, deleteContact, startAddingNewContact }
3+
from '../PatientModule'
34
import PatientDemographics from './PatientDemographicsComponent'
45

56
const mapDispatchToProps = {
67
setPatientInContext,
78
updatePatientData,
89
updateContactData,
9-
deleteContact
10+
deleteContact,
11+
startAddingNewContact
1012
}
1113

1214
const extractBasicInfo = (state) => {

samples/react-redux-patient-demographics-example/src/routes/Patient/PatientModule.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export const updatePatientData = (data) => {
100100
export const updateContactData = (data) => {
101101
return (dispatch, getState) => {
102102
return new Promise((resolve, reject) => {
103-
console.debug(`updating contact patient data for ${getState().patient.patientInContext}`)
103+
console.debug(`updating contact data for ${getState().patient.patientInContext}`)
104104
dispatch({
105105
type : 'UPDATE_CONTACT_DATA',
106106
payload : [getState().patient.patientInContext, data]
@@ -113,7 +113,7 @@ export const updateContactData = (data) => {
113113
export const deleteContact = (data) => {
114114
return (dispatch, getState) => {
115115
return new Promise((resolve, reject) => {
116-
console.debug(`deleting contact patient data for ${getState().patient.patientInContext}`)
116+
console.debug(`deleting contact data for ${getState().patient.patientInContext}`)
117117
dispatch({
118118
type : 'DELETING_CONTACT',
119119
payload : [getState().patient.patientInContext, data]
@@ -123,9 +123,18 @@ export const deleteContact = (data) => {
123123
}
124124
}
125125

126-
export const actions = {
127-
setPatientInContext
128-
};
126+
export const startAddingNewContact = (data) => {
127+
return (dispatch, getState) => {
128+
return new Promise((resolve, reject) => {
129+
console.debug(`starting to add contact data for ${getState().patient.patientInContext}`)
130+
dispatch({
131+
type : 'START_ADDING_CONTACT',
132+
payload : [getState().patient.patientInContext, data]
133+
})
134+
resolve()
135+
})
136+
}
137+
}
129138

130139
/**
131140
* Reducer
@@ -145,19 +154,23 @@ export default function patientReducer (state = initialState, action) {
145154
break
146155
case 'UPDATE_CONTACT_DATA':
147156
const contactIndexForUpdation = _.findIndex(copy[action.payload[0]].contacts, (c) => {
148-
return c.id == action.payload[1].id
157+
return c.id === action.payload[1].id
149158
})
150159
copy[action.payload[0]].contacts[contactIndexForUpdation] = action.payload[1]
151160
result = copy
152161
break
162+
case 'START_ADDING_CONTACT':
163+
const newContactId = _.last(copy[action.payload[0]].contacts).id + 1;
164+
copy[action.payload[0]].contacts.push({ isNewContact: true, id: newContactId })
165+
result = copy
166+
break
153167
case 'DELETING_CONTACT':
154168
const contactIndexForDeletion = _.findIndex(copy[action.payload[0]].contacts, (c) => {
155169
if (c && c.hasOwnProperty('id')) {
156-
return c.id == action.payload[1]
170+
return c.id === action.payload[1]
157171
}
158172
})
159173
delete copy[action.payload[0]].contacts[contactIndexForDeletion]
160-
console.log(copy)
161174
result = copy
162175
break
163176
default:

0 commit comments

Comments
 (0)