1
|
|
|
import { |
2
|
|
|
ERROR_OCCURRED, |
3
|
|
|
HIDE_HEADER, |
4
|
|
|
MOVE_NODE, |
5
|
|
|
RESIZE_COLUMNS, |
6
|
|
|
SET_COLUMNS, |
7
|
|
|
SET_DATA, |
8
|
|
|
SET_SORT_DIRECTION, |
9
|
|
|
SET_TREE_DATA_PARTIAL, |
10
|
|
|
SET_TREE_NODE_VISIBILITY, |
11
|
|
|
SORT_DATA |
12
|
|
|
} from '../constants/ActionTypes'; |
13
|
|
|
|
14
|
|
|
import { setLoaderState } from '../actions/plugins/loader/LoaderActions'; |
15
|
|
|
|
16
|
|
|
import { dismissEditor } from '../actions/plugins/editor/EditorActions'; |
17
|
|
|
|
18
|
|
|
import { keyGenerator } from '../util/keyGenerator'; |
19
|
|
|
|
20
|
|
|
import { treeToFlatList } from '../util/treeToFlatList'; |
21
|
|
|
|
22
|
|
|
import Request from '../components/plugins/ajax/Request'; |
23
|
|
|
|
24
|
|
|
export function getAsyncData({ |
25
|
|
|
stateKey, dataSource, type, showTreeRootNode, extraParams = {} |
26
|
|
|
}) { |
27
|
|
|
|
28
|
|
|
return (dispatch) => { |
29
|
|
|
|
30
|
|
|
dispatch(dismissEditor({ stateKey })); |
31
|
|
|
|
32
|
|
|
dispatch( |
33
|
|
|
setLoaderState({state: true, stateKey }) |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
if (typeof dataSource === 'function') { |
37
|
|
|
|
38
|
|
|
// passing extraParams.parentId |
39
|
|
|
// to custom func so they can do partial |
40
|
|
|
// loading |
41
|
|
|
dataSource(extraParams).then((response) => { |
42
|
|
|
|
43
|
|
|
if (response && response.data) { |
44
|
|
|
|
45
|
|
|
dispatch( |
46
|
|
|
setLoaderState({ state: false, stateKey }) |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
if (type !== 'tree') { |
50
|
|
|
|
51
|
|
|
dispatch({ |
52
|
|
|
type: SET_DATA, |
53
|
|
|
data: response.data, |
54
|
|
|
total: response.total, |
55
|
|
|
currentRecords: response.data, |
56
|
|
|
success: true, |
57
|
|
|
stateKey |
58
|
|
|
}); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
else { |
62
|
|
|
// upon the return of read |
63
|
|
|
// response needs to clarify |
64
|
|
|
// whether this is a partial update |
65
|
|
|
dispatch(setTreeData({ |
66
|
|
|
data: response.data, |
67
|
|
|
stateKey, |
68
|
|
|
showTreeRootNode, |
69
|
|
|
parentId: extraParams.parentId, |
70
|
|
|
partial: response.partial |
71
|
|
|
})); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (response && !response.data) { |
78
|
|
|
/* eslint-disable no-console */ |
79
|
|
|
console.warn( |
80
|
|
|
`A response was recieved |
81
|
|
|
but no data entry was found` |
82
|
|
|
); |
83
|
|
|
console.warn( |
84
|
|
|
`Please see |
85
|
|
|
https://github.com/bencripps/react-redux-grid |
86
|
|
|
for documentation` |
87
|
|
|
); |
88
|
|
|
/* eslint-enable no-console */ |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
dispatch( |
92
|
|
|
setLoaderState({ state: false, stateKey }) |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
dispatch({ |
96
|
|
|
type: ERROR_OCCURRED, |
97
|
|
|
error: 'Unable to Retrieve Grid Data', |
98
|
|
|
errorOccurred: true, |
99
|
|
|
stateKey |
100
|
|
|
}); |
101
|
|
|
|
102
|
|
|
}); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
else if (typeof dataSource === 'string') { |
|
|
|
|
106
|
|
|
|
107
|
|
|
if (type !== 'tree') { |
108
|
|
|
|
109
|
|
|
return Request.api({ |
110
|
|
|
route: dataSource, |
111
|
|
|
method: 'GET' |
112
|
|
|
}).then((response) => { |
113
|
|
|
|
114
|
|
|
if (response && response.data) { |
115
|
|
|
|
116
|
|
|
dispatch({ |
117
|
|
|
type: SET_DATA, |
118
|
|
|
data: response.data, |
119
|
|
|
total: response.total, |
120
|
|
|
currentRecords: response.data, |
121
|
|
|
success: true, |
122
|
|
|
stateKey |
123
|
|
|
}); |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
else { |
128
|
|
|
dispatch({ |
129
|
|
|
type: ERROR_OCCURRED, |
130
|
|
|
error: 'Unable to Retrieve Grid Data', |
131
|
|
|
errorOccurred: true, |
132
|
|
|
stateKey |
133
|
|
|
}); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
dispatch( |
137
|
|
|
setLoaderState({state: false, stateKey }) |
138
|
|
|
); |
139
|
|
|
}); |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
else { |
144
|
|
|
|
145
|
|
|
return Request.api({ |
146
|
|
|
route: dataSource, |
147
|
|
|
method: 'GET', |
148
|
|
|
queryStringParams: { |
149
|
|
|
parentId: extraParams.parentId |
150
|
|
|
} |
151
|
|
|
}).then((response) => { |
152
|
|
|
|
153
|
|
|
if (response && response.data) { |
154
|
|
|
|
155
|
|
|
// response needs to specify |
156
|
|
|
// whether this is full or partial update |
157
|
|
|
dispatch(setTreeData({ |
158
|
|
|
data: response.data, |
159
|
|
|
stateKey, |
160
|
|
|
showTreeRootNode, |
161
|
|
|
partial: response.partial, |
162
|
|
|
parentId: extraParams.parentId |
163
|
|
|
})); |
164
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
else { |
168
|
|
|
dispatch({ |
169
|
|
|
type: ERROR_OCCURRED, |
170
|
|
|
error: 'Unable to Retrieve Grid Data', |
171
|
|
|
errorOccurred: true, |
172
|
|
|
stateKey |
173
|
|
|
}); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
dispatch( |
177
|
|
|
setLoaderState({state: false, stateKey }) |
178
|
|
|
); |
179
|
|
|
}); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
}; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
export function setColumns({ columns, stateKey, stateful }) { |
187
|
|
|
|
188
|
|
|
let cols = columns; |
189
|
|
|
|
190
|
|
|
if (!cols[0].id) { |
191
|
|
|
cols = columns.map((col) => { |
192
|
|
|
col.id = keyGenerator(col.name, 'grid-column'); |
193
|
|
|
return col; |
194
|
|
|
}); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return { type: SET_COLUMNS, columns: cols, stateKey, stateful }; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
export function setSortDirection({ |
201
|
|
|
columns, id, sortDirection, stateKey |
202
|
|
|
}) { |
203
|
|
|
|
204
|
|
|
let cols = columns; |
|
|
|
|
205
|
|
|
|
206
|
|
|
cols = columns.map((col) => { |
207
|
|
|
|
208
|
|
|
if (col.id === id) { |
209
|
|
|
col.sortDirection = sortDirection; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
else { |
213
|
|
|
// to do: remove this if we want to build |
214
|
|
|
// up the sorts |
215
|
|
|
col.sortDirection = null; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return col; |
219
|
|
|
}); |
220
|
|
|
|
221
|
|
|
return { type: SET_SORT_DIRECTION, columns: cols, stateKey }; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
export function doLocalSort({ data, stateKey }) { |
225
|
|
|
return { type: SORT_DATA, data, stateKey }; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
export function doRemoteSort( |
229
|
|
|
{ dataSource, pageIndex, pageSize, sortParams, stateKey } |
230
|
|
|
) { |
231
|
|
|
|
232
|
|
|
return (dispatch) => { |
233
|
|
|
|
234
|
|
|
dispatch( |
235
|
|
|
setLoaderState({state: true, stateKey }) |
236
|
|
|
); |
237
|
|
View Code Duplication |
|
|
|
|
|
238
|
|
|
if (typeof dataSource === 'function') { |
239
|
|
|
return dataSource({}, {}, sortParams).then((response) => { |
240
|
|
|
|
241
|
|
|
if (response && response.data) { |
242
|
|
|
|
243
|
|
|
dispatch({ |
244
|
|
|
type: SET_DATA, |
245
|
|
|
data: response.data, |
246
|
|
|
total: response.total, |
247
|
|
|
currentRecords: response.data, |
248
|
|
|
success: true, |
249
|
|
|
stateKey |
250
|
|
|
}); |
251
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
else { |
255
|
|
|
|
256
|
|
|
if (response && !response.data) { |
257
|
|
|
/* eslint-disable no-console */ |
258
|
|
|
console.warn( |
259
|
|
|
`A response was recieved but no data |
260
|
|
|
entry was found` |
261
|
|
|
); |
262
|
|
|
console.warn( |
263
|
|
|
`Please see |
264
|
|
|
https://github.com/bencripps/react-redux-grid |
265
|
|
|
for documentation` |
266
|
|
|
); |
267
|
|
|
/* eslint-enable no-console */ |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
dispatch({ |
271
|
|
|
type: ERROR_OCCURRED, |
272
|
|
|
error: 'Unable to Retrieve Grid Data', |
273
|
|
|
errorOccurred: true, |
274
|
|
|
stateKey |
275
|
|
|
}); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
dispatch( |
279
|
|
|
setLoaderState({state: false, stateKey }) |
280
|
|
|
); |
281
|
|
|
}); |
282
|
|
|
|
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
return Request.api({ |
286
|
|
|
route: dataSource, |
287
|
|
|
method: 'POST', |
288
|
|
|
data: { |
289
|
|
|
pageIndex: pageIndex, |
290
|
|
|
pageSize: pageSize, |
291
|
|
|
sort: sortParams.sort |
292
|
|
|
} |
293
|
|
|
}).then((response) => { |
294
|
|
|
|
295
|
|
|
if (response && response.data) { |
296
|
|
|
|
297
|
|
|
dispatch({ |
298
|
|
|
type: SET_DATA, |
299
|
|
|
data: response.data, |
300
|
|
|
total: response.total, |
301
|
|
|
currentRecords: response.data, |
302
|
|
|
success: true |
303
|
|
|
}); |
304
|
|
|
|
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
else { |
308
|
|
|
dispatch({ |
309
|
|
|
type: ERROR_OCCURRED, |
310
|
|
|
error: 'Unable to Retrieve Grid Data', |
311
|
|
|
errorOccurred: true |
312
|
|
|
}); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
dispatch( |
316
|
|
|
setLoaderState({state: false, stateKey }) |
317
|
|
|
); |
318
|
|
|
}); |
319
|
|
|
|
320
|
|
|
}; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
export function setColumnVisibility({ |
324
|
|
|
columns, column, isHidden, stateKey, stateful |
325
|
|
|
}) { |
326
|
|
|
const hidden = !isHidden; |
327
|
|
|
|
328
|
|
|
const columnsArr = columns.map((col) => { |
329
|
|
|
if (col.name === column.name) { |
330
|
|
|
col.hidden = hidden; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
return col; |
334
|
|
|
}); |
335
|
|
|
|
336
|
|
|
return { type: SET_COLUMNS, columns: columnsArr, stateKey, stateful }; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
export function resizeColumns({ |
340
|
|
|
width, id, nextColumn, columns, stateKey, stateful |
341
|
|
|
}) { |
342
|
|
|
|
343
|
|
|
const cols = columns.map((col) => { |
344
|
|
|
|
345
|
|
|
if (col.id === id) { |
346
|
|
|
col.width = `${width}%`; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
else if (col.id === nextColumn.id) { |
350
|
|
|
col.width = `${nextColumn.width}%`; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
return col; |
354
|
|
|
|
355
|
|
|
}); |
356
|
|
|
|
357
|
|
|
return { |
358
|
|
|
type: RESIZE_COLUMNS, |
359
|
|
|
stateKey, |
360
|
|
|
columns: cols, |
361
|
|
|
stateful |
362
|
|
|
}; |
363
|
|
|
|
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
export function setData({ data, stateKey }) { |
367
|
|
|
return { type: SET_DATA, data, stateKey }; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
export function setTreeData({ |
371
|
|
|
data, stateKey, showTreeRootNode, partial, parentId |
372
|
|
|
}) { |
373
|
|
|
|
374
|
|
|
// if this is a partial update to |
375
|
|
|
// a tree grid, dispatch separate action; |
376
|
|
|
if (partial) { |
377
|
|
|
return { |
378
|
|
|
type: SET_TREE_DATA_PARTIAL, |
379
|
|
|
data: data, |
380
|
|
|
stateKey, |
381
|
|
|
gridType: 'tree', |
382
|
|
|
showTreeRootNode, |
383
|
|
|
parentId |
384
|
|
|
}; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
const flat = treeToFlatList(data); |
388
|
|
|
|
389
|
|
|
// remove root node |
390
|
|
|
if (!showTreeRootNode) { |
391
|
|
|
flat.shift(); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
return { |
395
|
|
|
type: SET_DATA, |
396
|
|
|
data: flat, |
397
|
|
|
stateKey, |
398
|
|
|
gridType: 'tree', |
399
|
|
|
treeData: data |
400
|
|
|
}; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
export function setTreeNodeVisibility({ |
404
|
|
|
id, visible, stateKey, showTreeRootNode |
405
|
|
|
}) { |
406
|
|
|
return { |
407
|
|
|
type: SET_TREE_NODE_VISIBILITY, |
408
|
|
|
id, |
409
|
|
|
visible, |
410
|
|
|
stateKey, |
411
|
|
|
showTreeRootNode |
412
|
|
|
}; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
export function moveNode({ |
416
|
|
|
stateKey, current, next, showTreeRootNode |
417
|
|
|
}) { |
418
|
|
|
return { |
419
|
|
|
type: MOVE_NODE, |
420
|
|
|
stateKey, |
421
|
|
|
current, |
422
|
|
|
next, |
423
|
|
|
showTreeRootNode |
424
|
|
|
}; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
export function setHeaderVisibility({ hidden, stateKey }) { |
428
|
|
|
return { type: HIDE_HEADER, headerHidden: hidden, stateKey }; |
429
|
|
|
} |
430
|
|
|
|