src/component/modal/DeviceTypeModal.jsx   A
last analyzed

Complexity

Total Complexity 10
Complexity/F 1.43

Size

Lines of Code 114
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 90
dl 0
loc 114
rs 10
c 0
b 0
f 0
wmc 10
mnd 3
bc 3
fnc 7
bpm 0.4285
cpm 1.4285
noi 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
A DeviceTypeModal.renderChild 0 33 1
A DeviceTypeModal.validateField 0 16 4
A DeviceTypeModal.resolveData 0 3 1
A DeviceTypeModal.getData 0 5 1
A DeviceTypeModal.doAdd 0 3 1
A DeviceTypeModal.doModify 0 3 1
A DeviceTypeModal.doValidate 0 3 1
1
import React from 'react';
2
import PropTypes from 'prop-types';
3
import { FormControl } from 'react-bootstrap';
4
import BaseDataEditableModal from './BaseDataEditableModal';
5
import Api from '../../common/api';
6
import ValidationField from '../form/ValidationField';
7
import ValidationForm, { ValidationError } from '../form/ValidationForm';
8
9
export default class DeviceTypeModal extends BaseDataEditableModal {
10
  constructor(props) {
11
    super(
12
      props,
13
      { label: '', value: '' },
14
      {
15
        add: { title: '새로운 디바이스 타입 등록' },
16
        modify: { title: '디바이스 타입 수정' },
17
      },
18
    );
19
  }
20
21
  getData() {
22
    return {
23
      label: this.state.data.label,
24
      value: this.state.data.value,
25
    };
26
  }
27
28
  resolveData(data) {
29
    return Object.assign({}, data);
30
  }
31
32
  doAdd(data) {
33
    return Api.addDeviceType(data);
34
  }
35
36
  doModify(id, data) {
37
    return Api.updateDeviceType(id, data);
38
  }
39
40
  doValidate() {
41
    return this.form.validate();
42
  }
43
44
  validateField(id) {
45
    const data = this.state.data;
46
    switch (id) {
47
      case 'label':
48
        if (!data.label) {
49
          throw new ValidationError('디바이스 타입의 라벨을 설정해 주세요.');
50
        }
51
        break;
52
      case 'value':
53
        if (!data.value) {
54
          throw new ValidationError('디바이스 타입의 값을 설정해 주세요.');
55
        }
56
        break;
57
      default:
58
        break;
59
    }
60
  }
61
62
  renderChild() {
63
    return (
64
      <ValidationForm ref={(form) => { this.form = form; }}>
65
        <ValidationField
66
          controlId="label"
67
          required
68
          label="디바이스 타입 라벨"
69
          validate={() => this.validateField('label')}
70
        >
71
          <FormControl
72
            componentClass="input"
73
            value={this.state.data.label}
74
            onChange={e => this.setDataField({ label: e.target.value })}
75
            placeholder="라벨"
76
          />
77
        </ValidationField>
78
79
        <ValidationField
80
          controlId="value"
81
          label="디바이스 타입 값"
82
          required
83
          validate={() => this.validateField('value')}
84
        >
85
          <FormControl
86
            componentClass="input"
87
            value={this.state.data.value}
88
            onChange={e => this.setDataField({ value: e.target.value })}
89
            placeholder="값"
90
            readOnly={this.props.mode === 'modify'}
91
          />
92
        </ValidationField>
93
      </ValidationForm>
94
    );
95
  }
96
}
97
98
DeviceTypeModal.defaultProps = {
99
  onSuccess: () => {},
100
};
101
102
DeviceTypeModal.propTypes = {
103
  visible: PropTypes.bool.isRequired,
104
  onSuccess: PropTypes.func,
105
  onClose: PropTypes.func.isRequired,
106
  mode: PropTypes.oneOf(['add', 'modify']).isRequired,
107
  data: PropTypes.shape({
108
    id: PropTypes.string,
109
    label: PropTypes.string,
110
    value: PropTypes.string,
111
    template: PropTypes.string,
112
  }).isRequired,
113
};
114