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