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