src/components/page/admin/Create.js   A
last analyzed

Complexity

Total Complexity 7
Complexity/F 1.75

Size

Lines of Code 101
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 82
mnd 3
bc 3
fnc 4
dl 0
loc 101
rs 10
bpm 0.75
cpm 1.75
noi 0
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A Create.componentDidMount 0 2 1
A Create.render 0 23 1
A Create.getOptions 0 24 4
A Create.registerSubmit 0 20 1
1
/*eslint max-len: ["error", { "code": 160 }]*/
2
3
import React, { Component } from 'react';
4
import PropTypes from "prop-types";
5
import base from '../../../config/api.js';
6
let api = base.api();
7
8
class Create extends Component {
9
    static propTypes = {
10
        match: PropTypes.object.isRequired,
11
        location: PropTypes.object.isRequired,
12
        history: PropTypes.object.isRequired
13
    };
14
    constructor(props) {
15
        super(props);
16
        this.registerSubmit = this.registerSubmit.bind(this);
17
        this.getOptions = this.getOptions.bind(this);
18
        this.state = {
19
            options: ""
20
        };
21
    }
22
    componentDidMount() {
23
        this.getOptions();
24
    }
25
    registerSubmit(event) {
26
        event.preventDefault();
27
        const data = new FormData(event.target);
28
29
        let token = localStorage.getItem("token");
30
31
        token = JSON.parse(token);
32
        let report = {
33
            "title": data.get('title'),
34
            "content": data.get('content')
35
        };
36
37
        fetch(api + "/reports", {
38
            method: 'POST',
39
            body: JSON.stringify(report),
40
            headers: {
41
                'Content-Type': 'application/json',
42
                'x-access-token': token
43
            }
44
        }).then(this.props.history.push('/reports/week/1'));
45
    }
46
    getOptions() {
47
        const that = this;
48
49
        let reports = [1, 2, 3, 4, 5, 6, 10],
50
            options = [],
51
            count = 0;
52
53
        reports.forEach(function (report) {
54
            fetch(api + `/reports/week/${report}`)
55
                .then(res => res.json())
56
                .then(function(res) {
57
                    if (res.data.report.title === "Report comming soon") {
58
                        if (report >= 10) {
59
                            options[report] = <option>Kmom{report}</option>;
60
                        } else {
61
                            options[report] = <option>Kmom0{report}</option>;
62
                        }
63
                    }
64
                    if (count < reports.length) {
65
                        that.setState({
66
                            options: options
67
                        });
68
                    }
69
                    count ++;
70
                });
71
        });
72
    }
73
    render() {
74
        return (
75
            <div className="form-wrapper">
76
                <h1>Create Report</h1>
77
                <form action="/reports/week/1" method="post" className="form-register" onSubmit={this.registerSubmit}>
78
                    <label className="form-label">Title
79
                        <select className="form-input" name="title" required>
80
                            { this.state.options }
81
                        </select>
82
                    </label>
83
84
                    <label className="form-label">Content
85
                        <textarea className="form-input textarea" type="text" name="content" required placeholder="Write your report here!"></textarea>
86
                    </label>
87
88
                    <label className="form-label check-label">
89
                        <input className="check-input" type="checkbox" name="finished" required />
90
                        Are you finished?
91
                    </label><br />
92
93
                    <input className="button form-button center" type="submit" name="create" value="Create" />
94
                </form>
95
            </div>
96
        );
97
    }
98
}
99
100
export default Create;
101