GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#133)
by
unknown
05:26
created

src/reducers/actionHelpers/datasource.js   B

Complexity

Total Complexity 36
Complexity/F 2.57

Size

Lines of Code 312
Function Count 14

Duplication

Duplicated Lines 64
Ratio 20.51 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 0
wmc 36
c 3
b 1
f 1
nc 1
mnd 1
bc 22
fnc 14
dl 64
loc 312
rs 8.8
bpm 1.5713
cpm 2.5714
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A datasource.js ➔ ??? 4 23 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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