Skip to content

Commit f1e383b

Browse files
author
Matthew Vita
committed
deletion
1 parent fd8d8e3 commit f1e383b

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

samples/react-redux-patient-demographics-example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
"rimraf": "^2.5.4",
113113
"sass-loader": "^4.0.0",
114114
"style-loader": "^0.13.1",
115+
"underscore": "^1.8.3",
115116
"url-loader": "^0.5.6",
116117
"webpack": "^1.12.14",
117118
"yargs": "^6.3.0"

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,24 @@ class Contact extends Component {
1616

1717
this.handleCancel = this.handleCancel.bind(this)
1818
this.handleEdit = this.handleEdit.bind(this)
19+
this.handleDelete = this.handleDelete.bind(this)
1920
this.handleInputChange = this.handleInputChange.bind(this)
2021
wireUpCustomFormsyValidators()
2122
}
2223

2324
handleCancel() {
24-
console.debug('Basic component in read mode')
25+
console.debug(`Contact ${this.props.contact.id} component in cancel mode`)
2526
this.setState({ cachedForm: {} })
2627
this.setState({ showForm: false })
2728
}
2829

2930
handleDelete() {
30-
console.log('handleDelete')
31+
console.debug(`Contact ${this.props.contact.id} component is being deleted`)
32+
this.props.deleteContact(this.props.contact.id)
3133
}
3234

3335
handleEdit() {
34-
console.debug('Contact component in edit mode')
36+
console.debug(`Contact ${this.props.contact.id} component in edit mode`)
3537
this.setPropsToLocalState()
3638
this.setState({showForm: true})
3739
this.setState({ cachedForm: this.props.contact })

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ class PatientDemographics extends React.Component {
5959
case this.TABS.CONTACTS:
6060
if (this.props.contacts) {
6161
children = this.props.contacts.map(contact =>
62-
<Contact updateContactData={this.props.updateContactData} key={contact.id} contact={contact}/>
62+
<Contact updateContactData={this.props.updateContactData}
63+
deleteContact={this.props.deleteContact}
64+
key={contact.id}
65+
contact={contact}/>
6366
)
6467
}
6568
break;

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

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

55
const mapDispatchToProps = {
66
setPatientInContext,
77
updatePatientData,
8-
updateContactData
8+
updateContactData,
9+
deleteContact
910
}
1011

1112
const extractBasicInfo = (state) => {

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import clone from 'clone'
2+
import _ from 'underscore'
23

34
/**
45
* Stub data that will be used as the initial state in the store.
@@ -109,6 +110,19 @@ export const updateContactData = (data) => {
109110
}
110111
}
111112

113+
export const deleteContact = (data) => {
114+
return (dispatch, getState) => {
115+
return new Promise((resolve, reject) => {
116+
console.debug(`deleting contact patient data for ${getState().patient.patientInContext}`)
117+
dispatch({
118+
type : 'DELETING_CONTACT',
119+
payload : [getState().patient.patientInContext, data]
120+
})
121+
resolve()
122+
})
123+
}
124+
}
125+
112126
export const actions = {
113127
setPatientInContext
114128
};
@@ -130,8 +144,20 @@ export default function patientReducer (state = initialState, action) {
130144
result = copy
131145
break
132146
case 'UPDATE_CONTACT_DATA':
133-
let id = action.payload[1].id - 1
134-
copy[action.payload[0]].contacts[id] = action.payload[1]
147+
const contactIndexForUpdation = _.findIndex(copy[action.payload[0]].contacts, (c) => {
148+
return c.id == action.payload[1].id
149+
})
150+
copy[action.payload[0]].contacts[contactIndexForUpdation] = action.payload[1]
151+
result = copy
152+
break
153+
case 'DELETING_CONTACT':
154+
const contactIndexForDeletion = _.findIndex(copy[action.payload[0]].contacts, (c) => {
155+
if (c && c.hasOwnProperty('id')) {
156+
return c.id == action.payload[1]
157+
}
158+
})
159+
delete copy[action.payload[0]].contacts[contactIndexForDeletion]
160+
console.log(copy)
135161
result = copy
136162
break
137163
default:

0 commit comments

Comments
 (0)