Conditions | 26 |
Paths | 26 |
Total Lines | 191 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like datasource.js ➔ dataSource often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import { fromJS } from 'immutable'; |
||
22 | export default function dataSource(state = initialState, action) { |
||
23 | switch (action.type) { |
||
24 | |||
25 | case SET_DATA: |
||
26 | return state.setIn([action.stateKey], fromJS({ |
||
27 | data: action.data, |
||
28 | proxy: action.data, |
||
29 | total: action.total || action.data.length, |
||
30 | treeData: action.treeData, |
||
31 | gridType: action.gridType || 'grid', |
||
32 | currentRecords: action.currentRecords || action.data, |
||
33 | lastUpdate: generateLastUpdate() |
||
34 | })); |
||
35 | |||
36 | case SET_TREE_DATA_PARTIAL: |
||
37 | const treeVals = state.getIn([action.stateKey, 'treeData']).toJS(); |
||
38 | const flattened = state.getIn([action.stateKey, 'data']).toJS(); |
||
39 | const pathToNode = [ |
||
40 | -1, ...getTreePathFromId(flattened, action.parentId) |
||
41 | ]; |
||
42 | const newTreeValues = setTreeValue( |
||
43 | treeVals, pathToNode, { children: action.data } |
||
44 | ); |
||
45 | |||
46 | const newFlatList = treeToFlatList(newTreeValues); |
||
47 | |||
48 | if (!action.showTreeRootNode) { |
||
49 | newFlatList.shift(); |
||
50 | } |
||
51 | |||
52 | return state.mergeIn([action.stateKey], fromJS({ |
||
53 | data: newFlatList, |
||
54 | proxy: newFlatList, |
||
55 | currentRecords: newFlatList, |
||
56 | treeData: newTreeValues, |
||
57 | total: newFlatList.length, |
||
58 | lastUpdate: generateLastUpdate() |
||
59 | })); |
||
60 | |||
61 | case DISMISS_EDITOR: |
||
62 | const previousData = state.getIn([action.stateKey, 'data']); |
||
63 | const previousProxy = state.getIn([action.stateKey, 'proxy']); |
||
64 | let previousTotal = state.getIn([action.stateKey, 'total']); |
||
65 | |||
66 | // upon dismiss, if a new row was in edit |
||
67 | // but isn't save, update the total to reflect that |
||
68 | if (previousData |
||
69 | && previousProxy |
||
70 | && previousData.size > previousProxy.size) { |
||
71 | previousTotal = previousProxy.size; |
||
72 | } |
||
73 | |||
74 | if (state.get(action.stateKey)) { |
||
75 | return state.mergeIn([action.stateKey], fromJS({ |
||
76 | data: previousProxy, |
||
77 | proxy: previousProxy, |
||
78 | currentRecords: previousProxy, |
||
79 | total: previousTotal, |
||
80 | isEditing: false, |
||
81 | lastUpdate: generateLastUpdate() |
||
82 | })); |
||
83 | } |
||
84 | |||
85 | return state; |
||
86 | |||
87 | case REMOVE_ROW: |
||
88 | const remainingRows = state |
||
89 | .getIn([action.stateKey, 'data']) |
||
90 | .remove(action.rowIndex || 0, 1); |
||
91 | |||
92 | return state.mergeIn([action.stateKey], fromJS({ |
||
93 | data: remainingRows, |
||
94 | proxy: remainingRows, |
||
95 | currentRecords: remainingRows, |
||
96 | lastUpdate: generateLastUpdate() |
||
97 | })); |
||
98 | |||
99 | case UPDATE_ROW: |
||
100 | |||
101 | const existingData = state.getIn([action.stateKey, 'data']); |
||
102 | const prevRow = existingData |
||
103 | ? existingData.get(action.rowIndex) |
||
104 | : null; |
||
105 | |||
106 | if (!prevRow) { |
||
107 | return state; |
||
108 | } |
||
109 | |||
110 | const updatedRow = prevRow.merge(action.values); |
||
111 | const updatedData = state.getIn([action.stateKey, 'data']) |
||
112 | .set(action.rowIndex, updatedRow); |
||
113 | |||
114 | return state.mergeIn([action.stateKey], { |
||
115 | data: updatedData, |
||
116 | proxy: updatedData, |
||
117 | currentRecords: updatedData, |
||
118 | lastUpdate: generateLastUpdate() |
||
119 | }); |
||
120 | |||
121 | case ADD_NEW_ROW: |
||
122 | |||
123 | const existingState = state.get(action.stateKey); |
||
124 | const isEditing = existingState && existingState.get('isEditing'); |
||
125 | const data = existingState && existingState.get('data'); |
||
126 | |||
127 | if (existingState && isEditing) { |
||
128 | return state; |
||
129 | } |
||
130 | |||
131 | const newRow = data |
||
132 | && data.size > 0 |
||
133 | && data.get(0) |
||
134 | ? data.get(0).map((k, v) => v = '') |
||
135 | : fromJS({}); |
||
136 | |||
137 | const newData = data.unshift(newRow); |
||
138 | |||
139 | return state.mergeIn([action.stateKey], fromJS({ |
||
140 | data: newData, |
||
141 | proxy: data, |
||
142 | isEditing: true, |
||
143 | lastUpdate: generateLastUpdate(), |
||
144 | total: newData.size |
||
145 | })); |
||
146 | |||
147 | case SET_TREE_NODE_VISIBILITY: |
||
148 | |||
149 | const treeFlatList = state.getIn([action.stateKey, 'data']).toJS(); |
||
150 | const tree = state.getIn([action.stateKey, 'treeData']).toJS(); |
||
151 | const currentVisibility = !!treeFlatList |
||
152 | .find(node => node._id === action.id)._hideChildren; |
||
153 | const path = [-1, ...getTreePathFromId(treeFlatList, action.id)]; |
||
154 | const newTree = setTreeValue( |
||
155 | tree, path, { _hideChildren: !currentVisibility |
||
156 | }); |
||
157 | const flattenedTree = treeToFlatList(newTree); |
||
158 | |||
159 | // remove root-node |
||
160 | if (!action.showTreeRootNode) { |
||
161 | flattenedTree.shift(); |
||
162 | } |
||
163 | |||
164 | return state.mergeIn([action.stateKey], fromJS({ |
||
165 | data: flattenedTree, |
||
166 | currentRecords: flattenedTree, |
||
167 | treeData: newTree, |
||
168 | lastUpdate: generateLastUpdate() |
||
169 | })); |
||
170 | |||
171 | case SAVE_ROW: |
||
172 | const gridData = state |
||
173 | .getIn([action.stateKey, 'data']) |
||
174 | .set(action.rowIndex, fromJS(action.values)); |
||
175 | |||
176 | return state.mergeIn([action.stateKey], fromJS({ |
||
177 | data: gridData, |
||
178 | proxy: gridData, |
||
179 | currentRecords: gridData, |
||
180 | lastUpdate: generateLastUpdate() |
||
181 | })); |
||
182 | |||
183 | case SORT_DATA: |
||
184 | return state.mergeIn([action.stateKey], { |
||
185 | data: action.data, |
||
186 | lastUpdate: generateLastUpdate() |
||
187 | }); |
||
188 | |||
189 | case CLEAR_FILTER_LOCAL: |
||
190 | |||
191 | const proxy = state.getIn([action.stateKey, 'proxy']); |
||
192 | const prevData = state.getIn([action.stateKey, 'data']); |
||
193 | const recs = proxy || prevData; |
||
194 | |||
195 | return state.mergeIn([action.stateKey], { |
||
196 | data: recs, |
||
197 | proxy: recs, |
||
198 | currentRecords: recs, |
||
199 | lastUpdate: generateLastUpdate() |
||
200 | }); |
||
201 | |||
202 | case FILTER_DATA: |
||
203 | return state.mergeIn([action.stateKey], { |
||
204 | data: action.data, |
||
205 | lastUpdate: generateLastUpdate() |
||
206 | }); |
||
207 | |||
208 | default: |
||
209 | |||
210 | return state; |
||
211 | } |
||
212 | } |
||
213 |