]> git.somenet.org - pub/jan/aic18.git/blob - service-website/src/components/SentimentAnalysis.js
Add run configuration for 'react-scripts start'
[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, Columns} 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             isLoading: false,
16         };
17         this.handleSubmit = this.handleSubmit.bind(this);
18         this.handleTermChange = this.handleTermChange.bind(this);
19         this.handleAddTerm = this.handleAddTerm.bind(this);
20         this.handleRemoveTerm = this.handleRemoveTerm.bind(this);
21         this.addEmptyTerm = this.addEmptyTerm.bind(this);
22         this.emptyTerms = this.emptyTerms.bind(this);
23         this.getNonEmptyTerms = this.getNonEmptyTerms.bind(this);
24         this.createPdf = this.createPdf.bind(this);
25         this.setPdfLink = this.setPdfLink.bind(this);
26         this.camundaService = new CamundaService(this.setPdfLink);
27     }
28
29     handleSubmit(event) {
30         event.preventDefault();
31         const target = event.target;
32         console.log(target);
33         const terms = this.state.terms;
34         terms.forEach((term) => {
35             console.log(term);
36         });
37         this.setState({
38             isLoading: true,
39         });
40         this.createPdf();
41         this.emptyTerms();
42         
43     }
44
45     handleTermChange(id, value) {
46         const terms = this.state.terms;
47         this.setState({
48             terms: terms.set(id, value),
49         });
50     }
51
52     handleAddTerm(event) {
53         this.addEmptyTerm();
54     }
55
56     handleRemoveTerm(id) {
57         const terms = this.state.terms;
58         this.setState({
59             terms: terms.delete(id),
60         });
61     }
62
63     addEmptyTerm() {
64         const terms = this.state.terms;
65         const termId = this.state.nextTermId;
66         this.setState({
67             terms: terms.set(termId, ''),
68             nextTermId: termId + 1,
69         });
70     }
71
72     emptyTerms() {
73         this.setState({
74             terms: new Map().set(0, ''),
75             nextTermId: 1,
76         });
77     }
78
79     getNonEmptyTerms() {
80         const terms = this.state.terms;
81         return terms.filter((value, key) => value.trim() !== '');
82     }
83
84     createPdf() {
85         const terms = this.getNonEmptyTerms();
86         if (terms.size > 0) {
87             this.camundaService.submitPdfForm(terms);
88         }
89     }
90
91     setPdfLink(url) {
92         this.setState({
93             pdfLink: url,
94         });
95     }
96
97     render() {
98         return (
99             <div className="sentiment-analysis">
100                 <Columns>
101                     <Columns.Column>
102                     <form onSubmit={this.handleSubmit}>
103                         <Heading size={3} id="headerMargin">Search Terms: </Heading>
104                         <SearchTermsInput terms={this.state.terms}
105                                         onTermChange={this.handleTermChange}
106                                         onAddTerm={this.handleAddTerm}
107                                         onRemoveTerm={this.handleRemoveTerm}/>
108                         <Form.Field kind="group">
109                             <p className="control">
110                                 <Button color="info" type="submit" value="Submit">Submit</Button>
111                             </p>
112                             <p className="control">
113                                 <Button value="Reset" onClick={this.emptyTerms}>Reset</Button>
114                             </p>
115                         </Form.Field>
116                     </form>
117                     </Columns.Column>
118                     <Columns.Column>
119                         <PdfReport pdfLink={this.state.pdfLink} loading={this.state.isLoading}/>
120                     </Columns.Column>
121                 </Columns>
122             </div>
123         );
124     }
125 }
126
127 SentimentAnalysis.propTypes = {};
128
129 export default SentimentAnalysis;