import React, {Component} from 'react';
import {Button, SVGIcon, TextField} from 'react-md';
import AnswerSource from './AnswerSource.js';
import '../css/components/IntentDetails.css';
import '../css/Common.css';
import {
QUESTION_MAX_LENGTH,
ANSWER_MAX_LENGTH,
INTENT_NAME_MAX_LENGTH,
INTENT_EXPLANATION_MAX_LENGTH,
ANSWER_TYPE,
} from '../config/constants';
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.handleAnswerSourceEdit = this.handleAnswerSourceEdit.bind (this);
this.handleIntentNameEdit = this.handleIntentNameEdit.bind (this);
this.handleIntentExplanationEdit = this.handleIntentExplanationEdit.bind (
this
);
this.handleExternalSourceSave = this.handleExternalSourceSave.bind (this);
}
componentWillReceiveProps (props) {
this.setState ({intent: props.selectedIntent});
}
render () {
let answerBox;
switch (this.state.intent.answerType) {
case ANSWER_TYPE.PREDEFINED:
answerBox = (
);
break;
case ANSWER_TYPE.EXTERNAL_SOURCE_WP_JSON:
case ANSWER_TYPE.EXTERNAL_SOURCE_RSS:
answerBox = (
);
break;
}
return (
{' '}
In introduction, Alexa will help users to ask her the right questions about your business. For Example, she will say : "To ask us about our services, say : What do you do ? ". What do you do ? is defined in question field. Alexa will use first variation of question in intro.
Question variants
{this.state.intent.questions.map ((question, index) => {
return (
{
this.deleteQuestion (question);
}}
>
{' '}
{' '}
}
onChange={e => {
this.handleQuestionEdit (e, index);
}}
value={question}
/>
);
})}
add
{answerBox}
{
this.props.onSaveIntentClick (this.state.intent);
}}
disabled={this.props.waiting}
>
Save
{
this.props.onDeleteIntentClick (this.state.intent);
}}
disabled={this.props.waiting}
>
Delete
);
}
addQuestion () {
let newIntent = this.state.intent;
newIntent.questions.push ('');
this.setState ({intent: newIntent});
}
deleteQuestion (question) {
if (this.state.intent.questions.length > 1) {
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 || !/^[a-z,.' ]*$/i.test (e)) return;
let newIntent = this.state.intent;
newIntent.questions[index] = e;
this.setState ({intent: newIntent});
}
handleIntentExplanationEdit (e, index) {
if (e.length === INTENT_EXPLANATION_MAX_LENGTH || !/^[a-z,.' ]*$/i.test (e))
return;
let newIntent = this.state.intent;
newIntent.intentExplanation = e;
this.setState ({intent: newIntent});
}
handleAnswerEdit (e) {
if (e.length === ANSWER_MAX_LENGTH || !/^[a-z,.' ]*$/i.test (e)) return;
let newIntent = this.state.intent;
newIntent.answer = e;
this.setState ({intent: newIntent});
}
handleAnswerSourceEdit (e) {
if (e.length === ANSWER_MAX_LENGTH) return;
let newIntent = this.state.intent;
newIntent.externalAnswerSource = e;
this.setState ({intent: newIntent});
}
handleIntentNameEdit (e) {
if (e.length === INTENT_NAME_MAX_LENGTH || !/^[a-z]*$/i.test (e)) return;
let newIntent = this.state.intent;
newIntent.intentName = e;
this.setState ({intent: newIntent});
}
handleExternalSourceSave (answerType) {
let newIntent = this.state.intent;
newIntent.answerType = answerType;
this.setState ({intent: newIntent});
}
}
export default IntentDetails;