1
|
|
|
import { fromJS, List } from 'immutable'; |
2
|
|
|
import { generateLastUpdate } from './../../util/lastUpdate'; |
3
|
|
|
|
4
|
|
|
import { getTreePathFromId } from './../../util/getTreePathFromId'; |
5
|
|
|
import { moveTreeNode } from './../../util/moveTreeNode'; |
6
|
|
|
import { setTreeValue } from './../../util/setTreeValue'; |
7
|
|
|
import { treeToFlatList } from './../../util/treeToFlatList'; |
8
|
|
|
import { setKeysInData } from './../../util/getData'; |
9
|
|
|
|
10
|
|
|
export const setData = (state, { |
11
|
|
|
currentRecords, data, gridType, stateKey, treeData, total |
12
|
|
|
}) => { |
13
|
|
|
|
14
|
|
|
const keyedData = setKeysInData(data); |
15
|
|
|
|
16
|
|
|
return state.setIn([stateKey], fromJS({ |
17
|
|
|
data: keyedData, |
18
|
|
|
proxy: keyedData, |
19
|
|
|
total: total || keyedData.count(), |
20
|
|
|
treeData: treeData, |
21
|
|
|
gridType: gridType || 'grid', |
22
|
|
|
currentRecords: currentRecords |
23
|
|
|
? List(currentRecords) |
24
|
|
|
: keyedData, |
25
|
|
|
lastUpdate: generateLastUpdate() |
26
|
|
|
})); |
27
|
|
|
|
28
|
|
|
}; |
29
|
|
|
|
30
|
|
|
export const setPartialTreeData = (state, { |
31
|
|
|
data, parentId, showTreeRootNode, stateKey |
32
|
|
|
}) => { |
33
|
|
|
|
34
|
|
|
const tree = state.getIn([stateKey, 'treeData']); |
35
|
|
|
const flat = state.getIn([stateKey, 'data']); |
36
|
|
|
const pathToNode = [ |
37
|
|
|
-1, ...getTreePathFromId(flat, parentId) |
38
|
|
|
]; |
39
|
|
|
const updatedTree = setTreeValue( |
40
|
|
|
tree, pathToNode, { children: data } |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
let updatedFlat = treeToFlatList(updatedTree); |
44
|
|
|
|
45
|
|
|
if (!showTreeRootNode) { |
46
|
|
|
updatedFlat = updatedFlat.shift(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return state.mergeIn([stateKey], { |
50
|
|
|
data: updatedFlat, |
51
|
|
|
currentRecords: updatedFlat, |
52
|
|
|
treeData: updatedTree, |
53
|
|
|
proxy: updatedFlat, |
54
|
|
|
total: updatedFlat.count(), |
55
|
|
|
lastUpdate: generateLastUpdate() |
56
|
|
|
}); |
57
|
|
|
}; |
58
|
|
|
|
59
|
|
|
export const dismissEditor = (state, { stateKey }) => { |
60
|
|
|
const previousData = state.getIn([stateKey, 'data']); |
61
|
|
|
const previousProxy = state.getIn([stateKey, 'proxy']); |
62
|
|
|
let previousTotal = state.getIn([stateKey, 'total']); |
63
|
|
|
|
64
|
|
|
// upon dismiss, if a new row was in edit |
65
|
|
|
// but isn't save, update the total to reflect that |
66
|
|
|
if (previousData |
67
|
|
|
&& previousProxy |
68
|
|
|
&& previousData.size > previousProxy.size) { |
69
|
|
|
previousTotal = previousProxy.size; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (state.get(stateKey)) { |
73
|
|
|
return state.mergeIn([stateKey], fromJS({ |
74
|
|
|
data: previousProxy, |
75
|
|
|
proxy: previousProxy, |
76
|
|
|
currentRecords: previousProxy, |
77
|
|
|
total: previousTotal, |
78
|
|
|
isEditing: false, |
79
|
|
|
lastUpdate: generateLastUpdate() |
80
|
|
|
})); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return state; |
84
|
|
|
}; |
85
|
|
|
|
86
|
|
|
export const removeRow = (state, { stateKey, rowIndex }) => { |
87
|
|
|
const remainingRows = state |
88
|
|
|
.getIn([stateKey, 'data']) |
89
|
|
|
.remove(rowIndex || 0, 1); |
90
|
|
|
|
91
|
|
|
return state.mergeIn([stateKey], fromJS({ |
92
|
|
|
data: remainingRows, |
93
|
|
|
proxy: remainingRows, |
94
|
|
|
currentRecords: remainingRows, |
95
|
|
|
lastUpdate: generateLastUpdate() |
96
|
|
|
})); |
97
|
|
|
}; |
98
|
|
|
|
99
|
|
|
export const updateRow = (state, { rowIndex, stateKey, values }) => { |
100
|
|
|
|
101
|
|
|
const data = state.getIn([stateKey, 'data']); |
102
|
|
|
const row = data |
103
|
|
|
? data.get(rowIndex) |
104
|
|
|
: null; |
105
|
|
|
|
106
|
|
|
if (!row) { |
107
|
|
|
return state; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
const updatedRow = row.merge(values); |
111
|
|
|
const updatedData = state.getIn([stateKey, 'data']) |
112
|
|
|
.set(rowIndex, updatedRow); |
113
|
|
|
|
114
|
|
|
return state.mergeIn([stateKey], { |
115
|
|
|
data: updatedData, |
116
|
|
|
proxy: updatedData, |
117
|
|
|
currentRecords: updatedData, |
118
|
|
|
lastUpdate: generateLastUpdate() |
119
|
|
|
}); |
120
|
|
|
|
121
|
|
|
}; |
122
|
|
|
|
123
|
|
|
export const addNewRow = (state, { rowId, stateKey }) => { |
124
|
|
|
const existingState = state.get(stateKey); |
125
|
|
|
const isEditing = existingState && existingState.get('isEditing'); |
126
|
|
|
let data = existingState && existingState.get('data'); |
127
|
|
|
|
128
|
|
|
if (existingState && isEditing) { |
129
|
|
|
return state; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
let newRow = data |
133
|
|
|
&& data.size > 0 |
134
|
|
|
&& data.get(0) |
135
|
|
|
? data.get(0).map((k, v) => v = '') |
|
|
|
|
136
|
|
|
: fromJS({}); |
137
|
|
|
|
138
|
|
|
newRow = newRow.set('_key', rowId); |
139
|
|
|
|
140
|
|
|
if (!data) { |
141
|
|
|
data = new List(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
const newData = data.unshift(newRow); |
145
|
|
|
|
146
|
|
|
return state.mergeIn([stateKey], fromJS({ |
147
|
|
|
data: newData, |
148
|
|
|
proxy: data, |
149
|
|
|
isEditing: true, |
150
|
|
|
lastUpdate: generateLastUpdate(), |
151
|
|
|
total: newData.size |
152
|
|
|
})); |
153
|
|
|
}; |
154
|
|
|
|
155
|
|
|
export const moveNode = (state, { |
156
|
|
|
current, next, showTreeRootNode, stateKey |
157
|
|
|
}) => { |
158
|
|
|
const nextPath = List(next.path); |
159
|
|
|
const tree = state.getIn([stateKey, 'treeData']); |
160
|
|
|
const currentPath = List(current.path); |
161
|
|
|
|
162
|
|
|
const newTreeMove = moveTreeNode( |
163
|
|
|
tree, |
164
|
|
|
current.index, |
165
|
|
|
currentPath, |
166
|
|
|
next.index, |
167
|
|
|
nextPath |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
let flatMove = treeToFlatList(newTreeMove); |
171
|
|
|
|
172
|
|
|
// remove root-node |
173
|
|
|
if (!showTreeRootNode) { |
174
|
|
|
flatMove = flatMove.shift(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return state.mergeIn([stateKey], { |
178
|
|
|
data: flatMove, |
179
|
|
|
currentRecords: flatMove, |
180
|
|
|
treeData: newTreeMove, |
181
|
|
|
proxy: flatMove, |
182
|
|
|
lastUpdate: generateLastUpdate() |
183
|
|
|
}); |
184
|
|
|
}; |
185
|
|
|
|
186
|
|
|
export const setTreeNodeVisibility = (state, { |
187
|
|
|
id, showTreeRootNode, stateKey |
188
|
|
|
}) => { |
189
|
|
|
|
190
|
|
|
const flat = state.getIn([stateKey, 'data']); |
191
|
|
|
const tree = state.getIn([stateKey, 'treeData']); |
192
|
|
|
|
193
|
|
|
const currentVisibility = !!flat |
194
|
|
|
.find(node => node.get('_id') === id).get('_hideChildren'); |
195
|
|
|
|
196
|
|
|
const path = [-1, ...getTreePathFromId(flat, id)]; |
197
|
|
|
|
198
|
|
|
const updatedTree = setTreeValue( |
199
|
|
|
tree, path, { _hideChildren: !currentVisibility |
200
|
|
|
}); |
201
|
|
|
|
202
|
|
|
let updatedList = treeToFlatList(updatedTree); |
203
|
|
|
|
204
|
|
|
// remove root-node |
205
|
|
|
if (!showTreeRootNode) { |
206
|
|
|
updatedList = updatedList.shift(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
state = state.setIn([stateKey, 'data'], updatedList); |
210
|
|
|
state = state.setIn([stateKey, 'currentRecords'], updatedList); |
211
|
|
|
state = state.setIn([stateKey, 'treeData'], updatedTree); |
212
|
|
|
state = state.setIn([stateKey, 'proxy'], updatedList); |
213
|
|
|
state = state.setIn([stateKey, 'lastUpdate'], generateLastUpdate()); |
214
|
|
|
|
215
|
|
|
return state; |
216
|
|
|
}; |
217
|
|
|
|
218
|
|
|
export const saveRow = (state, { rowIndex, stateKey, values}) => { |
219
|
|
|
const data = state |
220
|
|
|
.getIn([stateKey, 'data']) |
221
|
|
|
.set(rowIndex, fromJS(values)); |
222
|
|
|
|
223
|
|
|
return state.mergeIn([stateKey], fromJS({ |
224
|
|
|
data: data, |
225
|
|
|
proxy: data, |
226
|
|
|
currentRecords: data, |
227
|
|
|
lastUpdate: generateLastUpdate() |
228
|
|
|
})); |
229
|
|
|
}; |
230
|
|
|
|
231
|
|
|
export const sortData = (state, { data, stateKey }) => |
232
|
|
|
state.mergeIn([stateKey], { |
233
|
|
|
data: data, |
234
|
|
|
lastUpdate: generateLastUpdate() |
235
|
|
|
}); |
236
|
|
|
|
237
|
|
|
export const clearFilter = (state, { stateKey }) => { |
238
|
|
|
const proxy = state.getIn([stateKey, 'proxy']); |
239
|
|
|
const prevData = state.getIn([stateKey, 'data']); |
240
|
|
|
const recs = proxy || prevData; |
241
|
|
|
|
242
|
|
|
return state.mergeIn([stateKey], { |
243
|
|
|
data: recs, |
244
|
|
|
proxy: recs, |
245
|
|
|
currentRecords: recs, |
246
|
|
|
lastUpdate: generateLastUpdate() |
247
|
|
|
}); |
248
|
|
|
}; |
249
|
|
|
|
250
|
|
|
export const filterData = (state, { data, stateKey }) => |
251
|
|
|
state.mergeIn([stateKey], { |
252
|
|
|
data: data, |
253
|
|
|
lastUpdate: generateLastUpdate() |
254
|
|
|
}); |
255
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.