Skip to content

Commit dd8cc34

Browse files
committed
Created GraphQL Middleware and changed Patient retrieval and creation to use new graphQL middleware
1 parent 6377412 commit dd8cc34

File tree

5 files changed

+154
-25
lines changed

5 files changed

+154
-25
lines changed

src/redux/configureStore.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import thunk from 'redux-thunk';
22
import rootReducer from './modules';
33
import { fetchMiddleware } from './middleware/FetchMiddleware';
4+
import { graphqlMiddleware } from './middleware/graphqlMiddleware';
45
import {
56
applyMiddleware,
67
compose,
@@ -10,7 +11,7 @@ import {
1011
export default function configureStore (initialState) {
1112
let createStoreWithMiddleware;
1213

13-
const middleware = applyMiddleware(thunk, fetchMiddleware);
14+
const middleware = applyMiddleware(thunk, fetchMiddleware, graphqlMiddleware);
1415

1516
if (__DEBUG__) {
1617
createStoreWithMiddleware = compose(

src/redux/middleware/FetchMiddleware.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import { notificationManager } from 'utils/NotificationManager';
22

33
const fetchMiddleware = store => next => action => {
44
next(action);
5-
5+
let options;
66
if (action.meta && action.meta.endpoint) {
7-
let options;
87
switch (action.meta.method) {
98
case 'POST':
109
options = {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { notificationManager } from 'utils/NotificationManager';
2+
3+
const graphqlMiddleware = store => next => action => {
4+
next(action);
5+
6+
if (action.meta && action.meta.query) {
7+
let options;
8+
options = {
9+
method: 'post',
10+
headers: {
11+
'Content-Type': 'application/json'
12+
},
13+
body: JSON.stringify({
14+
query: action.meta.query,
15+
variables: action.meta.variables,
16+
})
17+
};
18+
fetch(`${process.env.BACKEND_API_URL}/graphql`, options)
19+
.then(function (response) {
20+
return response.json();
21+
}).then(function (json) {
22+
if (json.status === 'error') {
23+
if (json.message) {
24+
notificationManager.addNotification({
25+
message: json.message,
26+
level: 'error'
27+
});
28+
}
29+
if (action.meta.error) {
30+
var errorResult = action.meta.error(json, action.payload);
31+
// Check to see if the result has a type attribute, if it does then
32+
// assume its a Redux action and dispatch it.
33+
if (errorResult && errorResult.type) {
34+
next(errorResult);
35+
}
36+
}
37+
return;
38+
}
39+
if (action.meta.success) {
40+
var successResult = action.meta.success(json.data, action.payload);
41+
// Check to see if the result has a type attribute, if it does then
42+
// assume its a Redux action and dispatch it.
43+
if (successResult && successResult.type) {
44+
next(successResult);
45+
}
46+
}
47+
}).catch(function (ex) {
48+
console.log('parsing failed', ex);
49+
});
50+
console.log('fetching: ' + action.meta.query);
51+
}
52+
};
53+
54+
export { graphqlMiddleware as graphqlMiddleware };

src/redux/modules/patient.js

Lines changed: 95 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,58 @@ export const createPatient = createAction(
6565
return {patient: newPatient};
6666
},
6767
(patient) => {
68-
const body = {
69-
firstNames: patient.firstName,
70-
lastNames: patient.lastName,
71-
gender: patient.gender,
72-
dateOfBirth: patient.dob.toISOString(),
73-
address: patient.address,
74-
mrn: patient.mrn,
75-
tumorType: patient.tumorType,
76-
isSurgical: (patient.surgical) ? 'true' : 'false',
77-
phone: patient.phone,
78-
email: patient.email,
79-
};
68+
const query = `mutation insertPatient (
69+
$firstname: String!,
70+
$surname: String!,
71+
$gender: String!,
72+
$dob: String!,
73+
$address: String!,
74+
$mrn: Int!,
75+
$tumorType: String!,
76+
$surgical: String!,
77+
$phone: String!,
78+
$email: String!
79+
) {
80+
patient: createPatient(
81+
firstname: $firstname,
82+
surname: $surname,
83+
gender: $gender,
84+
dob: $dob,
85+
address: $address,
86+
mrn: $mrn,
87+
tumorType: $tumorType,
88+
surgical: $surgical,
89+
phone: $phone,
90+
email: $email
91+
) {
92+
id,
93+
mrn,
94+
surname
95+
firstname,
96+
gender,
97+
tumorType,
98+
surgical,
99+
dob,
100+
address,
101+
phone,
102+
email,
103+
}
104+
}`;
105+
const variables = `{
106+
"firstname": "${patient.firstName}",
107+
"surname": "${patient.lastName}",
108+
"gender": "${patient.gender}",
109+
"dob": "${patient.dob.toISOString()}",
110+
"address": "${patient.address}",
111+
"mrn": ${patient.mrn},
112+
"tumorType": "${patient.tumorType}",
113+
"surgical": "${(patient.surgical) ? 'true' : 'false'}",
114+
"phone": "${patient.phone}",
115+
"email": "${patient.email}"
116+
}`;
80117
return {
81-
endpoint: `${process.env.BACKEND_API_URL}/patient/`,
82-
method: 'POST',
83-
body: body,
118+
query: query,
119+
variables: variables,
84120
success: updatePatient,
85121
error: removePatient,
86122
};
@@ -135,7 +171,7 @@ export const followUpPDF = createAction(
135171
export const updatePatient = createAction(
136172
UPDATE_PATIENT,
137173
(patient, originalPayload) => {
138-
return {patient, token: originalPayload.patient.token};
174+
return {patient: patient.patient, token: originalPayload.patient.token};
139175
}
140176
);
141177

@@ -149,7 +185,7 @@ export const removePatient = createAction(
149185
const fetchPatient = createAction(
150186
FETCH_PATIENT,
151187
(patientJson) => {
152-
return {patient: patientJson};
188+
return {patient: patientJson.patient};
153189
}
154190
);
155191

@@ -166,7 +202,7 @@ const setPatientSearchResults = createAction(
166202
SET_PATIENT_SEARCH_RESULTS,
167203
(patientsJson) => {
168204
return {
169-
patients: patientsJson
205+
patients: patientsJson.patients
170206
};
171207
}
172208
);
@@ -175,8 +211,23 @@ export const searchPatients = createAction(
175211
SEARCH_PATIENTS,
176212
null,
177213
(patientId) => {
214+
const query = `query getAllPatients {
215+
patients {
216+
id,
217+
mrn,
218+
surname
219+
firstname,
220+
gender,
221+
tumorType,
222+
surgical,
223+
dob,
224+
address,
225+
phone,
226+
email,
227+
}
228+
}`;
178229
return {
179-
endpoint: `${process.env.BACKEND_API_URL}/patient/`,
230+
query: query,
180231
success: setPatientSearchResults
181232
};
182233
}
@@ -188,8 +239,32 @@ export const fetchPatientFromServer = createAction(
188239
return {patientId: patientId};
189240
},
190241
(patientId) => {
242+
const query = `query getPatient ($id: Int!) {
243+
patient(id: $id) {
244+
id,
245+
mrn,
246+
ehrId,
247+
surname
248+
firstname,
249+
gender,
250+
tumorType,
251+
surgical,
252+
dob,
253+
address,
254+
phone,
255+
email,
256+
allergies{
257+
name,
258+
date
259+
}
260+
}
261+
}`;
262+
const variables = `{
263+
"id": ${patientId}
264+
}`;
191265
return {
192-
endpoint: `${process.env.BACKEND_API_URL}/patient/${patientId}`,
266+
query: query,
267+
variables: variables,
193268
success: fetchPatient
194269
};
195270
}

src/views/AddPatientView.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ export class AddPatientView extends Component {
135135
// when resetting
136136
>
137137
<option></option>
138-
<option value='PROSTATE'>Prostate</option>
138+
<option value='Prostate'>Prostate</option>
139139
<option value='CNS'>CNS</option>
140-
<option value='BREAST'>Breast</option>
140+
<option value='Breast'>Breast</option>
141141
</select>
142142
{tumorType.touched && tumorType.error && <span className={styles['apv-error-span']}>{tumorType.error}</span>}
143143
</div>

0 commit comments

Comments
 (0)