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 ( c335e5...c6cb10 )
by Benjamin
01:46
created

editor.js ➔ editor   C

Complexity

Conditions 11
Paths 13

Size

Total Lines 107

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 11
c 2
b 0
f 1
nc 13
dl 0
loc 107
rs 5.2653
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 } 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
} from '../../../constants/ActionTypes';
11
12
import {
13
    getData,
14
    setDataAtDataIndex,
15
    nameFromDataIndex
0 ignored issues
show
Unused Code introduced by
The variable nameFromDataIndex seems to be never used. Consider removing it.
Loading history...
16
} from './../../../util/getData';
17
18
import { generateLastUpdate } from './../../../util/lastUpdate';
19
20
const initialState = fromJS({
0 ignored issues
show
Unused Code introduced by
The constant initialState seems to be never used. Consider removing it.
Loading history...
21
    lastUpdate: generateLastUpdate()
22
});
23
24
export const isCellValid = ({ validator }, value, values) => {
25
    if (!validator || !typeof validator === 'function') {
26
        return true;
27
    }
28
29
    return validator({ value, values });
30
};
31
32
export const isRowValid = (columns, rowValues) => {
33
    for (let i = 0; i < columns.length; i++) {
34
35
        const col = columns[i];
36
        const val = isCellValid(col, getData(rowValues, columns, i), rowValues);
37
38
        if (!val) {
39
            return false;
40
        }
41
    }
42
43
    return true;
44
};
45
46
export const setDisabled = (col = {}, value, values) => {
47
48
    if (col.disabled === true || col.disabled === 'false') {
49
        return col.disabled;
50
    }
51
52
    if (typeof col.disabled === 'function') {
53
        return col.disabled({ column: col, value, values });
54
    }
55
56
    return false;
57
58
};
59
60
export const handleChangeFunc = (col, rowValues) => {
61
62
    if (!col.change || !typeof col.change === 'function') {
63
        return rowValues;
64
    }
65
66
    const overrideValue = col.change({ values: rowValues }) || {};
67
68
    Object.keys(overrideValue).forEach(k => {
69
        rowValues[k] = overrideValue[k];
70
    });
71
72
    return rowValues;
73
};
74
75
export default function editor(state = initialState, action) {
76
77
    switch (action.type) {
78
79
    case EDIT_ROW:
80
81
        const { values } = action;
82
        const isValid = isRowValid(action.columns, values);
83
        const iOverrides = state.getIn([stateKey, 'row', 'overrides'])
84
            ? state.getIn([stateKey, 'row', 'overrides']).toJS()
85
            : {};
86
87
        action.columns.forEach((col, i) => {
88
            const val = getData(values, action.columns, i);
89
            const dataIndex = col.dataIndex;
90
91
            // setting disabled
92
            iOverrides[dataIndex] = iOverrides[dataIndex] || {};
93
            iOverrides[dataIndex].disabled = setDisabled(col, val, values);
94
        });
95
96
        return state.setIn([action.stateKey], fromJS({
97
            row: {
98
                key: action.rowId,
99
                values: action.values,
100
                rowIndex: action.rowIndex,
101
                top: action.top,
102
                valid: isValid,
103
                isCreate: action.isCreate || false,
104
                overrides: iOverrides
105
            },
106
            lastUpdate: generateLastUpdate()
107
        }));
108
109
    case ROW_VALUE_CHANGE:
110
        const { column, columns, value, stateKey } = action;
111
        const previousValues = state.getIn([stateKey, 'row', 'values'])
112
            ? state.getIn([stateKey, 'row', 'values']).toJS()
113
            : {};
114
        const overrides = state.getIn([stateKey, 'row', 'overrides'])
115
            ? state.getIn([stateKey, 'row', 'overrides']).toJS()
116
            : {};
117
118
        let rowValues = setDataAtDataIndex(
119
            previousValues, column.dataIndex, value
120
        );
121
122
        columns.forEach((col, i) => {
123
            const val = getData(rowValues, columns, i);
124
            const dataIndex = col.dataIndex;
125
126
            // interpreting `change func` to set final values
127
            // happens first, due to other validation
128
            rowValues = handleChangeFunc(col, rowValues);
129
130
            // setting default value
131
            if (col.defaultValue !== undefined
132
                && val === undefined || val === null) {
133
                setDataAtDataIndex(rowValues, dataIndex, col.defaultValue);
134
            }
135
136
            // setting disabled
137
            overrides[dataIndex] = overrides[dataIndex] || {};
138
            overrides[dataIndex].disabled = setDisabled(col, val, rowValues);
139
140
        });
141
142
        const valid = isRowValid(columns, rowValues);
143
144
        state = state.mergeIn([action.stateKey, 'row'], {
145
            values: rowValues,
146
            previousValues: state.getIn([stateKey, 'row', 'values']),
147
            valid,
148
            overrides
149
        });
150
151
        return state.setIn(
152
            [action.stateKey, 'lastUpdate'],
153
            generateLastUpdate()
154
        );
155
156
    case REPOSITION_EDITOR:
157
158
        const row = state.mergeIn([action.stateKey, 'row'], {
159
            top: action.top
160
        }).getIn([action.stateKey, 'row']);
161
162
        return state.mergeIn(
163
            [action.stateKey],
164
            fromJS({
165
                row: row,
166
                lastUpdate: generateLastUpdate()
167
            })
168
        );
169
170
    case REMOVE_ROW:
171
    case DISMISS_EDITOR:
172
    case CANCEL_ROW:
173
        return state.setIn(
174
            [action.stateKey],
175
            fromJS({ lastUpdate: generateLastUpdate() })
176
        );
177
178
    default:
179
        return state;
180
    }
181
}
182