Passed
Push — dev ( 5c8013...a7a625 )
by Chris
13:04 queued 11s
created

resources/assets/js/components/ReferenceList.jsx   A

Complexity

Total Complexity 8
Complexity/F 1.6

Size

Lines of Code 108
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 87
dl 0
loc 108
rs 10
c 0
b 0
f 0
wmc 8
mnd 3
bc 3
fnc 5
bpm 0.6
cpm 1.6
noi 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
B ReferenceList.render 0 46 3
A ReferenceList.createEmptyReference 0 8 1
A ReferenceList.nextId 0 5 1
A ReferenceList.componentDidCatch 0 4 1
A ReferenceList.handleAddItemClick 0 6 1
1
import React, { Component } from "react";
2
import ReactDOM from "react-dom";
3
import classNames from "classnames";
4
5
export default class ReferenceList extends Component {
6
  constructor(props) {
7
    super(props);
8
    this.state = {
9
      references: props.initialRefs
10
    };
11
    this.handleAddItemClick = this.handleAddItemClick.bind(this);
12
  }
13
14
  componentDidCatch(error, info) {
15
    console.log(error);
16
    console.log(info);
17
  }
18
19
  nextId(objs) {
20
    const ids = objs.map(x => x.id);
21
    const maxReducer = (a, b) => Math.max(a, b);
22
    return ids.reduce(maxReducer, 0) + 1;
23
  }
24
25
  createEmptyReference(id) {
26
    return {
27
      id,
28
      name: "",
29
      email: "",
30
      relationship_id: null,
31
      description: ""
32
    };
33
  }
34
35
  handleAddItemClick() {
36
    const refs = this.state.references.slice();
37
    const nextId = this.nextId(this.state.references);
38
    refs.push(this.createEmptyReference(nextId));
39
    this.setState({ references: refs });
40
  }
41
42
  render() {
43
    if (this.state.references.length > 0) {
44
      var content = this.state.references.map(reference => (
45
        <Reference
46
          key={reference.id}
47
          id={reference.id}
48
          url="/"
49
          title={
50
            reference.name
51
              ? reference.name
52
              : this.props.langReference.new_reference_title
53
          }
54
          lang={this.props.langReference}
55
          initName={reference.name}
56
          showStatus={false}
57
        />
58
      ));
59
    } else {
60
      var content = this.state.references.map(reference => (
61
        <div className="profile-null active">
62
          <p>{this.props.lang.reference_section.null_copy}</p>
63
        </div>
64
      ));
65
    }
66
    return (
67
      <div className="profile-list">
68
        <div className="profile-list__header flex-grid middle">
69
          <div className="box med-1of2">
70
            <h3>{this.props.lang.reference_section.section_title}</h3>
71
          </div>
72
          <div className="box med-1of2">
73
            <button
74
              className="profile-list__add-element-trigger"
75
              type="button"
76
              onClick={this.handleAddItemClick}
77
            >
78
              {this.props.lang.reference_section.add_button_label}
79
            </button>
80
          </div>
81
          <div className="box full">
82
            <p>{this.props.lang.reference_section.section_description}</p>
83
          </div>
84
        </div>
85
        <div className="profile-element-list">{content}</div>
86
      </div>
87
    );
88
  }
89
}
90
91
if (document.getElementById("react-reference-list")) {
92
  const container = document.getElementById("react-reference-list");
93
  // const props = Object.assign({}, domContainer.dataset);
94
  const lang = JSON.parse(container.getAttribute("data-lang"));
95
  const langReference = JSON.parse(
96
    container.getAttribute("data-reference-lang")
97
  );
98
  const initialRefs = JSON.parse(container.getAttribute("data-references"));
99
  ReactDOM.render(
100
    <ReferenceList
101
      lang={lang}
102
      langReference={langReference}
103
      initialRefs={initialRefs}
104
    />,
105
    container
106
  );
107
}
108