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
Push — master ( ee15c2...bb31bb )
by Benjamin
01:50
created

editor.js ➔ editor   D

Complexity

Conditions 14
Paths 15

Size

Total Lines 132

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 14
c 2
b 0
f 1
nc 15
dl 0
loc 132
rs 4.9516
nop 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A editor.js ➔ ... ➔ ??? 0 8 1

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

Complex classes like editor.js ➔ editor 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, Map } from 'immutable';
2
3
import {
4
    EDIT_ROW,
5
    DISMISS_EDITOR,
6
    ROW_VALUE_CHANGE,
7
    CANCEL_ROW,
8
    REMOVE_ROW,
9
    REPOSITION_EDITOR,
10
    SET_DATA
11
} from '../../../constants/ActionTypes';
12
13
import {
14
    getData,
15
    setDataAtDataIndex,
16
    setKeysInData
17
} from './../../../util/getData';
18
19
import { generateLastUpdate } from './../../../util/lastUpdate';
20
21
const initialState = fromJS({
0 ignored issues
show
Unused Code introduced by
The constant initialState seems to be never used. Consider removing it.
Loading history...
22
    lastUpdate: generateLastUpdate()
23
});
24
25
export const isCellValid = ({ validator }, value, values) => {
26
    if (!validator || !typeof validator === 'function') {
27
        return true;
28
    }
29
30
    return validator({ value, values });
31
};
32
33
export const isRowValid = (columns, rowValues) => {
34
    for (let i = 0; i < columns.length; i++) {
35
36
        const col = columns[i];
37
        const val = isCellValid(col, getData(rowValues, columns, i), rowValues);
38
39
        if (!val) {
40
            return false;
41
        }
42
    }
43
44
    return true;
45
};
46
47
export const setDisabled = (col = {}, value, values) => {
48
49
    if (col.disabled === true || col.disabled === 'false') {
50
        return col.disabled;
51
    }
52
53
    if (typeof col.disabled === 'function') {
54
        return col.disabled({ column: col, value, values });
55
    }
56
57
    return false;
58
59
};
60
61
export const handleChangeFunc = (col, rowValues) => {
62
63
    if (!col.change || !typeof col.change === 'function') {
64
        return rowValues;
65
    }
66
67
    const overrideValue = col.change({ values: rowValues }) || {};
68
69
    Object.keys(overrideValue).forEach(k => {
70
        rowValues[k] = overrideValue[k];
71
    });
72
73
    return rowValues;
74
};
75
76
export default function editor(state = initialState, action) {
77
78
    switch (action.type) {
79
80
    case EDIT_ROW:
81
82
        const { values } = action;
83
        const isValid = isRowValid(action.columns, values);
84
        const iOverrides = state.getIn([stateKey, action.rowId, 'overrides'])
85
            ? state.getIn([stateKey, action.rowId, 'overrides']).toJS()
86
            : {};
87
88
        action.columns.forEach((col, i) => {
89
            const val = getData(values, action.columns, i);
90
            const dataIndex = col.dataIndex;
91
92
            // setting disabled
93
            iOverrides[dataIndex] = iOverrides[dataIndex] || {};
94
            iOverrides[dataIndex].disabled = setDisabled(col, val, values);
95
        });
96
97
        const operation = action.editMode === 'inline'
98
            ? 'setIn'
99
            : 'mergeIn';
100
101
        return state[operation]([action.stateKey], fromJS({
102
            [action.rowId]: {
103
                key: action.rowId,
104
                values: action.values,
105
                rowIndex: action.rowIndex,
106
                top: action.top,
107
                valid: isValid,
108
                isCreate: action.isCreate || false,
109
                overrides: iOverrides
110
            },
111
            lastUpdate: generateLastUpdate()
112
        }));
113
114
    case SET_DATA:
115
116
        // if grid editor is type 'grid', we need to map the datasource
117
        // to editor state
118
        if (action.editMode === 'grid') {
119
120
            const dataWithKeys = setKeysInData(action.data);
121
            const editorData = dataWithKeys.reduce((prev, curr, i) => {
122
                return prev.set(curr.get('_key'), fromJS({
123
                    key: curr.get('_key'),
124
                    values: curr,
125
                    rowIndex: i,
126
                    top: null,
127
                    valid: null,
128
                    isCreate: false,
129
                    overrides: {}
130
                }));
131
            }, Map({ lastUpdate: generateLastUpdate() }));
132
133
            return state.mergeIn([action.stateKey], editorData);
134
        }
135
136
        return state;
137
138
    case ROW_VALUE_CHANGE:
139
140
        const { column, columns, value, stateKey } = action;
141
        const previousValues = state.getIn([stateKey, action.rowId, 'values'])
142
            ? state.getIn([stateKey, action.rowId, 'values']).toJS()
143
            : {};
144
        const overrides = state.getIn([stateKey, action.rowId, 'overrides'])
145
            ? state.getIn([stateKey, action.rowId, 'overrides']).toJS()
146
            : {};
147
148
        let rowValues = setDataAtDataIndex(
149
            previousValues, column.dataIndex, value
150
        );
151
152
        columns.forEach((col, i) => {
153
            const val = getData(rowValues, columns, i);
154
            const dataIndex = col.dataIndex;
155
156
            // interpreting `change func` to set final values
157
            // happens first, due to other validation
158
            rowValues = handleChangeFunc(col, rowValues);
159
160
            // setting default value
161
            if (col.defaultValue !== undefined
162
                && val === undefined || val === null) {
163
                setDataAtDataIndex(rowValues, dataIndex, col.defaultValue);
164
            }
165
166
            // setting disabled
167
            overrides[dataIndex] = overrides[dataIndex] || {};
168
            overrides[dataIndex].disabled = setDisabled(col, val, rowValues);
169
170
        });
171
172
        const valid = isRowValid(columns, rowValues);
173
174
        state = state.mergeIn([action.stateKey, action.rowId], {
175
            values: rowValues,
176
            previousValues: state.getIn([stateKey, action.rowId, 'values']),
177
            valid,
178
            overrides
179
        });
180
181
        return state.setIn(
182
            [action.stateKey, 'lastUpdate'],
183
            generateLastUpdate()
184
        );
185
186
    case REPOSITION_EDITOR:
187
        const newState = state.mergeIn([action.stateKey, action.rowId], {
188
            top: action.top
189
        });
190
191
        return newState.mergeIn(
192
            [action.stateKey],
193
            { lastUpdate: generateLastUpdate() }
194
        );
195
196
    case REMOVE_ROW:
197
    case DISMISS_EDITOR:
198
    case CANCEL_ROW:
199
        return state.setIn(
200
            [action.stateKey],
201
            fromJS({ lastUpdate: generateLastUpdate() })
202
        );
203
204
    default:
205
        return state;
206
    }
207
}
208