Completed
Push — master ( 3927ec...729a6c )
by Stefan
02:59
created

src/ReportEdit.js   A

Complexity

Total Complexity 4
Complexity/F 1

Size

Lines of Code 83
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 64
mnd 0
bc 0
fnc 4
dl 0
loc 83
rs 10
bpm 0
cpm 1
noi 0
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A ReportEdit.componentDidMount 0 8 1
A ReportEdit.mySubmitHandler 0 24 1
A ReportEdit.render 0 21 1
A ReportEdit.myChangeHandler 0 2 1
1
import React, {Component} from 'react';
2
import {Link} from "react-router-dom";
3
import auth from "./auth";
4
5
class ReportEdit extends Component {
6
7
    constructor() {
8
        super();
9
        this.state = {
10
            text: '',
11
            week: null,
12
            msg: ""
13
        };
14
    }
15
16
    componentDidMount() {
17
        // console.log(this.props);
18
        fetch("https://me-api.listrom.me/reports/week/" + this.props.match.params.number)
19
            .then(response => response.json())
20
            .then(data => {
21
                this.setState({ text: data.data.text});
22
                this.setState({ week: data.data.week});
23
                // console.log(this.state.data);
24
            });
25
    };
26
27
    mySubmitHandler = (event) => {
28
        event.preventDefault();
29
        const API = 'https://me-api.listrom.me/reports/';
30
        let payload={
31
            "text":this.state.text,
32
            "week":this.state.week
33
        }
34
        fetch(API, {
35
            method: 'POST',
36
            headers: {
37
                "x-access-token": auth.token,
38
                'Content-Type': 'application/json',
39
            },
40
            body: JSON.stringify(payload),
41
        })
42
            .then(response => response.json())
43
            .then(data => {
44
                this.setState({msg: "<span class='infomsg'>Report updated</span>"});
45
                console.log('Success:', data);
46
                // this.props.history.push('/login');
47
48
            })
49
            .catch((error) => {
50
                console.error('Error:', error);
51
            });
52
    }
53
54
    myChangeHandler = (event) => {
55
        this.setState({text: event.target.value});
56
    }
57
58
    render() {
59
        // console.log(this.props);
60
61
        return (
62
            <main>
63
                {/*<p>{this.state.msg}</p>*/}
64
                <p dangerouslySetInnerHTML={{__html: this.state.msg}}></p>
65
                <h3>Kmom {this.state.week}</h3>
66
                <h3>Update report text</h3>
67
                    <form onSubmit={this.mySubmitHandler}>
68
                        <textarea value={this.state.text} name='text' rows='30' cols='90' onChange={this.myChangeHandler} />
69
                        <p></p>
70
                        <input
71
                            className='button'
72
                            type='submit'
73
                            value='Save'
74
                        />
75
                    </form>
76
                <p><Link className='button' to={`/reports/week/${this.props.match.params.number}`}>Cancel</Link></p>
77
            </main>
78
        );
79
    }
80
}
81
82
export default ReportEdit;
83