]> git.somenet.org - pub/jan/aic18.git/blob - service-website/src/components/SentimentAnalysis.js
Merge branch '68-website-and-failover-problem-in-master-branch' into 'master'
[pub/jan/aic18.git] / service-website / src / components / SentimentAnalysis.js
1 import React, {Component} from "react";
2 import {Map} from "immutable";
3 import PdfReport from "./PdfReport";
4 import SearchTermsInput from "./SearchTermsInput";
5 import CamundaService from "../services/CamundaService";
6 import {Button, Heading, Form} from "react-bulma-components/full";
7
8 class SentimentAnalysis extends Component {
9     constructor(props, context) {
10         super(props, context);
11         this.state = {
12             terms: new Map().set(0, ''),
13             nextTermId: 1,
14             pdfLink: null,
15         };
16         this.handleSubmit = this.handleSubmit.bind(this);
17         this.handleTermChange = this.handleTermChange.bind(this);
18         this.handleAddTerm = this.handleAddTerm.bind(this);
19         this.handleRemoveTerm = this.handleRemoveTerm.bind(this);
20         this.addEmptyTerm = this.addEmptyTerm.bind(this);
21         this.emptyTerms = this.emptyTerms.bind(this);
22         this.getNonEmptyTerms = this.getNonEmptyTerms.bind(this);
23         this.createPdf = this.createPdf.bind(this);
24         this.setPdfLink = this.setPdfLink.bind(this);
25         this.camundaService = new CamundaService(this.setPdfLink);
26     }
27
28     handleSubmit(event) {
29         event.preventDefault();
30         const target = event.target;
31         console.log(target);
32         const terms = this.state.terms;
33         terms.forEach((term) => {
34             console.log(term);
35         });
36         this.createPdf();
37         this.emptyTerms();
38     }
39
40     handleTermChange(id, value) {
41         const terms = this.state.terms;
42         this.setState({
43             terms: terms.set(id, value),
44         });
45     }
46
47     handleAddTerm(event) {
48         this.addEmptyTerm();
49     }
50
51     handleRemoveTerm(id) {
52         const terms = this.state.terms;
53         this.setState({
54             terms: terms.delete(id),
55         });
56     }
57
58     addEmptyTerm() {
59         const terms = this.state.terms;
60         const termId = this.state.nextTermId;
61         this.setState({
62             terms: terms.set(termId, ''),
63             nextTermId: termId + 1,
64         });
65     }
66
67     emptyTerms() {
68         this.setState({
69             terms: new Map().set(0, ''),
70             nextTermId: 1,
71         });
72     }
73
74     getNonEmptyTerms() {
75         const terms = this.state.terms;
76         return terms.filter((value, key) => value.trim() !== '');
77     }
78
79     createPdf() {
80         const terms = this.getNonEmptyTerms();
81         if (terms.size > 0) {
82             this.camundaService.submitPdfForm(terms);
83         }
84     }
85
86     setPdfLink(url) {
87         this.setState({
88             pdfLink: url,
89         });
90     }
91
92     render() {
93         return (
94             <div className="sentiment-analysis">
95                 <form onSubmit={this.handleSubmit}>
96                     <Heading size={3} Style="margin-top:1em">Search Terms: </Heading>
97                     <SearchTermsInput terms={this.state.terms}
98                                       onTermChange={this.handleTermChange}
99                                       onAddTerm={this.handleAddTerm}
100                                       onRemoveTerm={this.handleRemoveTerm}/>
101                     <Form.Field kind="group">
102                         <p className="control">
103                             <Button color="info" type="submit" value="Submit">Submit</Button>
104                         </p>
105                         <p className="control">
106                             <Button value="Reset" onClick={this.emptyTerms}>Reset</Button>
107                         </p>
108                     </Form.Field>
109                 </form>
110                 <PdfReport pdfLink={this.state.pdfLink}/>
111             </div>
112         );
113     }
114 }
115
116 SentimentAnalysis.propTypes = {};
117
118 export default SentimentAnalysis;