1
|
|
|
import React from 'react'; |
2
|
|
|
import {HeaderReports} from './Header.js'; |
3
|
|
|
import Link from "./Link"; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class Report extends React.Component { |
7
|
|
|
constructor(props){ |
8
|
|
|
super(props); |
9
|
|
|
this.state = { |
10
|
|
|
report: [], |
11
|
|
|
kmom: this.props.match.params.kmom, |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
} |
15
|
|
|
getReport = () => { |
16
|
|
|
var kmom = this.props.match.params.kmom |
17
|
|
|
fetch("http://localhost:8333/reports/" + kmom, { |
18
|
|
|
method: "GET", |
19
|
|
|
headers : { |
20
|
|
|
'Content-Type': 'application/json', |
21
|
|
|
'Accept': 'application/json' |
22
|
|
|
}, |
23
|
|
|
}) |
24
|
|
|
.then((res) => { |
25
|
|
|
console.log("Success I guess"); |
26
|
|
|
return res.json(); |
27
|
|
|
}) |
28
|
|
|
.then((res) => { |
29
|
|
|
console.log(res); |
30
|
|
|
if(res.data){ |
31
|
|
|
var jsonData = JSON.parse(res.data.text); |
32
|
|
|
this.setState({report: jsonData}); |
33
|
|
|
} |
34
|
|
|
else{ |
35
|
|
|
var infoText = JSON.parse('[{"question":"No report found, press \\"Create report\\" "}]'); |
36
|
|
|
this.setState({report: infoText}); |
37
|
|
|
|
38
|
|
|
} |
39
|
|
|
}) |
40
|
|
|
}; |
41
|
|
|
componentDidMount(){ |
42
|
|
|
this.getReport(); |
43
|
|
|
} |
44
|
|
|
componentDidUpdate(prevProps) { |
45
|
|
|
|
46
|
|
|
if(prevProps.match.params.kmom !== this.props.match.params.kmom){ |
47
|
|
|
this.getReport(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
} |
51
|
|
|
render() { |
52
|
|
|
|
53
|
|
|
const kmom = this.props.match.params.kmom; |
54
|
|
|
const editUrl = "/reports/edit/"+kmom; |
55
|
|
|
var questions = this.state.report; |
56
|
|
|
|
57
|
|
|
const QuestionsList = () => |
58
|
|
|
questions.map((question, index) => ( |
59
|
|
|
<div className="question" key={index}> |
60
|
|
|
<p> |
61
|
|
|
<strong>{question.question}</strong> |
62
|
|
|
</p> |
63
|
|
|
<p>{question.answer}</p> |
64
|
|
|
</div> |
65
|
|
|
)); |
66
|
|
|
|
67
|
|
|
return ( |
68
|
|
|
<article className="article"> |
69
|
|
|
<div className="article_header"> |
70
|
|
|
<h2>Reports</h2> |
71
|
|
|
<HeaderReports/> |
72
|
|
|
<Link url={editUrl} text="Create report" className="report_week_link"/> |
73
|
|
|
</div> |
74
|
|
|
|
75
|
|
|
<div className="article_main_content"> |
76
|
|
|
<QuestionsList/> |
77
|
|
|
</div> |
78
|
|
|
|
79
|
|
|
</article> |
80
|
|
|
) |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
export default Report; |