-
Notifications
You must be signed in to change notification settings - Fork 6
/
dataProvider.js
262 lines (239 loc) · 8.38 KB
/
dataProvider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import get from "lodash/get";
import {
GET_LIST,
GET_ONE,
CREATE,
UPDATE,
HttpError,
} from "react-admin";
import moment from "moment";
import sort, { ASC, DESC } from 'sort-array-objects';
import pluginFilters from "../config/pluginFilters";
import { token, domainName } from "../token";
import fakePatientsProvider from "./fakePatientsProvider";
import fakeTestResultsProvider from "./fakeTestResultsProvider";
import newPatientsProvider from "./patientsProvider";
import { httpErrorAction } from '../actions/httpErrorAction';
const apiPatientsUser = 'api/patients';
/**
* This constant prepare data for requests (URL and options)
*
* @author Bogdan Shcherban <bsc@piogroup.net>
* @param {shape} type
* @param {string} resource
* @param {shape} params
*/
const convertDataRequestToHTTP = (type, resource, params) => {
let url = "";
const options = {};
switch (type) {
case GET_LIST: {
url = `${domainName}/${apiPatientsUser}/${localStorage.getItem('patientId')}/${resource}`;
if (!options.headers) {
options.headers = new Headers({ Accept: 'application/json' });
}
options.headers = {
Authorization: "Bearer " + token,
'X-Requested-With': "XMLHttpRequest",
};
break;
}
case GET_ONE:
url = `${domainName}/${apiPatientsUser}/${localStorage.getItem('patientId')}/${resource}/${params.id}`;
if (!options.headers) {
options.headers = new Headers({ Accept: 'application/json' });
}
options.headers = {
Authorization: "Bearer " + token,
'X-Requested-With': "XMLHttpRequest",
};
break;
case UPDATE:
let updateData = Object.assign({userId: localStorage.getItem('patientId')}, params.data);
if (resource === 'problems') {
let dateCreated = get(params, 'data.dateCreated', null);
let dateOfOnset = get(params, 'data.dateOfOnset', null);
updateData.dateCreated = moment(dateCreated).format('DD-MM-YYYY');
updateData.dateOfOnset = moment(dateOfOnset).format('YYYY-MM-DD');
}
url = `${domainName}/${apiPatientsUser}/${localStorage.getItem('patientId')}/${resource}/${params.id}`;
options.method = "PUT";
if (!options.headers) {
options.headers = new Headers({ Accept: 'application/json' });
}
options.headers = {
Authorization: "Bearer " + token,
'Content-Type': 'application/json',
'X-Requested-With': "XMLHttpRequest",
};
options.body = JSON.stringify(updateData);
break;
case CREATE:
let newData = Object.assign({userId: localStorage.getItem('patientId')}, params.data);
url = `${domainName}/${apiPatientsUser}/${localStorage.getItem('patientId')}/${resource}`;
options.method = "POST";
if (!options.headers) {
options.headers = new Headers({ Accept: 'application/json' });
}
options.headers = {
Authorization: "Bearer " + token,
'Content-Type': 'application/json',
'X-Requested-With': "XMLHttpRequest",
};
options.body = JSON.stringify(newData);
break;
default:
throw new Error(`Unsupported fetch action type ${type}`);
}
return { url, options };
};
/**
* This function extracts results from response
*
* @author Bogdan Shcherban <bsc@piogroup.net>
* @param {shape} response
* @return {array}
*/
function getResultsFromResponse(response) {
return response.map((item, id) => {
return Object.assign({
number: (id + 1),
id: item.sourceId
}, item);
});
}
/**
* This function cheks is current item consider to filter condition
*
* @author Bogdan Shcherban <bsc@piogroup.net>
* @param {shape} item
* @param {shape} filters
* @param {string} filterText
* @return {boolean}
*/
function isItemConsider(item, filters, filterText) {
let result = false;
filters.forEach(filterItem => {
let string = item[filterItem];
if (String(string).toLowerCase().search(filterText) >= 0) {
result = true;
}
});
return result;
}
/**
* This function filters response array
*
* @author Bogdan Shcherban <bsc@piogroup.net>
* @param {string} resource
* @param {array} results
* @param {shape} params
* @return {array}
*/
function getFilterResults(resource, results, params) {
const filterText = get(params, 'filter.filterText', null);
const filters = pluginFilters[resource];
return !filterText ? results : results.filter(item => isItemConsider(item, filters, filterText));
}
/**
* This function sorts response array
*
* @author Bogdan Shcherban <bsc@piogroup.net>
* @param {array} results
* @param {shape} params
* @return {array}
*/
function getSortedResults(results, params) {
const sortField = get(params, 'sort.field', null);
const sortOrder = (get(params, 'sort.order', null) === 'DESC') ? DESC : ASC;
return sort(results, [sortField], sortOrder);
}
/**
* This constant handle response data
*
* @author Bogdan Shcherban <bsc@piogroup.net>
* @param {shape} response
* @param {shape} type
* @param {string} resource
* @param {shape} params
*/
const convertHTTPResponse = (response, type, resource, params) => {
switch (type) {
case GET_LIST:
const pageNumber = get(params, 'pagination.page', 1);
const numberPerPage = get(params, 'pagination.perPage', 10);
const results = getResultsFromResponse(response);
const resultsFiltering = getFilterResults(resource, results, params);
const resultsSorting = getSortedResults(resultsFiltering, params);
const startItem = (pageNumber - 1) * numberPerPage;
const endItem = pageNumber * numberPerPage;
const paginationResults = resultsSorting.slice(startItem, endItem);
return {
data: paginationResults,
total: results.length,
};
case GET_ONE:
return {
data: Object.assign({id: response.sourceId}, response),
};
case UPDATE:
return params;
case CREATE:
const dataFromRequest = get(params, 'data', null);
const compositionUid = get(response, 'compositionUid', null);
let sourceID = '';
if (compositionUid) {
const compositionUidArray = compositionUid.split('::');
sourceID = compositionUidArray[0];
}
dataFromRequest.id = get(response, 'host', null) + '-' + sourceID;
dataFromRequest.isNew = true;
if (!get(params, 'source', null)) {
dataFromRequest.source = 'ethercis';
}
return {
data: dataFromRequest,
};
default:
return { data: 'No results' };
}
};
const dataProvider = (type, resource, params) => {
let { url, options } = convertDataRequestToHTTP(type, resource, params);
let responseInfo = '';
return fetch(url, options).then(response => {
responseInfo = get(response, 'status', null);
return response.json();
})
.then(res => {
if (responseInfo !== 200) {
responseInfo += '|' + get(res, 'error', null);
throw new HttpError(responseInfo);
}
return convertHTTPResponse(res, type, resource, params)
})
.catch(err => {
console.log('Error: ', err);
throw new Error(err);
});
};
/**
* This function provides requests/response to server
*
* @author Bogdan Shcherban <bsc@piogroup.net>
* @param {shape} type
* @param {string} resource
* @param {shape} params
*/
export default (type, resource, params) => {
// if (resource === `patients`) {
// return fakePatientsProvider(type, resource, params);
// }
if (resource === `patients`) {
return newPatientsProvider(type, resource, params);
}
if (resource === `labresults`) {
return fakeTestResultsProvider(type, resource, params);
}
return dataProvider(type, resource, params);
};