Passed
Push — feature/add-2fa-support ( 23e818...85b1f3 )
by Chris
24:19 queued 11:16
created

Reference.render   B

Complexity

Conditions 1

Size

Total Lines 60
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 53
dl 0
loc 60
rs 8.5381
c 0
b 0
f 0

How to fix   Long Method   

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:

1
import React, { Component } from "react";
2
import ReactDOM from "react-dom";
3
import classNames from "classnames";
4
5
export default class Reference extends Component {
6
  constructor(props) {
7
    super(props);
8
    this.state = {
9
      active: false,
10
      name: props.initName,
11
    };
12
    this.handleTriggerClick = this.handleTriggerClick.bind(this);
13
    this.handleNameChange = this.handleNameChange.bind(this);
14
  }
15
16
  handleTriggerClick() {
17
    console.log("click");
18
    this.setState(state => ({
19
      active: !state.active,
20
    }));
21
    // this.setState({active: !this.state.active});
22
  }
23
24
  handleNameChange(event) {
25
    this.setState({
26
      name: event.target.value,
27
    });
28
  }
29
30
  componentDidCatch(error, info) {
31
    console.log(error);
32
    console.log(info);
33
  }
34
35
  render() {
36
    const triggerClass = classNames(
37
      "profile-element",
38
      "accordion",
39
      "reference",
40
      "modal-target-object",
41
      { active: this.state.active },
42
    );
43
    return (
44
      <div className={triggerClass}>
45
        <button
46
          aria-expanded={this.state.active}
47
          className="accordion-trigger"
48
          tabIndex="0"
49
          type="button"
50
          onClick={this.handleTriggerClick}
51
        >
52
          {this.props.showStatus && (
53
            <div className="accordion-status">
54
              <i className="fas fa-check" />
55
              <i className="fas fa-exclamation-circle" />
56
            </div>
57
          )}
58
          <span className="accordion-title">{this.props.title}</span>
59
        </button>
60
61
        <div aria-hidden={!this.state.active} className="accordion-content">
62
          <form action={this.props.url} method="POST">
63
            <div className="form__wrapper">
64
              <div className="form-error box" />
65
              <div className="flex-grid">
66
                <div className="box med-1of2">
67
                  <div
68
                    className={classNames("form__input- wrapper--float", {
69
                      active: this.state.name,
70
                    })}
71
                  >
72
                    <label
73
                      className="form__label"
74
                      htmlFor={`referenceName${this.props.id}`}
75
                    >
76
                      {this.props.lang.name_label}
77
                    </label>
78
                    <input
79
                      className="form__input"
80
                      id={`referenceName${this.props.id}`}
81
                      type="text"
82
                      name="name"
83
                      required
84
                      value={this.state.name}
85
                      onChange={this.handleNameChange}
86
                    />
87
                  </div>
88
                </div>
89
              </div>
90
            </div>
91
          </form>
92
        </div>
93
      </div>
94
    );
95
  }
96
}
97
98
if (document.getElementById("react-reference")) {
99
  ReactDOM.render(
100
    <Reference key="1" id="1" url="/" initName="Joe Bob" locale="profile" />,
101
    document.getElementById("react-reference"),
102
  );
103
}
104