| Conditions | 11 |
| Paths | 13 |
| Total Lines | 107 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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 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'; |
||
| 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 |