Newer
Older
import { fetchUtils } from 'react-admin';
import { stringify } from 'query-string';
import {idFromURL, backEndURL} from '../utils'
const apiUrl = backEndURL();
const httpClient = (url, options = {}) => {
options.credentials = 'include';
if (sessionStorage.getItem('arolios_auth') ) {
if (!options.headers) {
options.headers = new Headers({ Accept: 'application/json' });
}
const { token } = JSON.parse(sessionStorage.getItem('arolios_auth'));
options.headers.set('Authorization', `Bearer ${token}`);
}
return fetchUtils.fetchJson(url, options);
};
const CustomDataProvider = {
getList: (resource, params) => {
const { page, perPage } = params.pagination || {};
const { field, order } = params.sort || {};
const filter = params.filter || {};
const {prefix, suffix ,context, properties, i18n } = params.meta || {};
Philippe Coicadan
committed
const lang = localStorage.getItem ("arolios_requested_language") || sessionStorage.getItem ("arolios_default_language");
let query ='';
let querySep = '?';
if (i18n !== false) {
query += `${querySep}lang=${lang}`;
querySep = '&';
}
if (page >0 && perPage >0) {
const offset= (page-1)*perPage;
//const limit =perPage-1;
query += `${querySep}o=${offset}&l=${perPage}`;
querySep = '&';
}
if (field) {
//eliminate field translation
const field_arr1 = field.split('.');
const field_arr2 = field_arr1.map ( (item) => ( item.startsWith ('_t')) ? item.substring(2) : item);
const true_field = field_arr2.join('.');
query +=`${querySep}s=${true_field}`;
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
querySep = '&';
}
if (order) {
query +=`${querySep}d=${order}`;
querySep = '&';
}
if (Object.entries(filter).length > 0) {
query +=`${querySep}f=${JSON.stringify(filter)}`;
querySep = '&';
}
let url = `${apiUrl}/`;
if (prefix) {
url += `${prefix}/`
}
url += `${resource}`;
if (suffix) {
url += `/${suffix}`;
}
if (context) {
query += `${querySep}c=${context}`;
}
if (properties) {
query += `${querySep}p=${properties}`;
}
if (query.length >0) {
url += `${query}`;
}
if (resource === 'apps') {
return httpClient(url).then(({ headers, json }) => ({
data: json["elements"].map(record => ( {id:record.name, ...record})),
total: json["total"],
}));
} else if (resource === 'users') {
return httpClient(url).then(({ headers, json }) => ({
data: json["elements"].map(record => ( {id:record.identifier, ...record})),
total: json["total"],
}));
} else if (resource === 'languages') {
return httpClient(url).then(({ headers, json }) => ({
data: json["elements"].map(record => ( {id:record.code, ...record})),
total: json["total"],
}));
} else if (suffix === 'properties') { //list properties
return httpClient(url).then(({ headers, json }) => ({
data: json["elements"].map(record => ( {id:record.id_name, ...record})),
total: json["total"],
}));
} else if (suffix === 'values') { //list enum values
return httpClient(url).then(({ headers, json }) => ({
data: json["elements"].map(record => ( {id:record.name, ...record})),
total: json["total"],
}));
} else {
return httpClient(url).then(({ headers, json }) => ({
data: json["elements"].map(record => ( {id:idFromURL(record.url), ...record})),
total: json["total"],
}));
}
},
getOne: (resource, params) => {
Philippe Coicadan
committed
const lang = localStorage.getItem ("arolios_requested_language") || sessionStorage.getItem ("arolios_default_language");
let query = `?lang=${lang}`;
const querySep = '&';
const {prefix, suffix ,context } = params.meta || {};
if (prefix === 'settings') {
Philippe Coicadan
committed
const modelLang = localStorage.getItem("arolios_requested_language") || sessionStorage.getItem ("arolios_default_language");
return Promise.resolve({data: { id: params.id, requested_language: modelLang}});
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
}
let url = `${apiUrl}/`;
if (prefix) {
url += `${prefix}/`
}
url += `${resource}`;
if (suffix) {
url += `/${suffix}`;
}
url += `/${params.id}`;
if (context) {
query += `${querySep}c=${context}`;
}
if (query.length >0) {
url += `${query}`;
}
if (resource === 'apps') {
return httpClient(url).then(({ json }) => ({
data: { id: json['name'], ...json}
})
);
} else if (resource === 'users') {
return httpClient(url).then(({ json }) => ({
data: { id: json['identifier'], ...json}
})
);
} else {
return httpClient(url).then(({ json }) => ({
data: json
})
);
}
},
getMany: (resource, params) => {
const {prefix, getmany_context } = params.meta || {};
if (getmany_context === "values") {
// query of enumeration value
const ids = params.ids;
return Promise.resolve ({ data: [ { id:ids[0] }]});
} else if (getmany_context === "languages" ) {
// query of language
const ids = params.ids;
const query = { code: ids[0] };
const url = `${apiUrl}/${resource}?f=${JSON.stringify(query)}`;
return httpClient(url).then(({ headers, json }) => ({
data: json["elements"].map(record => ( {id:record.code, ...record})),
total: json["total"],
}));
} else if (getmany_context === "appassocs" ) {
// query of app assocations (apps, domains)
const ids = params.ids;
const instid =ids[0] ;
if (resource === 'apps' || resource === 'domains') {
return Promise.resolve ( { data: [ { id: instid }]});
} else if (resource === 'users') {
const ids = params.ids;
const instid = ids[0];
// variant of getOne
const url = `${apiUrl}/${resource}/${instid}`;
return httpClient(url).then(({ json }) => ({
data: [ { id: json['identifier'], ...json} ],
total: 1
}));
} else { // no case implemented
let url = `${apiUrl}/`;
if (prefix) {
url += `${prefix}/`
}
url += `${resource}/${instid}`;
return httpClient(url).then(({ headers, json }) => ({
Philippe Coicadan
committed
data: json.map(record => ( {id:idFromURL(record.url), ...record})),
Philippe Coicadan
committed
} else if (getmany_context === "references" ) {
const ids = params.ids;
const instid =ids[0] ;
// variant of getOne
Philippe Coicadan
committed
const lang = localStorage.getItem ("arolios_requested_language") || sessionStorage.getItem ("arolios_default_language");
Philippe Coicadan
committed
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
let query = `?lang=${lang}`;
const querySep = '&';
const {prefix, suffix ,context } = params.meta || {};
let url = `${apiUrl}/`;
// suffix of the list query is the resource
if (suffix) {
url += `${suffix}`;
}
url += `/${instid}`;
if (context) {
query += `${querySep}c=${context}`;
}
if (query.length >0) {
url += `${query}`;
}
return httpClient(url).then(({ json }) => ({
data: [ json ],
total: 1
}));
} else { // no case implemented
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
const query = {
filter: JSON.stringify({ ids: params.ids }),
};
const url = `${apiUrl}/${resource}?${stringify(query)}`;
return httpClient(url).then(({ json }) => ({ data: json }));
}
},
getManyReference: (resource, params) => { // no case implemented
const { page, perPage } = params.pagination;
const { field, order } = params.sort;
const query = {
sort: JSON.stringify([field, order]),
range: JSON.stringify([(page - 1) * perPage, page * perPage - 1]),
filter: JSON.stringify({
...params.filter,
[params.target]: params.id,
}),
};
const url = `${apiUrl}/${resource}?${stringify(query)}`;
return httpClient(url).then(({ headers, json }) => ({
data: json,
total: parseInt(headers.get('content-range').split('/').pop(), 10),
}));
},
create: (resource, params) =>
{
const {prefix, suffix ,context } = params.meta || {};
let url = `${apiUrl}/`;
if (prefix) {
url += `${prefix}/`
}
url += `${resource}`;
if (suffix) {
url += `/${suffix}`;
}
return httpClient(url, {
method: 'POST',
body: JSON.stringify(params.data)
})
.then(({ json }) => ({
data: (json) ? { ...params.data, id: idFromURL(json.url) } : { ...params.data, id: 0 }
}))
;
// }
},
update: (resource, params) =>
{
let query = '';
let querySep = '?';
const {prefix, suffix ,context } = params.meta || {};
if (prefix === 'settings') {
Philippe Coicadan
committed
localStorage.setItem("arolios_requested_language", params.data.requested_language);
Philippe Coicadan
committed
const url = `${apiUrl}/domains?lang=${params.data.requested_language}`
httpClient(url).then(({ headers, json }) => {
if (json["total"] === 1) {
sessionStorage.setItem('arolios_model_default_domain', JSON.stringify({ name: json["elements"][0]['name'], _tname: json["elements"][0]['_tname'] }));
} else {
sessionStorage.removeItem('arolios_model_default_domain');
};
});
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
return Promise.resolve({data: params.data});
}
let url = `${apiUrl}/`;
if (prefix) {
url += `${prefix}/`
}
url += `${resource}`;
if (suffix) {
url += `/${suffix}`;
}
url += `/${params.id}`;
if (context) {
query += `${querySep}c=${context}`;
}
if (query.length >0) {
url += `${query}`;
}
return httpClient(url, {
method: 'PATCH',
body: JSON.stringify(Object.keys(params.data)
.filter((key) => (key !== 'id'))
.reduce ( (obj, key) => { return Object.assign(obj, { [key]: params.data[key]});
}, {})
), }
)
.then(({ json }) => ({
data: (json) ? { ...params.data, id: idFromURL(json.url) } : params.data
}))
;
},
updateMany: (resource, params) => { //no case implemented
const query = {
filter: JSON.stringify({ id: params.ids}),
};
return httpClient(`${apiUrl}/${resource}?${stringify(query)}`, {
method: 'PUT',
body: JSON.stringify(params.data),
}).then(({ json }) => ({ data: json }));
},
delete: (resource, params) =>
{
const {prefix, suffix , instance } = params.meta || {};
let url = `${apiUrl}/`;
if (prefix) {
url += `${prefix}/`
}
url += `${resource}`;
if (suffix) {
url += `/${suffix}`;
}
if (instance !== false) {
url += `/${params.id}`;
}
return httpClient(url, {
method: 'DELETE', }
)
.then(({json}) => ({ data: json }) );
},
deleteMany: (resource, params) => { // no case implemented
const query = {
filter: JSON.stringify({ id: params.ids}),
};
return httpClient(`${apiUrl}/${resource}?${stringify(query)}`, {
method: 'DELETE',
body: JSON.stringify(params.data),
}).then(({ json }) => ({ data: json }));
},
} ;
export default CustomDataProvider;