Completed
Pull Request — master (#981)
by wiese
110:54 queued 45:53
created

module.exports.formContent   C

Complexity

Conditions 10
Paths 55

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
c 1
b 0
f 0
nc 55
dl 0
loc 47
rs 5.1578
nop 2

How to fix   Complexity   

Complexity

Complex classes like module.exports.formContent 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
'use strict';
2
3
var objectAssign = require( 'object-assign' ),
4
  _ = require( 'underscore' );
5
6
/**
7
 * Return object keys that are not defined in initial state
8
 *
9
 * @param {Object} state
10
 * @param {Object} initialState
11
 * @returns {Array}
12
 */
13
function getInvalidKeys( state, initialState ) {
14
  return _.keys( _.omit( state, _.keys( initialState ) ) );
15
}
16
17
function clearFieldsIfAddressTypeChanges( newState, payload ) {
18
  if ( payload.contentName !== 'addressType'  ) {
19
    return;
20
  }
21
  switch ( payload.value ) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
22
    case 'person':
23
      newState.companyName = '';
24
      newState.contactPerson = '';
25
      break;
26
    case 'firma':
27
      newState.salutation = '';
28
      newState.title = '';
29
      break;
30
    case 'anonym':
31
      newState.salutation = '';
32
      newState.title = '';
33
      newState.companyName = '';
34
      newState.firstName = '';
35
      newState.lastName = '';
36
      newState.street = '';
37
      newState.postcode = '';
38
      newState.city = '';
39
      newState.email = '';
40
      break;
41
  }
42
}
43
44
function setPaymentType(newState, payload) {
45
  if (typeof payload.value !== 'string') {
46
    return;
47
  }
48
  if ( (payload.contentName === 'iban' || payload.contentName === 'bic') && trimValue(payload.value) ) {
49
    newState.debitType = "sepa";
50
  }
51
  else if ( (payload.contentName !== 'accountNumber' || payload.contentName === 'bankCode') && trimValue(payload.value)) {
52
    newState.debitType = "non-sepa";
53
  }
54
}
55
56
function forcePersonalDataForDirectDebit( state ) {
57
  if ( state.paymentType === 'BEZ' && state.addressType === 'anonym' ) {
58
    return objectAssign( {}, state, { addressType: 'person' } );
59
  } else {
60
    return state;
61
  }
62
}
63
64
function forceAddressTypeForActiveMembership( state ) {
65
  if ( state.membershipType === 'active' ) {
66
    return objectAssign( {}, state, { addressType: 'person' } );
67
  } else {
68
    return state;
69
  }
70
}
71
72
function trimValue( value ) {
73
  return value.replace( /^\s+|\s+$/gm, '' );
74
}
75
76
module.exports = {
77
  stateContainsUnknownKeys: function ( state, initialState ) {
78
    return !_.isEmpty( getInvalidKeys( state, initialState ) );
79
  },
80
  getInvalidKeys: getInvalidKeys,
81
  formContent: function ( state, action ) {
82
    var newAmount, newState;
83
    switch ( action.type ) {
84
      case 'SELECT_AMOUNT':
85
        newAmount = action.payload.amount === null ? state.amount : action.payload.amount;
86
        return objectAssign( {}, state, {
87
          amount: newAmount,
88
          isCustomAmount: false
89
        } );
90
      case 'INPUT_AMOUNT':
91
        return objectAssign( {}, state, {
92
          amount: action.payload.amount,
93
          isCustomAmount: true
94
        } );
95
      case 'CHANGE_CONTENT':
96
        if ( !_.has( state, action.payload.contentName ) ) {
97
          throw new Error( 'Unsupported form content name: ' + action.payload.contentName );
98
        }
99
        newState = _.clone( state );
100
        clearFieldsIfAddressTypeChanges( newState, action.payload );
101
102
        if ( _.isString( action.payload.value ) ) {
103
          newState[ action.payload.contentName ] = trimValue( action.payload.value );
104
        } else {
105
          newState[ action.payload.contentName ] = action.payload.value;
106
        }
107
108
        setPaymentType(newState, action.payload);
109
110
        newState = forcePersonalDataForDirectDebit( newState );
111
        newState = forceAddressTypeForActiveMembership( newState );
112
        return newState;
113
      case 'FINISH_BANK_DATA_VALIDATION':
114
        if ( action.payload.status !== 'OK' ) {
115
          return state;
116
        }
117
        return objectAssign( {}, state, {
118
          iban: action.payload.iban || '',
119
          bic: action.payload.bic || state.bic || '',
120
          accountNumber: action.payload.account || '',
121
          bankCode: action.payload.bankCode || '',
122
          bankName: action.payload.bankName || ''
123
        } );
124
      default:
125
        return state;
126
    }
127
  }
128
};
129