Skip to content

Commit 1b5fe6c

Browse files
authored
Merge pull request #96 from GoTeamEpsilon/simplify_reducer
Simplify reducer
2 parents a09d15c + d706de9 commit 1b5fe6c

File tree

1 file changed

+15
-13
lines changed
  • samples/react-redux-patient-demographics-example/src/routes/Patient

1 file changed

+15
-13
lines changed

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export const updatePatientData = (data) => {
9090
console.debug(`updating basic patient data for ${getState().patient.patientInContext}`)
9191
dispatch({
9292
type : 'UPDATE_PATIENT_DATA',
93-
payload : [getState().patient.patientInContext, data]
93+
payload : data
9494
})
9595
resolve()
9696
})
@@ -103,7 +103,7 @@ export const updateContactData = (data) => {
103103
console.debug(`updating contact data for ${getState().patient.patientInContext}`)
104104
dispatch({
105105
type : 'UPDATE_CONTACT_DATA',
106-
payload : [getState().patient.patientInContext, data]
106+
payload : data
107107
})
108108
resolve()
109109
})
@@ -116,7 +116,7 @@ export const deleteContact = (data) => {
116116
console.debug(`deleting contact data for ${getState().patient.patientInContext}`)
117117
dispatch({
118118
type : 'DELETING_CONTACT',
119-
payload : [getState().patient.patientInContext, data]
119+
payload : data
120120
})
121121
resolve()
122122
})
@@ -129,7 +129,7 @@ export const startAddingNewContact = (data) => {
129129
console.debug(`starting to add contact data for ${getState().patient.patientInContext}`)
130130
dispatch({
131131
type : 'START_ADDING_CONTACT',
132-
payload : [getState().patient.patientInContext, data]
132+
payload : data
133133
})
134134
resolve()
135135
})
@@ -149,32 +149,34 @@ export default function patientReducer (state = initialState, action) {
149149
result = copy
150150
break
151151
case 'UPDATE_PATIENT_DATA':
152-
copy[action.payload[0]].basic = action.payload[1]
152+
copy[copy.patientInContext].basic = action.payload
153153
result = copy
154154
break
155155
case 'UPDATE_CONTACT_DATA':
156-
const contactIndexForUpdation = _.findIndex(copy[action.payload[0]].contacts, (c) => {
157-
return c.id === action.payload[1].id
156+
const contactIndexForUpdation = _.findIndex(copy[copy.patientInContext].contacts, (c) => {
157+
if (c && c.hasOwnProperty('id')) {
158+
return c.id === action.payload.id
159+
}
158160
})
159-
copy[action.payload[0]].contacts[contactIndexForUpdation] = action.payload[1]
161+
copy[copy.patientInContext].contacts[contactIndexForUpdation] = action.payload
160162
result = copy
161163
break
162164
case 'START_ADDING_CONTACT':
163-
const lastContact = _.last(copy[action.payload[0]].contacts)
165+
const lastContact = _.last(copy[copy.patientInContext].contacts)
164166
let newContactId = 0
165167
if (lastContact != null && lastContact.hasOwnProperty('id')) {
166168
newContactId = lastContact.id + 1
167169
}
168-
copy[action.payload[0]].contacts.push({ isNewContact: true, id: newContactId })
170+
copy[copy.patientInContext].contacts.push({ isNewContact: true, id: newContactId })
169171
result = copy
170172
break
171173
case 'DELETING_CONTACT':
172-
const contactIndexForDeletion = _.findIndex(copy[action.payload[0]].contacts, (c) => {
174+
const contactIndexForDeletion = _.findIndex(copy[copy.patientInContext].contacts, (c) => {
173175
if (c && c.hasOwnProperty('id')) {
174-
return c.id === action.payload[1]
176+
return c.id === action.payload
175177
}
176178
})
177-
delete copy[action.payload[0]].contacts[contactIndexForDeletion]
179+
delete copy[copy.patientInContext].contacts[contactIndexForDeletion]
178180
result = copy
179181
break
180182
default:

0 commit comments

Comments
 (0)