Files
old-tellall/web/src/components/IntentDetails.js

117 lines
4.1 KiB
JavaScript
Raw Normal View History

2017-11-30 17:43:24 +01:00
import React, { Component } from 'react';
2017-12-01 11:03:48 +01:00
import {Button, SVGIcon, TextField} from 'react-md';
2018-01-14 01:00:35 +01:00
import '../css/components/IntentDetails.css';
2018-01-12 01:56:17 +01:00
import {QUESTION_MAX_LENGTH, ANSWER_MAX_LENGTH, INTENT_NAME_MAX_LENGTH} from '../config/constants';
2017-11-30 17:43:24 +01:00
class IntentDetails extends Component {
constructor(props){
super(props);
this.state= {intent: props.selectedIntent};
this.addQuestion = this.addQuestion.bind(this);
this.deleteQuestion = this.deleteQuestion.bind(this);
this.handleQuestionEdit = this.handleQuestionEdit.bind(this);
this.handleAnswerEdit = this.handleAnswerEdit.bind(this);
this.handleIntentNameEdit = this.handleIntentNameEdit.bind(this);
2017-11-30 17:43:24 +01:00
}
componentWillReceiveProps(props){
this.setState({intent: props.selectedIntent});
}
render() {
return (
2018-01-14 01:00:35 +01:00
<div className="RightPanelBox">
<div className="QuestionBox">
<TextField
id="intent name"
lineDirection="center"
placeholder="Intent name"
2018-01-16 13:38:05 +01:00
label="Question name"
2018-01-14 01:00:35 +01:00
className="md-cell md-cell--bottom IntentDetailsInputBoxes"
onChange={this.handleIntentNameEdit}
maxLength={INTENT_NAME_MAX_LENGTH}
value={this.state.intent.intentName} />
</div>
2018-01-16 13:38:05 +01:00
<h5 className="QuestionTitle">Question variants</h5>
2017-11-30 17:43:24 +01:00
{
this.state.intent.questions.map((question, index)=>{
return (
<div key={index} className="QuestionBox">
2017-12-01 11:03:48 +01:00
<TextField
id="intent question"
2017-12-01 11:03:48 +01:00
lineDirection="center"
placeholder="Question"
2018-01-14 01:00:35 +01:00
className="md-cell md-cell--bottom IntentDetailsInputBoxes"
maxLength={QUESTION_MAX_LENGTH}
2017-12-01 11:03:48 +01:00
rightIcon={<SVGIcon onClick={()=>{this.deleteQuestion(question)}}> <path fill="#000000" d="M19,4H15.5L14.5,3H9.5L8.5,4H5V6H19M6,19A2,2 0 0,0 8,21H16A2,2 0 0,0 18,19V7H6V19Z"/> </SVGIcon>}
onChange={(e)=>{this.handleQuestionEdit(e,index)}}
2017-12-01 11:03:48 +01:00
value={question}/>
2017-11-30 17:43:24 +01:00
</div>
);
})
}
2017-12-01 11:03:48 +01:00
<br></br>
2017-11-30 17:43:24 +01:00
{
2017-12-01 11:03:48 +01:00
<div className="QuestionBox">
<TextField
id="intent answer"
2017-12-01 11:03:48 +01:00
lineDirection="center"
label="Answer"
placeholder="Answer"
maxLength={ANSWER_MAX_LENGTH}
2018-01-14 01:00:35 +01:00
className="md-cell md-cell--bottom IntentDetailsInputBoxes"
onChange={this.handleAnswerEdit}
2017-12-01 11:03:48 +01:00
value={this.state.intent.answer}/>
</div>
2017-11-30 17:43:24 +01:00
}
<br></br>
<br></br>
2018-01-16 13:38:05 +01:00
<Button className="IntentDetailsButton" flat primary onClick={()=>{this.props.onDeleteIntentClick(this.state.intent)}} disabled={this.props.waiting}>Delete question</Button>
<Button className="IntentDetailsButton" flat primary swapTheming onClick={this.addQuestion} disabled={this.props.waiting}>Add variant</Button>
2018-01-14 01:00:35 +01:00
<Button className="IntentDetailsButton" flat primary swapTheming onClick={()=>{this.props.onSaveIntentClick(this.state.intent)}} disabled={this.props.waiting}>Save</Button>
2017-11-30 17:43:24 +01:00
</div>
);
}
addQuestion(){
let newIntent = this.state.intent;
newIntent.questions.push('');
2017-11-30 17:43:24 +01:00
this.setState({intent: newIntent});
}
deleteQuestion(question){
let newIntent = this.state.intent;
let removeId = newIntent.questions.indexOf(question);
if (removeId !== -1)
newIntent.questions.splice(removeId,1);
this.setState({intent: newIntent});
}
handleQuestionEdit(e,index){
if (e.length === QUESTION_MAX_LENGTH) return;
let newIntent = this.state.intent;
newIntent.questions[index] = e;
this.setState({intent: newIntent});
}
handleAnswerEdit(e){
if (e.length === ANSWER_MAX_LENGTH) return;
let newIntent = this.state.intent;
newIntent.answer = e;
this.setState({intent: newIntent});
}
handleIntentNameEdit(e){
if (e.length === INTENT_NAME_MAX_LENGTH) return;
let newIntent = this.state.intent;
newIntent.intentName = e;
this.setState({intent: newIntent});
}
2017-11-30 17:43:24 +01:00
}
export default IntentDetails;